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