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