99 |
|
/** |
100 |
|
* Implements AQS base release to always signal. |
101 |
|
* Status is changed in ack or cancel methods before calling, |
102 |
< |
* which is needed to handle cancellation races. |
102 |
> |
* Which we need to do because we need their return values. |
103 |
|
*/ |
104 |
|
public boolean releaseExclusiveState(int ignore) { |
105 |
|
return true; |
149 |
|
* to continue. |
150 |
|
*/ |
151 |
|
Object getItem() { |
152 |
< |
if (!ack()) |
153 |
< |
return null; |
154 |
< |
return extract(); |
152 |
> |
return (ack())? extract() : null; |
153 |
|
} |
154 |
|
|
155 |
|
/** |
156 |
< |
* Wait for a taker to take item placed by putter. |
156 |
> |
* Wait for a taker to take item placed by putter or time out. |
157 |
|
*/ |
158 |
< |
boolean waitForTake() throws InterruptedException { |
158 |
> |
boolean waitForTake(boolean timed, long nanos) throws InterruptedException { |
159 |
|
try { |
160 |
< |
acquireExclusiveInterruptibly(0); |
160 |
> |
if (!timed) |
161 |
> |
acquireExclusiveInterruptibly(0); |
162 |
> |
else if (!acquireExclusiveTimed(0, nanos) && cancel()) |
163 |
> |
return false; |
164 |
|
return true; |
165 |
|
} catch (InterruptedException ie) { |
166 |
|
if (cancel()) |
171 |
|
} |
172 |
|
|
173 |
|
/** |
174 |
< |
* Wait for a taker to take item placed by putter, or time out. |
174 |
< |
*/ |
175 |
< |
boolean waitForTake(long nanos) throws InterruptedException { |
176 |
< |
try { |
177 |
< |
return acquireExclusiveTimed(0, nanos) || !cancel(); |
178 |
< |
} catch (InterruptedException ie) { |
179 |
< |
if (cancel()) |
180 |
< |
throw ie; |
181 |
< |
Thread.currentThread().interrupt(); |
182 |
< |
return true; |
183 |
< |
} |
184 |
< |
} |
185 |
< |
|
186 |
< |
/** |
187 |
< |
* Wait for a putter to put item placed by taker. |
174 |
> |
* Wait for a putter to put item placed by taker, or time out. |
175 |
|
*/ |
176 |
< |
Object waitForPut() throws InterruptedException { |
176 |
> |
Object waitForPut(boolean timed, long nanos) throws InterruptedException { |
177 |
|
try { |
178 |
< |
acquireExclusiveInterruptibly(0); |
178 |
> |
if (!timed) |
179 |
> |
acquireExclusiveInterruptibly(0); |
180 |
> |
else if (!acquireExclusiveTimed(0, nanos) && cancel()) |
181 |
> |
return null; |
182 |
|
return extract(); |
183 |
|
} catch (InterruptedException ie) { |
184 |
|
if (cancel()) |
188 |
|
} |
189 |
|
} |
190 |
|
|
201 |
– |
/** |
202 |
– |
* Wait for a putter to put item placed by taker, or time out. |
203 |
– |
*/ |
204 |
– |
Object waitForPut(long nanos) throws InterruptedException { |
205 |
– |
try { |
206 |
– |
if (acquireExclusiveTimed(0, nanos) || !cancel()) |
207 |
– |
return extract(); |
208 |
– |
return null; |
209 |
– |
} catch (InterruptedException ie) { |
210 |
– |
if (cancel()) |
211 |
– |
throw ie; |
212 |
– |
Thread.currentThread().interrupt(); |
213 |
– |
return extract(); |
214 |
– |
} |
215 |
– |
} |
191 |
|
} |
192 |
|
|
193 |
|
/** |
233 |
|
} |
234 |
|
|
235 |
|
if (mustWait) |
236 |
< |
return timed? node.waitForTake(nanos) : node.waitForTake(); |
236 |
> |
return node.waitForTake(timed, nanos); |
237 |
|
|
238 |
|
else if (node.setItem(x)) |
239 |
|
return true; |
261 |
|
} |
262 |
|
|
263 |
|
if (mustWait) { |
264 |
< |
Object x = timed? node.waitForPut(nanos) : node.waitForPut(); |
264 |
> |
Object x = node.waitForPut(timed, nanos); |
265 |
|
return (E)x; |
266 |
|
} |
267 |
|
else { |