Search in sources :

Example 1 with Condition

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

the class TickerChannelConsumer method attemptReceive.

void attemptReceive(long timeout, TimeUnit unit) throws SuspendExecution, InterruptedException, TimeoutException, EOFException {
    checkClosed();
    final Condition sync = channel.sync;
    long left = unit.toNanos(timeout);
    final long deadline = System.nanoTime() + left;
    Object token = sync.register();
    try {
        for (int i = 0; !consumer.hasNext(); i++) {
            if (channel.isSendClosed()) {
                setReceiveClosed();
                checkClosed();
            }
            sync.await(i, left, TimeUnit.NANOSECONDS);
            left = deadline - System.nanoTime();
            if (left <= 0)
                throw new TimeoutException();
        }
        consumer.poll0();
    } finally {
        sync.unregister(token);
    }
}
Also used : Condition(co.paralleluniverse.strands.Condition) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with Condition

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

the class FiberTest method testDumpStackWaitingFiber.

@Test
public void testDumpStackWaitingFiber() throws Exception {
    final Condition cond = new SimpleConditionSynchronizer(null);
    final AtomicBoolean flag = new AtomicBoolean(false);
    Fiber fiber = new Fiber(scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            foo();
        }

        private void foo() throws InterruptedException, SuspendExecution {
            Object token = cond.register();
            try {
                for (int i = 0; !flag.get(); i++) cond.await(i);
            } finally {
                cond.unregister(token);
            }
        }
    }).start();
    Thread.sleep(200);
    StackTraceElement[] st = fiber.getStackTrace();
    // Strand.printStackTrace(st, System.err);
    assertThat(st[0].getMethodName(), equalTo("park"));
    boolean found = false;
    for (StackTraceElement ste : st) {
        if (ste.getMethodName().equals("foo")) {
            found = true;
            break;
        }
    }
    assertThat(found, is(true));
    assertThat(st[st.length - 1].getMethodName(), equalTo("run"));
    assertThat(st[st.length - 1].getClassName(), equalTo(Fiber.class.getName()));
    flag.set(true);
    cond.signalAll();
    fiber.join();
}
Also used : Condition(co.paralleluniverse.strands.Condition) SimpleConditionSynchronizer(co.paralleluniverse.strands.SimpleConditionSynchronizer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Test(org.junit.Test)

Example 3 with Condition

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

the class FiberTest method testDumpStackWaitingFiberWhenCalledFromFiber.

@Test
public void testDumpStackWaitingFiberWhenCalledFromFiber() throws Exception {
    final Condition cond = new SimpleConditionSynchronizer(null);
    final AtomicBoolean flag = new AtomicBoolean(false);
    final Fiber fiber = new Fiber(scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            foo();
        }

        private void foo() throws InterruptedException, SuspendExecution {
            Object token = cond.register();
            try {
                for (int i = 0; !flag.get(); i++) cond.await(i);
            } finally {
                cond.unregister(token);
            }
        }
    }).start();
    Thread.sleep(200);
    Fiber fiber2 = new Fiber(scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            StackTraceElement[] st = fiber.getStackTrace();
            // Strand.printStackTrace(st, System.err);
            assertThat(st[0].getMethodName(), equalTo("park"));
            boolean found = false;
            for (StackTraceElement ste : st) {
                if (ste.getMethodName().equals("foo")) {
                    found = true;
                    break;
                }
            }
            assertThat(found, is(true));
            assertThat(st[st.length - 1].getMethodName(), equalTo("run"));
            assertThat(st[st.length - 1].getClassName(), equalTo(Fiber.class.getName()));
        }
    }).start();
    fiber2.join();
    flag.set(true);
    cond.signalAll();
    fiber.join();
}
Also used : Condition(co.paralleluniverse.strands.Condition) SimpleConditionSynchronizer(co.paralleluniverse.strands.SimpleConditionSynchronizer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Test(org.junit.Test)

Example 4 with Condition

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

the class TickerChannelConsumer method attemptReceive.

void attemptReceive() throws EOFException, SuspendExecution, InterruptedException {
    checkClosed();
    final Condition sync = channel.sync;
    Object token = sync.register();
    try {
        for (int i = 0; !consumer.hasNext(); i++) {
            if (channel.isSendClosed()) {
                setReceiveClosed();
                checkClosed();
            }
            sync.await(i);
        }
        consumer.poll0();
    } finally {
        sync.unregister(token);
    }
}
Also used : Condition(co.paralleluniverse.strands.Condition)

Example 5 with Condition

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

the class ValChannel method register.

@Override
public Object register(SelectAction<V> action1) {
    SelectActionImpl<V> action = (SelectActionImpl<V>) action1;
    if (action.isData())
        throw new UnsupportedOperationException("Send is not supported by DelayedValChanel");
    Condition sync = dv.getSync();
    if (sync == null) {
        if (!action.lease())
            return null;
        action.setItem(dv.getValue());
        action.won();
        return null;
    }
    sync.register();
    return action;
}
Also used : Condition(co.paralleluniverse.strands.Condition) SelectActionImpl(co.paralleluniverse.strands.channels.SelectActionImpl)

Aggregations

Condition (co.paralleluniverse.strands.Condition)5 SimpleConditionSynchronizer (co.paralleluniverse.strands.SimpleConditionSynchronizer)2 SuspendableRunnable (co.paralleluniverse.strands.SuspendableRunnable)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Test (org.junit.Test)2 SelectActionImpl (co.paralleluniverse.strands.channels.SelectActionImpl)1 TimeoutException (java.util.concurrent.TimeoutException)1