Package co.paralleluniverse.strands

Examples of co.paralleluniverse.strands.Strand


        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

        strand.join(timeout, unit);
    }

    @Override
    public final V get() throws InterruptedException, ExecutionException {
        final Strand s = strand;
        if (s == null)
            throw new IllegalStateException("Actor strand not set (not started?)");
        if (s instanceof Fiber)
            return ((Fiber<V>) s).get();
        else {
            s.join();
            return actor.getResult();
        }
    }
View Full Code Here

        if (old != null && actor.getMonitor() == null && isLocal(old) && LocalActorUtil.getMonitor(old) != null)
            actor.setMonitor(LocalActorUtil.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) ? LocalActorUtil.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

    @SuppressWarnings("LeakingThisInConstructor")
    public Fiber(String name, FiberScheduler scheduler, int stackSize, SuspendableCallable<V> target) {
        this.name = name;
        this.fid = nextFiberId();
        this.scheduler = scheduler;
        Strand parent = Strand.currentStrand(); // retaining the parent as a field is a huge, complex memory leak
        this.target = target;
        this.task = scheduler != null ? scheduler.newFiberTask(this) : new FiberForkJoinScheduler.FiberForkJoinTask(this);
        this.initialStackSize = stackSize;
        this.stack = new Stack(this, stackSize > 0 ? stackSize : DEFAULT_STACK_SIZE);
        this.state = State.NEW;
View Full Code Here

    private static Fiber getCurrentFiber() {
        final Thread currentThread = Thread.currentThread();
        if (FiberForkJoinScheduler.isFiberThread(currentThread))
            return FiberForkJoinScheduler.getTargetFiber(currentThread);
        else {
            Strand s = currentStrand.get();
            return s instanceof Fiber ? (Fiber) s : null;
        }
    }
View Full Code Here

     * @param nanos timeout in nanosecs, used only if timed is true
     * @return matched item, or e if unmatched on interrupt or timeout
     */
    private Message awaitMatch(Node s, Node pred, Message e, boolean timed, long nanos) throws SuspendExecution {
        long lastTime = timed ? System.nanoTime() : 0L;
        Strand w = Strand.currentStrand();
        int spins = (w instanceof Fiber ? 0 : -1); // no spins in fiber; otherwise, initialized after first item and cancel checks
        ThreadLocalRandom randomYields = null; // bound if needed
       
        if(spins == 0)
            s.waiter = w;
       
        for (;;) {
            Object item = s.item;

            if (item == CHANNEL_CLOSED)
                setReceiveClosed();

            if (item != e) {                  // matched
                // assert item != s;
                s.forgetContents();           // avoid garbage
                return this.<Message>cast(item);
            }
            if ((w.isInterrupted() || (timed && nanos <= 0))
                    && s.casItem(e, s)) {        // cancel
                unsplice(pred, s);
                return e;
            }

View Full Code Here

        final Val<Integer> val3 = new Val<>();
        final Val<Integer> val4 = new Val<>();

        final AtomicReference<Integer> res = new AtomicReference<>();

        final Strand f1 = new Fiber<Integer>(new SuspendableRunnable() {
            @Override
            public void run() throws InterruptedException, SuspendExecution {
                val2.set(val1.get() + 1); // 2
            }
        }).start();

        final Strand t1 = Strand.of(new Thread(Strand.toRunnable(new SuspendableRunnable() {
            @Override
            public void run() throws SuspendExecution {
                try {
                    val3.set(val1.get() + val2.get()); // 3
                } catch (InterruptedException ex) {
                    throw new RuntimeException(ex);
                }
            }
        }))).start();

        final Strand f2 = new Fiber<Integer>(new SuspendableRunnable() {
            @Override
            public void run() throws InterruptedException, SuspendExecution {
                val4.set(val2.get() + val3.get()); // 5
            }
        }).start();


        Thread.sleep(100);

        val1.set(1);

        int myRes = val4.get();
        assertThat(myRes, is(5));
       
        t1.join();
        f1.join();
        f2.join();
    }
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.