99 |
|
* exchanger is used by a large number of threads. |
100 |
|
*/ |
101 |
|
|
102 |
< |
/** |
102 |
> |
/** |
103 |
|
* Size of collision space. Using a size of half the number of |
104 |
|
* CPUs provides enough space for threads to find each other but |
105 |
|
* not so much that it would always require one or more to time |
106 |
|
* out to become unstuck. Note that the arena array holds SIZE+1 |
107 |
|
* elements, to include the top-of-stack slot. |
108 |
|
*/ |
109 |
< |
private static final int SIZE = |
109 |
> |
private static final int SIZE = |
110 |
|
(Runtime.getRuntime().availableProcessors() + 1) / 2; |
111 |
|
|
112 |
|
/** |
116 |
|
*/ |
117 |
|
private static final long BACKOFF_BASE = 128L; |
118 |
|
|
119 |
< |
/** |
119 |
> |
/** |
120 |
|
* Sentinel item representing cancellation. This value is placed |
121 |
|
* in holes on cancellation, and used as a return value from Node |
122 |
|
* methods to indicate failure to set or get hole. |
123 |
< |
**/ |
123 |
> |
*/ |
124 |
|
static final Object FAIL = new Object(); |
125 |
|
|
126 |
< |
/** |
126 |
> |
/** |
127 |
|
* The collision arena. arena[0] is used as the top of the stack. |
128 |
|
* The remainder is used as the collision elimination space. |
129 |
< |
* Each slot holds an AtomicReference<Node>, but this cannot be |
129 |
> |
* Each slot holds an AtomicReference<Node>, but this cannot be |
130 |
|
* expressed for arrays, so elements are casted on each use. |
131 |
|
*/ |
132 |
|
private final AtomicReference[] arena; |
246 |
|
} |
247 |
|
|
248 |
|
/** |
249 |
< |
* Wait for and get the hole filled in by another thread. |
250 |
< |
* Fail if timed out or interrupted before hole filled. |
249 |
> |
* Waits for and gets the hole filled in by another thread. |
250 |
> |
* Fails if timed out or interrupted before hole filled. |
251 |
|
* @param timed true if the wait is timed. |
252 |
|
* @param nanos if timed, the maximum wait time. |
253 |
|
* @return on success, the hole; on failure, FAIL. |
282 |
|
* it is {@link Thread#interrupt interrupted}), |
283 |
|
* and then transfers the given object to it, receiving its object |
284 |
|
* in return. |
285 |
+ |
* |
286 |
|
* <p>If another thread is already waiting at the exchange point then |
287 |
|
* it is resumed for thread scheduling purposes and receives the object |
288 |
|
* passed in by the current thread. The current thread returns immediately, |
289 |
|
* receiving the object passed to the exchange by that other thread. |
290 |
+ |
* |
291 |
|
* <p>If no other thread is already waiting at the exchange then the |
292 |
|
* current thread is disabled for thread scheduling purposes and lies |
293 |
|
* dormant until one of two things happens: |