21 |
|
* create such a subclass, you will also need to supply a custom |
22 |
|
* ForkJoinWorkerThreadFactory to use it in a ForkJoinPool. |
23 |
|
* |
24 |
+ |
* @since 1.7 |
25 |
+ |
* @author Doug Lea |
26 |
|
*/ |
27 |
|
public class ForkJoinWorkerThread extends Thread { |
28 |
|
/* |
275 |
|
int s = runState; |
276 |
|
if (s >= state) |
277 |
|
return false; |
278 |
< |
if (_unsafe.compareAndSwapInt(this, runStateOffset, s, state)) |
278 |
> |
if (UNSAFE.compareAndSwapInt(this, runStateOffset, s, state)) |
279 |
|
return true; |
280 |
|
} |
281 |
|
} |
410 |
|
* Caller must ensure q is non-null and index is in range. |
411 |
|
*/ |
412 |
|
private static void setSlot(ForkJoinTask<?>[] q, int i, |
413 |
< |
ForkJoinTask<?> t){ |
414 |
< |
_unsafe.putOrderedObject(q, (i << qShift) + qBase, t); |
413 |
> |
ForkJoinTask<?> t) { |
414 |
> |
UNSAFE.putOrderedObject(q, (i << qShift) + qBase, t); |
415 |
|
} |
416 |
|
|
417 |
|
/** |
420 |
|
*/ |
421 |
|
private static boolean casSlotNull(ForkJoinTask<?>[] q, int i, |
422 |
|
ForkJoinTask<?> t) { |
423 |
< |
return _unsafe.compareAndSwapObject(q, (i << qShift) + qBase, t, null); |
423 |
> |
return UNSAFE.compareAndSwapObject(q, (i << qShift) + qBase, t, null); |
424 |
|
} |
425 |
|
|
426 |
|
/** |
427 |
|
* Sets sp in store-order. |
428 |
|
*/ |
429 |
|
private void storeSp(int s) { |
430 |
< |
_unsafe.putOrderedInt(this, spOffset, s); |
430 |
> |
UNSAFE.putOrderedInt(this, spOffset, s); |
431 |
|
} |
432 |
|
|
433 |
|
// Main queue methods |
517 |
|
if (q == null) |
518 |
|
return null; |
519 |
|
int mask = q.length - 1; |
520 |
< |
int i = locallyFifo? base : (sp - 1); |
520 |
> |
int i = locallyFifo ? base : (sp - 1); |
521 |
|
return q[i & mask]; |
522 |
|
} |
523 |
|
|
580 |
|
ForkJoinWorkerThread v = ws[mask & idx]; |
581 |
|
if (v == null || v.sp == v.base) { |
582 |
|
if (probes <= mask) |
583 |
< |
idx = (probes++ < 0)? r : (idx + 1); |
583 |
> |
idx = (probes++ < 0) ? r : (idx + 1); |
584 |
|
else |
585 |
|
break; |
586 |
|
} |
601 |
|
* @return a task, if available |
602 |
|
*/ |
603 |
|
final ForkJoinTask<?> pollTask() { |
604 |
< |
ForkJoinTask<?> t = locallyFifo? deqTask() : popTask(); |
604 |
> |
ForkJoinTask<?> t = locallyFifo ? deqTask() : popTask(); |
605 |
|
if (t == null && (t = scan()) != null) |
606 |
|
++stealCount; |
607 |
|
return t; |
613 |
|
* @return a task, if available |
614 |
|
*/ |
615 |
|
final ForkJoinTask<?> pollLocalTask() { |
616 |
< |
return locallyFifo? deqTask() : popTask(); |
616 |
> |
return locallyFifo ? deqTask() : popTask(); |
617 |
|
} |
618 |
|
|
619 |
|
/** |
693 |
|
* Returns an estimate of the number of tasks in the queue. |
694 |
|
*/ |
695 |
|
final int getQueueSize() { |
696 |
< |
int n = sp - base; |
697 |
< |
return n < 0? 0 : n; // suppress momentarily negative values |
696 |
> |
// suppress momentarily negative values |
697 |
> |
return Math.max(0, sp - base); |
698 |
|
} |
699 |
|
|
700 |
|
/** |
758 |
|
|
759 |
|
private static long fieldOffset(String fieldName) |
760 |
|
throws NoSuchFieldException { |
761 |
< |
return _unsafe.objectFieldOffset |
761 |
> |
return UNSAFE.objectFieldOffset |
762 |
|
(ForkJoinWorkerThread.class.getDeclaredField(fieldName)); |
763 |
|
} |
764 |
|
|
765 |
< |
static final Unsafe _unsafe; |
765 |
> |
static final Unsafe UNSAFE; |
766 |
|
static final long baseOffset; |
767 |
|
static final long spOffset; |
768 |
|
static final long runStateOffset; |
770 |
|
static final int qShift; |
771 |
|
static { |
772 |
|
try { |
773 |
< |
_unsafe = getUnsafe(); |
773 |
> |
UNSAFE = getUnsafe(); |
774 |
|
baseOffset = fieldOffset("base"); |
775 |
|
spOffset = fieldOffset("sp"); |
776 |
|
runStateOffset = fieldOffset("runState"); |
777 |
< |
qBase = _unsafe.arrayBaseOffset(ForkJoinTask[].class); |
778 |
< |
int s = _unsafe.arrayIndexScale(ForkJoinTask[].class); |
777 |
> |
qBase = UNSAFE.arrayBaseOffset(ForkJoinTask[].class); |
778 |
> |
int s = UNSAFE.arrayIndexScale(ForkJoinTask[].class); |
779 |
|
if ((s & (s-1)) != 0) |
780 |
|
throw new Error("data type scale not a power of two"); |
781 |
|
qShift = 31 - Integer.numberOfLeadingZeros(s); |