Java Code Examples for java.util.concurrent.ThreadLocalRandom#nextSecondarySeed()

The following examples show how to use java.util.concurrent.ThreadLocalRandom#nextSecondarySeed() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ForkJoinPool.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a (probably) non-empty steal queue, if one is found
 * during a scan, else null.  This method must be retried by
 * caller if, by the time it tries to use the queue, it is empty.
 */
private WorkQueue findNonEmptyStealQueue() {
    WorkQueue[] ws; int m;  // one-shot version of scan loop
    int r = ThreadLocalRandom.nextSecondarySeed();
    if ((ws = workQueues) != null && (m = ws.length - 1) >= 0) {
        for (int origin = r & m, k = origin, oldSum = 0, checkSum = 0;;) {
            WorkQueue q; int b;
            if ((q = ws[k]) != null) {
                if ((b = q.base) - q.top < 0)
                    return q;
                checkSum += b;
            }
            if ((k = (k + 1) & m) == origin) {
                if (oldSum == (oldSum = checkSum))
                    break;
                checkSum = 0;
            }
        }
    }
    return null;
}
 
Example 2
Source File: ForkJoinPool.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns a (probably) non-empty steal queue, if one is found
 * during a scan, else null.  This method must be retried by
 * caller if, by the time it tries to use the queue, it is empty.
 */
private WorkQueue findNonEmptyStealQueue() {
    WorkQueue[] ws; int m;  // one-shot version of scan loop
    int r = ThreadLocalRandom.nextSecondarySeed();
    if ((ws = workQueues) != null && (m = ws.length - 1) >= 0) {
        for (int origin = r & m, k = origin, oldSum = 0, checkSum = 0;;) {
            WorkQueue q; int b;
            if ((q = ws[k]) != null) {
                if ((b = q.base) - q.top < 0)
                    return q;
                checkSum += b;
            }
            if ((k = (k + 1) & m) == origin) {
                if (oldSum == (oldSum = checkSum))
                    break;
                checkSum = 0;
            }
        }
    }
    return null;
}
 
Example 3
Source File: ForkJoinPool.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns a (probably) non-empty steal queue, if one is found
 * during a scan, else null.  This method must be retried by
 * caller if, by the time it tries to use the queue, it is empty.
 */
private WorkQueue findNonEmptyStealQueue() {
    WorkQueue[] ws; int m;  // one-shot version of scan loop
    int r = ThreadLocalRandom.nextSecondarySeed();
    if ((ws = workQueues) != null && (m = ws.length - 1) >= 0) {
        for (int origin = r & m, k = origin, oldSum = 0, checkSum = 0;;) {
            WorkQueue q; int b;
            if ((q = ws[k]) != null) {
                if ((b = q.base) - q.top < 0)
                    return q;
                checkSum += b;
            }
            if ((k = (k + 1) & m) == origin) {
                if (oldSum == (oldSum = checkSum))
                    break;
                checkSum = 0;
            }
        }
    }
    return null;
}
 
Example 4
Source File: ForkJoinPool.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns a (probably) non-empty steal queue, if one is found
 * during a scan, else null.  This method must be retried by
 * caller if, by the time it tries to use the queue, it is empty.
 */
private WorkQueue findNonEmptyStealQueue() {
    WorkQueue[] ws; int m;  // one-shot version of scan loop
    int r = ThreadLocalRandom.nextSecondarySeed();
    if ((ws = workQueues) != null && (m = ws.length - 1) >= 0) {
        for (int origin = r & m, k = origin, oldSum = 0, checkSum = 0;;) {
            WorkQueue q; int b;
            if ((q = ws[k]) != null) {
                if ((b = q.base) - q.top < 0)
                    return q;
                checkSum += b;
            }
            if ((k = (k + 1) & m) == origin) {
                if (oldSum == (oldSum = checkSum))
                    break;
                checkSum = 0;
            }
        }
    }
    return null;
}
 
Example 5
Source File: ForkJoinPool.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns a (probably) non-empty steal queue, if one is found
 * during a scan, else null.  This method must be retried by
 * caller if, by the time it tries to use the queue, it is empty.
 */
private WorkQueue findNonEmptyStealQueue() {
    WorkQueue[] ws; int m;  // one-shot version of scan loop
    int r = ThreadLocalRandom.nextSecondarySeed();
    if ((ws = workQueues) != null && (m = ws.length - 1) >= 0) {
        for (int origin = r & m, k = origin, oldSum = 0, checkSum = 0;;) {
            WorkQueue q; int b;
            if ((q = ws[k]) != null) {
                if ((b = q.base) - q.top < 0)
                    return q;
                checkSum += b;
            }
            if ((k = (k + 1) & m) == origin) {
                if (oldSum == (oldSum = checkSum))
                    break;
                checkSum = 0;
            }
        }
    }
    return null;
}
 
Example 6
Source File: ForkJoinPool.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns a (probably) non-empty steal queue, if one is found
 * during a scan, else null.  This method must be retried by
 * caller if, by the time it tries to use the queue, it is empty.
 */
private WorkQueue findNonEmptyStealQueue() {
    WorkQueue[] ws; int m;  // one-shot version of scan loop
    int r = ThreadLocalRandom.nextSecondarySeed();
    if ((ws = workQueues) != null && (m = ws.length - 1) >= 0) {
        for (int origin = r & m, k = origin, oldSum = 0, checkSum = 0;;) {
            WorkQueue q; int b;
            if ((q = ws[k]) != null) {
                if ((b = q.base) - q.top < 0)
                    return q;
                checkSum += b;
            }
            if ((k = (k + 1) & m) == origin) {
                if (oldSum == (oldSum = checkSum))
                    break;
                checkSum = 0;
            }
        }
    }
    return null;
}
 
Example 7
Source File: ForkJoinPool.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns a (probably) non-empty steal queue, if one is found
 * during a scan, else null.  This method must be retried by
 * caller if, by the time it tries to use the queue, it is empty.
 */
private WorkQueue findNonEmptyStealQueue() {
    int r = ThreadLocalRandom.nextSecondarySeed();
    for (;;) {
        int ps = plock, m; WorkQueue[] ws; WorkQueue q;
        if ((ws = workQueues) != null && (m = ws.length - 1) >= 0) {
            for (int j = (m + 1) << 2; j >= 0; --j) {
                if ((q = ws[(((r - j) << 1) | 1) & m]) != null &&
                    q.base - q.top < 0)
                    return q;
            }
        }
        if (plock == ps)
            return null;
    }
}
 
Example 8
Source File: ForkJoinPool.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Acquires the plock lock to protect worker array and related
 * updates. This method is called only if an initial CAS on plock
 * fails. This acts as a spinlock for normal cases, but falls back
 * to builtin monitor to block when (rarely) needed. This would be
 * a terrible idea for a highly contended lock, but works fine as
 * a more conservative alternative to a pure spinlock.
 */
private int acquirePlock() {
    int spins = PL_SPINS, ps, nps;
    for (;;) {
        if (((ps = plock) & PL_LOCK) == 0 &&
            U.compareAndSwapInt(this, PLOCK, ps, nps = ps + PL_LOCK))
            return nps;
        else if (spins >= 0) {
            if (ThreadLocalRandom.nextSecondarySeed() >= 0)
                --spins;
        }
        else if (U.compareAndSwapInt(this, PLOCK, ps, ps | PL_SIGNAL)) {
            synchronized (this) {
                if ((plock & PL_SIGNAL) != 0) {
                    try {
                        wait();
                    } catch (InterruptedException ie) {
                        try {
                            Thread.currentThread().interrupt();
                        } catch (SecurityException ignore) {
                        }
                    }
                }
                else
                    notifyAll();
            }
        }
    }
}
 
Example 9
Source File: CompletableFuture.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns raw result after waiting, or null if interruptible and
 * interrupted.
 */
private Object waitingGet(boolean interruptible) {
    Signaller q = null;
    boolean queued = false;
    int spins = -1;
    Object r;
    while ((r = result) == null) {
        if (spins < 0)
            spins = SPINS;
        else if (spins > 0) {
            if (ThreadLocalRandom.nextSecondarySeed() >= 0)
                --spins;
        }
        else if (q == null)
            q = new Signaller(interruptible, 0L, 0L);
        else if (!queued)
            queued = tryPushStack(q);
        else if (interruptible && q.interruptControl < 0) {
            q.thread = null;
            cleanStack();
            return null;
        }
        else if (q.thread != null && result == null) {
            try {
                ForkJoinPool.managedBlock(q);
            } catch (InterruptedException ie) {
                q.interruptControl = -1;
            }
        }
    }
    if (q != null) {
        q.thread = null;
        if (q.interruptControl < 0) {
            if (interruptible)
                r = null; // report interruption
            else
                Thread.currentThread().interrupt();
        }
    }
    postComplete();
    return r;
}
 
Example 10
Source File: ForkJoinPool.java    From Java8CN with Apache License 2.0 4 votes vote down vote up
/**
 * Spins and/or blocks until runstate lock is available.  See
 * above for explanation.
 */
private int awaitRunStateLock() {
    Object lock;
    boolean wasInterrupted = false;
    for (int spins = SPINS, r = 0, rs, ns;;) {
        if (((rs = runState) & RSLOCK) == 0) {
            if (U.compareAndSwapInt(this, RUNSTATE, rs, ns = rs | RSLOCK)) {
                if (wasInterrupted) {
                    try {
                        Thread.currentThread().interrupt();
                    } catch (SecurityException ignore) {
                    }
                }
                return ns;
            }
        }
        else if (r == 0)
            r = ThreadLocalRandom.nextSecondarySeed();
        else if (spins > 0) {
            r ^= r << 6; r ^= r >>> 21; r ^= r << 7; // xorshift
            if (r >= 0)
                --spins;
        }
        else if ((rs & STARTED) == 0 || (lock = stealCounter) == null)
            Thread.yield();   // initialization race
        else if (U.compareAndSwapInt(this, RUNSTATE, rs, rs | RSIGNAL)) {
            synchronized (lock) {
                if ((runState & RSIGNAL) != 0) {
                    try {
                        lock.wait();
                    } catch (InterruptedException ie) {
                        if (!(Thread.currentThread() instanceof
                              ForkJoinWorkerThread))
                            wasInterrupted = true;
                    }
                }
                else
                    lock.notifyAll();
            }
        }
    }
}
 
Example 11
Source File: CompletableFuture.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns raw result after waiting, or null if interruptible and
 * interrupted.
 */
private Object waitingGet(boolean interruptible) {
    WaitNode q = null;
    boolean queued = false;
    int spins = SPINS;
    for (Object r;;) {
        if ((r = result) != null) {
            if (q != null) { // suppress unpark
                q.thread = null;
                if (q.interruptControl < 0) {
                    if (interruptible) {
                        removeWaiter(q);
                        return null;
                    }
                    Thread.currentThread().interrupt();
                }
            }
            postComplete(); // help release others
            return r;
        }
        else if (spins > 0) {
            int rnd = ThreadLocalRandom.nextSecondarySeed();
            if (rnd == 0)
                rnd = ThreadLocalRandom.current().nextInt();
            if (rnd >= 0)
                --spins;
        }
        else if (q == null)
            q = new WaitNode(interruptible, 0L, 0L);
        else if (!queued)
            queued = UNSAFE.compareAndSwapObject(this, WAITERS,
                                                 q.next = waiters, q);
        else if (interruptible && q.interruptControl < 0) {
            removeWaiter(q);
            return null;
        }
        else if (q.thread != null && result == null) {
            try {
                ForkJoinPool.managedBlock(q);
            } catch (InterruptedException ex) {
                q.interruptControl = -1;
            }
        }
    }
}
 
Example 12
Source File: ForkJoinPool.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Spins and/or blocks until runstate lock is available.  See
 * above for explanation.
 */
private int awaitRunStateLock() {
    Object lock;
    boolean wasInterrupted = false;
    for (int spins = SPINS, r = 0, rs, ns;;) {
        if (((rs = runState) & RSLOCK) == 0) {
            if (U.compareAndSwapInt(this, RUNSTATE, rs, ns = rs | RSLOCK)) {
                if (wasInterrupted) {
                    try {
                        Thread.currentThread().interrupt();
                    } catch (SecurityException ignore) {
                    }
                }
                return ns;
            }
        }
        else if (r == 0)
            r = ThreadLocalRandom.nextSecondarySeed();
        else if (spins > 0) {
            r ^= r << 6; r ^= r >>> 21; r ^= r << 7; // xorshift
            if (r >= 0)
                --spins;
        }
        else if ((rs & STARTED) == 0 || (lock = stealCounter) == null)
            Thread.yield();   // initialization race
        else if (U.compareAndSwapInt(this, RUNSTATE, rs, rs | RSIGNAL)) {
            synchronized (lock) {
                if ((runState & RSIGNAL) != 0) {
                    try {
                        lock.wait();
                    } catch (InterruptedException ie) {
                        if (!(Thread.currentThread() instanceof
                              ForkJoinWorkerThread))
                            wasInterrupted = true;
                    }
                }
                else
                    lock.notifyAll();
            }
        }
    }
}
 
Example 13
Source File: CompletableFuture.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns raw result after waiting, or null if interruptible and
 * interrupted.
 */
private Object waitingGet(boolean interruptible) {
    Signaller q = null;
    boolean queued = false;
    int spins = -1;
    Object r;
    while ((r = result) == null) {
        if (spins < 0)
            spins = (Runtime.getRuntime().availableProcessors() > 1) ?
                1 << 8 : 0; // Use brief spin-wait on multiprocessors
        else if (spins > 0) {
            if (ThreadLocalRandom.nextSecondarySeed() >= 0)
                --spins;
        }
        else if (q == null)
            q = new Signaller(interruptible, 0L, 0L);
        else if (!queued)
            queued = tryPushStack(q);
        else if (interruptible && q.interruptControl < 0) {
            q.thread = null;
            cleanStack();
            return null;
        }
        else if (q.thread != null && result == null) {
            try {
                ForkJoinPool.managedBlock(q);
            } catch (InterruptedException ie) {
                q.interruptControl = -1;
            }
        }
    }
    if (q != null) {
        q.thread = null;
        if (q.interruptControl < 0) {
            if (interruptible)
                r = null; // report interruption
            else
                Thread.currentThread().interrupt();
        }
    }
    postComplete();
    return r;
}
 
Example 14
Source File: CompletableFuture.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns raw result after waiting, or null if interruptible and
 * interrupted.
 */
private Object waitingGet(boolean interruptible) {
    Signaller q = null;
    boolean queued = false;
    int spins = -1;
    Object r;
    while ((r = result) == null) {
        if (spins < 0)
            spins = (Runtime.getRuntime().availableProcessors() > 1) ?
                1 << 8 : 0; // Use brief spin-wait on multiprocessors
        else if (spins > 0) {
            if (ThreadLocalRandom.nextSecondarySeed() >= 0)
                --spins;
        }
        else if (q == null)
            q = new Signaller(interruptible, 0L, 0L);
        else if (!queued)
            queued = tryPushStack(q);
        else if (interruptible && q.interruptControl < 0) {
            q.thread = null;
            cleanStack();
            return null;
        }
        else if (q.thread != null && result == null) {
            try {
                ForkJoinPool.managedBlock(q);
            } catch (InterruptedException ie) {
                q.interruptControl = -1;
            }
        }
    }
    if (q != null) {
        q.thread = null;
        if (q.interruptControl < 0) {
            if (interruptible)
                r = null; // report interruption
            else
                Thread.currentThread().interrupt();
        }
    }
    postComplete();
    return r;
}
 
Example 15
Source File: ForkJoinPool.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
/**
 * Spins and/or blocks until runstate lock is available.  See
 * above for explanation.
 */
private int awaitRunStateLock() {
    Object lock;
    boolean wasInterrupted = false;
    for (int spins = SPINS, r = 0, rs, ns;;) {
        if (((rs = runState) & RSLOCK) == 0) {
            if (U.compareAndSwapInt(this, RUNSTATE, rs, ns = rs | RSLOCK)) {
                if (wasInterrupted) {
                    try {
                        Thread.currentThread().interrupt();
                    } catch (SecurityException ignore) {
                    }
                }
                return ns;
            }
        }
        else if (r == 0)
            r = ThreadLocalRandom.nextSecondarySeed();
        else if (spins > 0) {
            r ^= r << 6; r ^= r >>> 21; r ^= r << 7; // xorshift
            if (r >= 0)
                --spins;
        }
        else if ((rs & STARTED) == 0 || (lock = stealCounter) == null)
            Thread.yield();   // initialization race
        else if (U.compareAndSwapInt(this, RUNSTATE, rs, rs | RSIGNAL)) {
            synchronized (lock) {
                if ((runState & RSIGNAL) != 0) {
                    try {
                        lock.wait();
                    } catch (InterruptedException ie) {
                        if (!(Thread.currentThread() instanceof
                              ForkJoinWorkerThread))
                            wasInterrupted = true;
                    }
                }
                else
                    lock.notifyAll();
            }
        }
    }
}
 
Example 16
Source File: ForkJoinPool.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Spins and/or blocks until runstate lock is available.  See
 * above for explanation.
 */
private int awaitRunStateLock() {
    Object lock;
    boolean wasInterrupted = false;
    for (int spins = SPINS, r = 0, rs, ns;;) {
        if (((rs = runState) & RSLOCK) == 0) {
            if (U.compareAndSwapInt(this, RUNSTATE, rs, ns = rs | RSLOCK)) {
                if (wasInterrupted) {
                    try {
                        Thread.currentThread().interrupt();
                    } catch (SecurityException ignore) {
                    }
                }
                return ns;
            }
        }
        else if (r == 0)
            r = ThreadLocalRandom.nextSecondarySeed();
        else if (spins > 0) {
            r ^= r << 6; r ^= r >>> 21; r ^= r << 7; // xorshift
            if (r >= 0)
                --spins;
        }
        else if ((rs & STARTED) == 0 || (lock = stealCounter) == null)
            Thread.yield();   // initialization race
        else if (U.compareAndSwapInt(this, RUNSTATE, rs, rs | RSIGNAL)) {
            synchronized (lock) {
                if ((runState & RSIGNAL) != 0) {
                    try {
                        lock.wait();
                    } catch (InterruptedException ie) {
                        if (!(Thread.currentThread() instanceof
                              ForkJoinWorkerThread))
                            wasInterrupted = true;
                    }
                }
                else
                    lock.notifyAll();
            }
        }
    }
}
 
Example 17
Source File: ForkJoinPool.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Spins and/or blocks until runstate lock is available.  See
 * above for explanation.
 */
private int awaitRunStateLock() {
    Object lock;
    boolean wasInterrupted = false;
    for (int spins = SPINS, r = 0, rs, ns;;) {
        if (((rs = runState) & RSLOCK) == 0) {
            if (U.compareAndSwapInt(this, RUNSTATE, rs, ns = rs | RSLOCK)) {
                if (wasInterrupted) {
                    try {
                        Thread.currentThread().interrupt();
                    } catch (SecurityException ignore) {
                    }
                }
                return ns;
            }
        }
        else if (r == 0)
            r = ThreadLocalRandom.nextSecondarySeed();
        else if (spins > 0) {
            r ^= r << 6; r ^= r >>> 21; r ^= r << 7; // xorshift
            if (r >= 0)
                --spins;
        }
        else if ((rs & STARTED) == 0 || (lock = stealCounter) == null)
            Thread.yield();   // initialization race
        else if (U.compareAndSwapInt(this, RUNSTATE, rs, rs | RSIGNAL)) {
            synchronized (lock) {
                if ((runState & RSIGNAL) != 0) {
                    try {
                        lock.wait();
                    } catch (InterruptedException ie) {
                        if (!(Thread.currentThread() instanceof
                              ForkJoinWorkerThread))
                            wasInterrupted = true;
                    }
                }
                else
                    lock.notifyAll();
            }
        }
    }
}
 
Example 18
Source File: ForkJoinPool.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Spins and/or blocks until runstate lock is available.  See
 * above for explanation.
 */
private int awaitRunStateLock() {
    Object lock;
    boolean wasInterrupted = false;
    for (int spins = SPINS, r = 0, rs, ns;;) {
        if (((rs = runState) & RSLOCK) == 0) {
            if (U.compareAndSwapInt(this, RUNSTATE, rs, ns = rs | RSLOCK)) {
                if (wasInterrupted) {
                    try {
                        Thread.currentThread().interrupt();
                    } catch (SecurityException ignore) {
                    }
                }
                return ns;
            }
        }
        else if (r == 0)
            r = ThreadLocalRandom.nextSecondarySeed();
        else if (spins > 0) {
            r ^= r << 6; r ^= r >>> 21; r ^= r << 7; // xorshift
            if (r >= 0)
                --spins;
        }
        else if ((rs & STARTED) == 0 || (lock = stealCounter) == null)
            Thread.yield();   // initialization race
        else if (U.compareAndSwapInt(this, RUNSTATE, rs, rs | RSIGNAL)) {
            synchronized (lock) {
                if ((runState & RSIGNAL) != 0) {
                    try {
                        lock.wait();
                    } catch (InterruptedException ie) {
                        if (!(Thread.currentThread() instanceof
                              ForkJoinWorkerThread))
                            wasInterrupted = true;
                    }
                }
                else
                    lock.notifyAll();
            }
        }
    }
}
 
Example 19
Source File: ForkJoinPool.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Spins and/or blocks until runstate lock is available.  See
 * above for explanation.
 */
private int awaitRunStateLock() {
    Object lock;
    boolean wasInterrupted = false;
    for (int spins = SPINS, r = 0, rs, ns;;) {
        if (((rs = runState) & RSLOCK) == 0) {
            if (U.compareAndSwapInt(this, RUNSTATE, rs, ns = rs | RSLOCK)) {
                if (wasInterrupted) {
                    try {
                        Thread.currentThread().interrupt();
                    } catch (SecurityException ignore) {
                    }
                }
                return ns;
            }
        }
        else if (r == 0)
            r = ThreadLocalRandom.nextSecondarySeed();
        else if (spins > 0) {
            r ^= r << 6; r ^= r >>> 21; r ^= r << 7; // xorshift
            if (r >= 0)
                --spins;
        }
        else if ((rs & STARTED) == 0 || (lock = stealCounter) == null)
            Thread.yield();   // initialization race
        else if (U.compareAndSwapInt(this, RUNSTATE, rs, rs | RSIGNAL)) {
            synchronized (lock) {
                if ((runState & RSIGNAL) != 0) {
                    try {
                        lock.wait();
                    } catch (InterruptedException ie) {
                        if (!(Thread.currentThread() instanceof
                              ForkJoinWorkerThread))
                            wasInterrupted = true;
                    }
                }
                else
                    lock.notifyAll();
            }
        }
    }
}
 
Example 20
Source File: ForkJoinPool.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
/**
 * Spins and/or blocks until runstate lock is available.  See
 * above for explanation.
 */
private int awaitRunStateLock() {
    Object lock;
    boolean wasInterrupted = false;
    for (int spins = SPINS, r = 0, rs, ns;;) {
        if (((rs = runState) & RSLOCK) == 0) {
            if (U.compareAndSwapInt(this, RUNSTATE, rs, ns = rs | RSLOCK)) {
                if (wasInterrupted) {
                    try {
                        Thread.currentThread().interrupt();
                    } catch (SecurityException ignore) {
                    }
                }
                return ns;
            }
        }
        else if (r == 0)
            r = ThreadLocalRandom.nextSecondarySeed();
        else if (spins > 0) {
            r ^= r << 6; r ^= r >>> 21; r ^= r << 7; // xorshift
            if (r >= 0)
                --spins;
        }
        else if ((rs & STARTED) == 0 || (lock = stealCounter) == null)
            Thread.yield();   // initialization race
        else if (U.compareAndSwapInt(this, RUNSTATE, rs, rs | RSIGNAL)) {
            synchronized (lock) {
                if ((runState & RSIGNAL) != 0) {
                    try {
                        lock.wait();
                    } catch (InterruptedException ie) {
                        if (!(Thread.currentThread() instanceof
                              ForkJoinWorkerThread))
                            wasInterrupted = true;
                    }
                }
                else
                    lock.notifyAll();
            }
        }
    }
}