Search in sources :

Example 11 with Strand

use of co.paralleluniverse.strands.Strand in project quasar by puniverse.

the class Phaser method abortWait.

/**
     * Variant of releaseWaiters that additionally tries to remove any
     * nodes no longer waiting for advance due to timeout or
     * interrupt. Currently, nodes are removed only if they are at
     * head of queue, which suffices to reduce memory footprint in
     * most usages.
     *
     * @return current phase on exit
     */
private int abortWait(int phase) {
    AtomicReference<QNode> head = (phase & 1) == 0 ? evenQ : oddQ;
    //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)) {
            //    System.out.println("PHASER: " + Fiber.currentFiber() + " abortWait: " + failed + '/' + total + " " + ((double)failed / total));
            return p;
        }
        if (head.compareAndSet(q, q.next) && t != null) {
            q.strand = null;
            Strand.unpark(t);
        }
    // else
    //   failed++;
    // total++;
    }
}
Also used : Strand(co.paralleluniverse.strands.Strand)

Example 12 with Strand

use of co.paralleluniverse.strands.Strand in project quasar by puniverse.

the class Phaser method releaseWaiters.

// Waiting mechanics
/**
     * Removes and signals threads from queue for phase.
     */
private void releaseWaiters(int phase) {
    // first element of queue
    QNode q;
    // its strand
    Strand t;
    AtomicReference<QNode> head = (phase & 1) == 0 ? evenQ : oddQ;
    // int failed = 0; // contention profiling
    while ((q = head.get()) != null && q.phase != (int) (root.state >>> PHASE_SHIFT)) {
        if (head.compareAndSet(q, q.next) && (t = q.strand) != null) {
            q.strand = null;
            // System.out.println("PHASER " + (System.currentTimeMillis() % 300000) + " " + (int) (state >>> PHASE_SHIFT) + " "  + Fiber.currentFiber() + " " + Thread.currentThread() + " - " + t);
            Strand.unpark(t);
        }
    // else
    //    failed++;
    // total++;
    }
// if (total > 0)
//    System.out.println("PHASER: " + Fiber.currentFiber() + " releaseWaiters: " + failed + '/' + total + " " + ((double)failed / total));
}
Also used : Strand(co.paralleluniverse.strands.Strand)

Example 13 with Strand

use of co.paralleluniverse.strands.Strand in project quasar by puniverse.

the class Selector method register.

@Override
public Object register() {
    Strand s = Strand.currentStrand();
    if (waiter != null && !waiter.equals(s))
        throw new IllegalMonitorStateException("A strand is already registered");
    this.waiter = Strand.currentStrand();
    final int n = actions.size();
    res = null;
    // register
    lastRegistered = -1;
    for (int i = 0; i < n; i++) {
        SelectActionImpl<Message> sa = actions.get(i);
        sa.token = sa.port.register((SelectActionImpl) sa);
        lastRegistered = i;
        if (sa.isDone()) {
            // seen to have failed in co.paralleluniverse.strands.channels.GeneralSelectorTest > testFans1[5] 
            assert winner == sa;
            res = sa;
            break;
        } else {
            Object w = winner;
            if (w != null & w != LEASED)
                break;
        }
    }
    return null;
}
Also used : FlightRecorderMessage(co.paralleluniverse.common.monitoring.FlightRecorderMessage) Strand(co.paralleluniverse.strands.Strand)

Example 14 with Strand

use of co.paralleluniverse.strands.Strand in project quasar by puniverse.

the class PeerTKB method spawnActor.

private <T extends Actor<Message, V>, Message, V> T spawnActor(T actor) {
    Fiber fiber = new Fiber(actor);
    fiber.setUncaughtExceptionHandler(new Strand.UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Strand s, Throwable e) {
            e.printStackTrace();
            throw Exceptions.rethrow(e);
        }
    });
    fiber.start();
    return actor;
}
Also used : Fiber(co.paralleluniverse.fibers.Fiber) Strand(co.paralleluniverse.strands.Strand)

Example 15 with Strand

use of co.paralleluniverse.strands.Strand in project quasar by puniverse.

the class TwoSidedTest method twoSidedTestWithProcessor.

@Test
public void twoSidedTestWithProcessor() throws Exception {
    // Publisher
    final Channel<Integer> publisherChannel = Channels.newChannel(random() ? 0 : 5, OverflowPolicy.BLOCK);
    final Strand publisherStrand = new Fiber<Void>(new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            for (long i = 0; i < ELEMENTS; i++) publisherChannel.send((int) (i % 1000));
            publisherChannel.close();
        }
    }).start();
    final Publisher<Integer> publisher = ReactiveStreams.toPublisher(publisherChannel);
    // Processor
    final Processor<Integer, Integer> processor = ReactiveStreams.toProcessor(5, OverflowPolicy.BLOCK, new SuspendableAction2<ReceivePort<Integer>, SendPort<Integer>>() {

        @Override
        public void call(ReceivePort<Integer> in, SendPort<Integer> out) throws SuspendExecution, InterruptedException {
            long count = 0;
            for (Integer element; ((element = in.receive()) != null); count++) {
                out.send(element * 10);
                out.send(element * 100);
                // Fiber.sleep(1); // just for fun
                assertTrue(count < ELEMENTS);
            }
            assertEquals(ELEMENTS, count);
            out.close();
        }
    });
    publisher.subscribe(processor);
    // Subscriber
    final ReceivePort<Integer> subscriberChannel = ReactiveStreams.subscribe(buffer, overflowPolicy, processor);
    final Strand subscriberStrand = new Fiber<Void>(new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            long count = 0;
            for (; ; ) {
                Integer x = subscriberChannel.receive();
                if (x == null)
                    break;
                assertTrue(x % 10 == 0);
                if (count % 2 != 0)
                    assertTrue(x % 100 == 0);
                count++;
            }
            subscriberChannel.close();
            assertEquals(ELEMENTS * 2, count);
        }
    }).start();
    subscriberStrand.join(5, TimeUnit.SECONDS);
    publisherStrand.join(5, TimeUnit.SECONDS);
}
Also used : SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) ReceivePort(co.paralleluniverse.strands.channels.ReceivePort) SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) SendPort(co.paralleluniverse.strands.channels.SendPort) Strand(co.paralleluniverse.strands.Strand)

Aggregations

Strand (co.paralleluniverse.strands.Strand)27 Fiber (co.paralleluniverse.fibers.Fiber)8 SuspendableRunnable (co.paralleluniverse.strands.SuspendableRunnable)4 SuspendExecution (co.paralleluniverse.fibers.SuspendExecution)3 Test (org.junit.Test)2 Actor (co.paralleluniverse.actors.Actor)1 ActorRef (co.paralleluniverse.actors.ActorRef)1 LocalActor (co.paralleluniverse.actors.LocalActor)1 FlightRecorderMessage (co.paralleluniverse.common.monitoring.FlightRecorderMessage)1 Pair (co.paralleluniverse.common.util.Pair)1 StrandFactoryBuilder (co.paralleluniverse.strands.StrandFactoryBuilder)1 Channel (co.paralleluniverse.strands.channels.Channel)1 ReceivePort (co.paralleluniverse.strands.channels.ReceivePort)1 SendPort (co.paralleluniverse.strands.channels.SendPort)1 ExecutionException (java.util.concurrent.ExecutionException)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Publisher (org.reactivestreams.Publisher)1