Package co.paralleluniverse.strands

Examples of co.paralleluniverse.strands.Strand


        WNode node = null, group = null, p;
        for (int spins = -1;;) {
            for (;;) {
                long s, m, ns;
                WNode h, q;
                Strand w; // anti-barging guard
                if (group == null && (h = whead) != null
                        && (q = h.next) != null && q.mode != RMODE)
                    break;
                if ((m = (s = state) & ABITS) < RFULL
                        ? U.compareAndSwapLong(this, STATE, s, ns = s + RUNIT)
                        : (m < WBIT && (ns = tryIncReaderOverflow(s)) != 0L)) {
                    if (group != null) {  // help release others
                        for (WNode r = group;;) {
                            if ((w = r.strand) != null) {
                                r.strand = null;
                                w.unpark();
                            }
                            if ((r = group.cowait) == null)
                                break;
                            U.compareAndSwapObject(group, WCOWAIT, r, r.cowait);
                        }
                    }
                    return ns;
                }
                if (m >= WBIT)
                    break;
            }
            if (spins > 0) {
                if (ThreadLocalRandom.current().nextInt() >= 0)
                    --spins;
            } else if ((p = wtail) == null) {
                WNode h = new WNode(WMODE, null);
                if (U.compareAndSwapObject(this, WHEAD, null, h))
                    wtail = h;
            } else if (spins < 0)
                spins = (p == whead) ? SPINS : 0;
            else if (node == null)
                node = new WNode(WMODE, p);
            else if (node.prev != p)
                node.prev = p;
            else if (p.mode == RMODE && p != whead) {
                WNode pp = p.prev;  // become co-waiter with group p
                if (pp != null && p == wtail
                        && U.compareAndSwapObject(p, WCOWAIT,
                        node.cowait = p.cowait, node)) {
                    node.strand = Strand.currentStrand();
                    for (long time;;) {
                        if (interruptible && Strand.interrupted())
                            return cancelWaiter(node, p, true);
                        if (deadline == 0L)
                            time = 0L;
                        else if ((time = deadline - System.nanoTime()) <= 0L)
                            return cancelWaiter(node, p, false);
                        if (node.strand == null)
                            break;
                        if (p.prev != pp || p.status == CANCELLED
                                || p == whead || p.prev != pp) {
                            node.strand = null;
                            break;
                        }
                        if (node.strand == null) // must recheck
                            break;
                        park(time);
                    }
                    group = p;
                }
                node = null; // throw away
            } else if (U.compareAndSwapObject(this, WTAIL, p, node)) {
                p.next = node;
                break;
            }
        }

        for (int spins = SPINS;;) {
            WNode np, pp, r;
            int ps;
            long m, s, ns;
            Strand w;
            while ((np = node.prev) != p && np != null)
                (p = np).next = node;
            if (whead == p) {
                for (int k = spins;;) {
                    if ((m = (s = state) & ABITS) != WBIT) {
                        if (m < RFULL
                                ? U.compareAndSwapLong(this, STATE, s, ns = s + RUNIT)
                                : (ns = tryIncReaderOverflow(s)) != 0L) {
                            whead = node;
                            node.prev = null;
                            while ((r = node.cowait) != null) {
                                if (U.compareAndSwapObject(node, WCOWAIT,
                                        r, r.cowait)
                                        && (w = r.strand) != null) {
                                    r.strand = null;
                                    w.unpark(); // release co-waiter
                                }
                            }
                            return ns;
                        }
                    } else if (ThreadLocalRandom.current().nextInt() >= 0
View Full Code Here


     * @param interrupted if already interrupted
     * @return INTERRUPTED if interrupted or Strand.interrupted, else zero
     */
    private long cancelWaiter(WNode node, WNode group, boolean interrupted) {
        if (node != null && group != null) {
            Strand w;
            node.status = CANCELLED;
            node.strand = null;
            // unsplice cancelled nodes from group
            for (WNode p = group, q; (q = p.cowait) != null;) {
                if (q.status == CANCELLED)
                    U.compareAndSwapObject(p, WNEXT, q, q.next);
                else
                    p = q;
            }
            if (group == node) {
                WNode r; // detach and wake up uncancelled co-waiters
                while ((r = node.cowait) != null) {
                    if (U.compareAndSwapObject(node, WCOWAIT, r, r.cowait)
                            && (w = r.strand) != null) {
                        r.strand = null;
                        w.unpark();
                    }
                }
                for (WNode pred = node.prev; pred != null;) { // unsplice
                    WNode succ, pp;        // find valid successor
                    while ((succ = node.next) == null
                            || succ.status == CANCELLED) {
                        WNode q = null;    // find successor the slow way
                        for (WNode t = wtail; t != null && t != node; t = t.prev)
                            if (t.status != CANCELLED)
                                q = t;     // don't link if succ cancelled
                        if (succ == q || // ensure accurate successor
                                U.compareAndSwapObject(node, WNEXT,
                                succ, succ = q)) {
                            if (succ == null && node == wtail)
                                U.compareAndSwapObject(this, WTAIL, node, pred);
                            break;
                        }
                    }
                    if (pred.next == node) // unsplice pred link
                        U.compareAndSwapObject(pred, WNEXT, node, succ);
                    if (succ != null && (w = succ.strand) != null) {
                        succ.strand = null;
                        w.unpark();       // wake up succ to observe new pred
                    }
                    if (pred.status != CANCELLED || (pp = pred.prev) == null)
                        break;
                    node.prev = pp;        // repeat if new pred wrong/cancelled
                    U.compareAndSwapObject(pp, WNEXT, pred, succ);
View Full Code Here

    /**
     * Removes and signals threads from queue for phase.
     */
    private void releaseWaiters(int phase) {
        QNode q;   // first element of queue
        Strand t;  // its strand
        AtomicReference<QNode> head = (phase & 1) == 0 ? evenQ : oddQ;
        // int total = 0;  // contention profiling
        // int failed = 0; // contention profiling
        while ((q = head.get()) != null
                && q.phase != (int) (root.state >>> PHASE_SHIFT)) {
View Full Code Here

    private int abortWait(int phase) {
        AtomicReference<QNode> head = (phase & 1) == 0 ? evenQ : oddQ;
        // int total = 0;  // contention profiling
        //int failed = 0; // contention profiling
        for (;;) {
            Strand t;
            QNode q = head.get();
            int p = (int) (root.state >>> PHASE_SHIFT);
            if (q == null || ((t = q.strand) != null && q.phase == p)) {
                // if (total > 0)
                //    System.out.println("PHASER: " + Fiber.currentFiber() + " abortWait: " + failed + '/' + total + " " + ((double)failed / total));
View Full Code Here

        return flightRecorder;
    }

    @SuppressWarnings("CallToThreadDumpStack")
    public static void exit(int code) {
        final Strand currentStrand = Strand.currentStrand();
        if (flightRecorder != null) {
            flightRecorder.record(1, "DEBUG EXIT REQUEST ON STRAND " + currentStrand + ": " + Arrays.toString(currentStrand.getStackTrace()));
            flightRecorder.stop();
        }

        if (requestShutdown.compareAndSet(false, true)) {
            System.err.println("DEBUG EXIT REQUEST ON STRAND " + currentStrand
                    + (currentStrand.isFiber() ? " (THREAD " + Thread.currentThread() + ")" : "")
                    + ": SHUTTING DOWN THE JVM.");
            Thread.dumpStack();
            if (!isUnitTest()) // Calling System.exit() in gradle unit tests breaks gradle
                System.exit(code);
            else
View Full Code Here

    @Override
    public String toString() {
        String className = getClass().getSimpleName();
        if (className.isEmpty())
            className = getClass().getName().substring(getClass().getPackage().getName().length() + 1);
        final Strand strand = runner.getStrand();
        final String strandName = (strand != null ? strand.getName() : "null"); // strand.getClass().getSimpleName() + '@' + strand.getId()
        return className + "@"
                + (getName() != null ? getName() : Integer.toHexString(System.identityHashCode(this)))
                + "[owner: " + strandName + ']';
    }
View Full Code Here

    //<editor-fold desc="Lifecycle">
    /////////// Lifecycle ///////////////////////////////////
    final V run0() throws InterruptedException, SuspendExecution {
        JMXActorsMonitor.getInstance().actorStarted(ref);
        final Strand strand = runner.getStrand(); // runner might be nulled by running actor
        if (!strand.isFiber())
            currentActor.set(this);
        try {
            if (this instanceof MigratingActor && globalId == null)
                this.globalId = MigrationService.registerMigratingActor();

            result = doRun();
            die(null);
            return result;
        } catch (ActorAbort abort) {
            throw abort;
        } catch (InterruptedException e) {
            if (this.exception != null) {
                die(exception);
                throw (RuntimeException) exception;
            }
            die(e);
            throw e;
        } catch (Throwable t) {
            if (t.getCause() instanceof InterruptedException) {
                InterruptedException ie = (InterruptedException) t.getCause();
                if (this.exception != null) {
                    die(exception);
                    throw (RuntimeException) exception;
                }
                die(ie);
                throw ie;
            }
            this.exception = t;
            die(t);
            throw t;
        } finally {
            record(1, "Actor", "die", "Actor %s is now dead of %s", this, getDeathCause());
            if (!strand.isFiber())
                currentActor.set(null);
            JMXActorsMonitor.getInstance().actorTerminated(ref, strand);
        }
    }
View Full Code Here

        if (old != null && actor.getMonitor() == null && isLocal(old) && LocalActor.getMonitor(old) != null)
            actor.setMonitor(LocalActor.getMonitor(old));
        if (actor.getMonitor() != null)
            actor.getMonitor().addRestart();

        final Strand strand;
        if (actor.getStrand() != null)
            strand = actor.getStrand();
        else
            strand = createStrandForActor(child.actor != null && isLocal(child.actor) ? LocalActor.getStrand(child.actor) : null, actor);

        try {
            strand.start();
        } catch (IllegalThreadStateException e) {
            log().info("Child {} has already been started.", actor);
        }

        return start(child, actor.ref());
View Full Code Here

            child.watch = null;
        }
    }

    private Strand createStrandForActor(Strand oldStrand, Actor actor) {
        final Strand strand;
        if (oldStrand != null)
            strand = Strand.clone(oldStrand, actor);
        else
            strand = new Fiber(actor);
        actor.setStrand(strand);
View Full Code Here

         * some other strand(s) concurrently performed setHead in
         * between some of our reads. We try this twice before
         * resorting to traversal.
         */
        Node h, s;
        Strand st;
        if (((h = head) != null && (s = h.next) != null &&
             s.prev == head && (st = s.strand) != null) ||
            ((h = head) != null && (s = h.next) != null &&
             s.prev == head && (st = s.strand) != null))
            return st;

        /*
         * Head's next field might not have been set yet, or may have
         * been unset after setHead. So we must check to see if tail
         * is actually first node. If not, we continue on, safely
         * traversing from tail back to head to find first,
         * guaranteeing termination.
         */

        Node t = tail;
        Strand firstStrand = null;
        while (t != null && t != head) {
            Strand tt = t.strand;
            if (tt != null)
                firstStrand = tt;
            t = t.prev;
        }
        return firstStrand;
View Full Code Here

TOP

Related Classes of co.paralleluniverse.strands.Strand

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.