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);
}
}
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();
}
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();
}
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();
}
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();
}
Aggregations