Search in sources :

Example 1 with SimpleConditionSynchronizer

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

the class Val method get.

/**
     * Returns the delayed value, blocking until it has been set, but no longer than the given timeout.
     *
     * @param timeout The maximum duration to block waiting for the value to be set.
     * @param unit    The time unit of the timeout value.
     * @return the value
     * @throws TimeoutException     if the timeout expires before the value is set.
     * @throws InterruptedException
     */
@Override
@Suspendable
public V get(long timeout, TimeUnit unit) throws InterruptedException, TimeoutException {
    try {
        final SimpleConditionSynchronizer s = sync;
        if (s != null) {
            Object token = s.register();
            try {
                final long start = System.nanoTime();
                long left = unit.toNanos(timeout);
                final long deadline = start + left;
                for (int i = 0; sync != null; i++) {
                    s.awaitNanos(i, left);
                    if (sync == null)
                        break;
                    left = deadline - System.nanoTime();
                    if (left <= 0)
                        throw new TimeoutException();
                }
            } finally {
                s.unregister(token);
            }
        }
        if (t != null)
            throw t instanceof CancellationException ? (CancellationException) t : new RuntimeExecutionException(t);
        return value;
    } catch (SuspendExecution e) {
        throw new AssertionError(e);
    }
}
Also used : SimpleConditionSynchronizer(co.paralleluniverse.strands.SimpleConditionSynchronizer) SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) CancellationException(java.util.concurrent.CancellationException) TimeoutException(java.util.concurrent.TimeoutException) RuntimeExecutionException(co.paralleluniverse.fibers.RuntimeExecutionException) Suspendable(co.paralleluniverse.fibers.Suspendable)

Example 2 with SimpleConditionSynchronizer

use of co.paralleluniverse.strands.SimpleConditionSynchronizer 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 SimpleConditionSynchronizer

use of co.paralleluniverse.strands.SimpleConditionSynchronizer 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 SimpleConditionSynchronizer

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

the class Val method set0.

private void set0(V value) {
    final SimpleConditionSynchronizer s = sync;
    if (s == null)
        throw new IllegalStateException("Value has already been set (and can only be set once)");
    this.value = value;
    // must be done before signal
    sync = null;
    this.f = null;
    s.signalAll();
}
Also used : SimpleConditionSynchronizer(co.paralleluniverse.strands.SimpleConditionSynchronizer)

Example 5 with SimpleConditionSynchronizer

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

the class Val method setException0.

private void setException0(Throwable t) {
    final SimpleConditionSynchronizer s = sync;
    if (s == null)
        throw new IllegalStateException("Value has already been set (and can only be set once)");
    this.t = t;
    // must be done before signal
    sync = null;
    this.f = null;
    s.signalAll();
}
Also used : SimpleConditionSynchronizer(co.paralleluniverse.strands.SimpleConditionSynchronizer)

Aggregations

SimpleConditionSynchronizer (co.paralleluniverse.strands.SimpleConditionSynchronizer)5 Condition (co.paralleluniverse.strands.Condition)2 SuspendableRunnable (co.paralleluniverse.strands.SuspendableRunnable)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Test (org.junit.Test)2 RuntimeExecutionException (co.paralleluniverse.fibers.RuntimeExecutionException)1 SuspendExecution (co.paralleluniverse.fibers.SuspendExecution)1 Suspendable (co.paralleluniverse.fibers.Suspendable)1 CancellationException (java.util.concurrent.CancellationException)1 TimeoutException (java.util.concurrent.TimeoutException)1