984 |
|
/** |
985 |
|
* Pops the given task for owner only if it is at the current top. |
986 |
|
*/ |
987 |
< |
final boolean tryUnpush(ForkJoinTask<?> task) { |
988 |
< |
int s = top - 1, cap, k; ForkJoinTask<?>[] a; |
989 |
< |
if ((a = array) != null && task != null && (cap = a.length) > 0 && |
990 |
< |
a[k = (cap - 1) & s] == task && casSlotToNull(a, k, task)) { |
991 |
< |
top = s; |
992 |
< |
return true; |
993 |
< |
} |
994 |
< |
return false; |
995 |
< |
} |
996 |
< |
|
997 |
< |
/** |
998 |
< |
* Locking version of tryUnpush. |
999 |
< |
*/ |
1000 |
< |
final boolean externalTryUnpush(ForkJoinTask<?> task) { |
987 |
> |
final boolean tryUnpush(ForkJoinTask<?> task, boolean owned) { |
988 |
|
boolean taken = false; |
989 |
|
int s = top, cap, k; ForkJoinTask<?>[] a; |
990 |
< |
if ((a = array) != null && task != null && (cap = a.length) > 0 && |
991 |
< |
a[k = (cap - 1) & (s - 1)] == task && tryLock()) { |
992 |
< |
if (top == s && array == a && |
993 |
< |
(taken = casSlotToNull(a, k, task))) |
994 |
< |
top = s - 1; |
995 |
< |
source = 0; // release lock |
990 |
> |
if ((a = array) != null && (cap = a.length) > 0 && |
991 |
> |
a[k = (cap - 1) & (s - 1)] == task) { |
992 |
> |
if (owned || tryLock()) { |
993 |
> |
if ((owned || (top == s && array == a)) && |
994 |
> |
(taken = casSlotToNull(a, k, task))) |
995 |
> |
top = s - 1; |
996 |
> |
if (!owned) |
997 |
> |
source = 0; // release lock |
998 |
> |
} |
999 |
|
} |
1000 |
|
return taken; |
1001 |
|
} |
1002 |
|
|
1003 |
|
/** |
1004 |
< |
* Deep form of pop: Traverses from top and removes task if |
1004 |
> |
* Deep form of tryUnpush: Traverses from top and removes task if |
1005 |
|
* present, shifting others to fill gap. |
1006 |
|
*/ |
1007 |
< |
final boolean tryRemove(ForkJoinTask<?> task) { |
1008 |
< |
int s = top, cap; ForkJoinTask<?>[] a; ForkJoinTask<?> t; |
1007 |
> |
final boolean tryRemove(ForkJoinTask<?> task, boolean owned) { |
1008 |
> |
boolean taken = false; |
1009 |
> |
int p = top, cap; ForkJoinTask<?>[] a; ForkJoinTask<?> t; |
1010 |
|
if ((a = array) != null && task != null && (cap = a.length) > 0) { |
1011 |
< |
for (int m = cap - 1, d = s - base, i = --s, k; d > 0; --i,--d) { |
1011 |
> |
int m = cap - 1, s = p - 1, d = p - base; |
1012 |
> |
for (int i = s, k; d > 0; --i, --d) { |
1013 |
|
if ((t = a[k = i & m]) == task) { |
1014 |
< |
if (!casSlotToNull(a, k, t)) |
1015 |
< |
break; |
1016 |
< |
for (int j = i; j != s; ) // shift down |
1017 |
< |
a[j & m] = getAndClearSlot(a, ++j & m); |
1018 |
< |
top = s; |
1019 |
< |
return true; |
1014 |
> |
if (owned || tryLock()) { |
1015 |
> |
if ((owned || (array == a && top == p)) && |
1016 |
> |
(taken = casSlotToNull(a, k, t))) { |
1017 |
> |
for (int j = i; j != s; ) // shift down |
1018 |
> |
a[j & m] = getAndClearSlot(a, ++j & m); |
1019 |
> |
top = s; |
1020 |
> |
} |
1021 |
> |
if (!owned) |
1022 |
> |
source = 0; |
1023 |
> |
} |
1024 |
> |
break; |
1025 |
|
} |
1026 |
|
} |
1027 |
|
} |
1028 |
< |
return false; |
1028 |
> |
return taken; |
1029 |
|
} |
1030 |
|
|
1031 |
|
// variants of poll |
1117 |
|
* @param limit max runs, or zero for no limit |
1118 |
|
* @return task status on exit |
1119 |
|
*/ |
1120 |
< |
final int helpComplete(CountedCompleter<?> task, boolean owned, |
1124 |
< |
int limit) { |
1120 |
> |
final int helpComplete(ForkJoinTask<?> task, boolean owned, int limit) { |
1121 |
|
int status = 0, cap, k, p, s; ForkJoinTask<?>[] a; ForkJoinTask<?> t; |
1122 |
|
while (task != null && (status = task.status) >= 0 && |
1123 |
|
(a = array) != null && (cap = a.length) > 0 && |
1695 |
|
} |
1696 |
|
|
1697 |
|
/** |
1702 |
– |
* Calls tryCompensate until success; needed for ForkJoinTask timed waits. |
1703 |
– |
* |
1704 |
– |
* @return 0 for no compensation, else ADJUST |
1705 |
– |
*/ |
1706 |
– |
final int preCompensate() { |
1707 |
– |
int comp; |
1708 |
– |
do {} while ((comp = tryCompensate(ctl)) < 0); |
1709 |
– |
return comp; |
1710 |
– |
} |
1711 |
– |
|
1712 |
– |
/** |
1698 |
|
* Helps if possible until the given task is done. Scans other |
1699 |
|
* queues for a task produced by one of w's stealers; returning |
1700 |
|
* compensated blocking sentinel if none are found. |
1769 |
|
* |
1770 |
|
* @param task root of CountedCompleter computation |
1771 |
|
* @param w caller's WorkQueue |
1772 |
+ |
* @param owned true if owned by a ForkJoinWorkerThread |
1773 |
|
* @return task status on exit, or ADJUST for compensated blocking |
1774 |
|
*/ |
1775 |
< |
final int helpComplete(CountedCompleter<?> task, WorkQueue w) { |
1775 |
> |
final int helpComplete(ForkJoinTask<?> task, WorkQueue w, boolean owned) { |
1776 |
|
int s = 0; |
1777 |
|
if (task != null && w != null) { |
1778 |
|
int r = w.config; |
1779 |
< |
boolean owned = (r & 1) != 0, scan = true, locals = true; |
1779 |
> |
boolean scan = true, locals = true; |
1780 |
|
long c = 0L; |
1781 |
|
outer: for (;;) { |
1782 |
|
if (locals) { // try locals before scanning |