64 |
|
} |
65 |
|
|
66 |
|
/** |
67 |
< |
* Insert element at current put position and advance. |
67 |
> |
* Insert element at current put position, advance, and signal. |
68 |
> |
* Call only when holding lock. |
69 |
|
*/ |
70 |
|
private void insert(E x) { |
71 |
|
items[putIndex] = x; |
72 |
|
putIndex = inc(putIndex); |
73 |
|
++count; |
74 |
+ |
notEmpty.signal(); |
75 |
|
} |
76 |
|
|
77 |
|
/** |
78 |
< |
* Extract element at current take position and advance. |
78 |
> |
* Extract element at current take position, advance, and signal. |
79 |
> |
* Call only when holding lock. |
80 |
|
*/ |
81 |
|
private E extract() { |
82 |
|
E x = items[takeIndex]; |
83 |
|
items[takeIndex] = null; |
84 |
|
takeIndex = inc(takeIndex); |
85 |
|
--count; |
86 |
+ |
notFull.signal(); |
87 |
|
return x; |
88 |
|
} |
89 |
|
|
90 |
|
/** |
91 |
< |
* Utility for remove and iterator.remove: Delete item at position |
92 |
< |
* i by sliding over all others up through putIndex. |
91 |
> |
* Utility for remove and iterator.remove: Delete item at position i. |
92 |
> |
* Call only when holding lock. |
93 |
|
*/ |
94 |
|
void removeAt(int i) { |
95 |
< |
for (;;) { |
96 |
< |
int nexti = inc(i); |
97 |
< |
items[i] = items[nexti]; |
98 |
< |
if (nexti != putIndex) |
99 |
< |
i = nexti; |
100 |
< |
else { |
101 |
< |
items[nexti] = null; |
102 |
< |
putIndex = i; |
103 |
< |
--count; |
104 |
< |
return; |
95 |
> |
// if removing front item, just advance |
96 |
> |
if (i == takeIndex) { |
97 |
> |
items[takeIndex] = null; |
98 |
> |
takeIndex = inc(takeIndex); |
99 |
> |
} |
100 |
> |
else { |
101 |
> |
// slide over all others up through putIndex. |
102 |
> |
for (;;) { |
103 |
> |
int nexti = inc(i); |
104 |
> |
if (nexti != putIndex) { |
105 |
> |
items[i] = items[nexti]; |
106 |
> |
i = nexti; |
107 |
> |
} |
108 |
> |
else { |
109 |
> |
items[i] = null; |
110 |
> |
putIndex = i; |
111 |
> |
break; |
112 |
> |
} |
113 |
|
} |
114 |
|
} |
115 |
+ |
--count; |
116 |
+ |
notFull.signal(); |
117 |
|
} |
118 |
|
|
119 |
|
/** |
202 |
|
throw ie; |
203 |
|
} |
204 |
|
insert(x); |
191 |
– |
notEmpty.signal(); |
205 |
|
} |
206 |
|
finally { |
207 |
|
lock.unlock(); |
223 |
|
throw ie; |
224 |
|
} |
225 |
|
E x = extract(); |
213 |
– |
notFull.signal(); |
226 |
|
return x; |
227 |
|
} |
228 |
|
finally { |
243 |
|
return false; |
244 |
|
else { |
245 |
|
insert(x); |
234 |
– |
notEmpty.signal(); |
246 |
|
return true; |
247 |
|
} |
248 |
|
} |
262 |
|
if (count == 0) |
263 |
|
return null; |
264 |
|
E x = extract(); |
254 |
– |
notFull.signal(); |
265 |
|
return x; |
266 |
|
} |
267 |
|
finally { |
289 |
|
for (;;) { |
290 |
|
if (count != items.length) { |
291 |
|
insert(x); |
282 |
– |
notEmpty.signal(); |
292 |
|
return true; |
293 |
|
} |
294 |
|
if (nanos <= 0) |
327 |
|
for (;;) { |
328 |
|
if (count != 0) { |
329 |
|
E x = extract(); |
321 |
– |
notFull.signal(); |
330 |
|
return x; |
331 |
|
} |
332 |
|
if (nanos <= 0) |
549 |
|
throw new IllegalStateException(); |
550 |
|
lastRet = -1; |
551 |
|
|
552 |
< |
nextIndex = i; // back up cursor |
552 |
> |
int ti = takeIndex; |
553 |
|
removeAt(i); |
554 |
+ |
// back up cursor (reset to front if was first element) |
555 |
+ |
nextIndex = (i == ti) ? takeIndex : i; |
556 |
|
checkNext(); |
557 |
|
} |
558 |
|
finally { |