Search in sources :

Example 96 with Fiber

use of co.paralleluniverse.fibers.Fiber in project quasar by puniverse.

the class Pipeline method parallelTransfer.

private void parallelTransfer() throws SuspendExecution, InterruptedException {
    // 1) Fire workers
    for (int i = 0; i < parallelism; i++) {
        strandFactory.newStrand(SuspendableUtils.runnableToCallable(new SuspendableRunnable() {

            @Override
            public void run() throws SuspendExecution, InterruptedException {
                // Get first job
                Pair<S, Channel<Channel<T>>> job = jobs.receive();
                while (job != null) {
                    // Build result channel
                    final Channel<T> res = resultChannelBuilder.run();
                    // Process
                    transformer.call(job.getFirst(), res);
                    final Channel<Channel<T>> resWrapper = job.getSecond();
                    // Send result asynchronously
                    strandFactory.newStrand(SuspendableUtils.runnableToCallable(new SuspendableRunnable() {

                        @Override
                        public void run() throws SuspendExecution, InterruptedException {
                            resWrapper.send(res);
                        }
                    })).start();
                    // Get next job
                    job = jobs.receive();
                }
                // No more jobs, close results channel and quit worker
                results.close();
            }
        })).start();
    }
    // 2) Send jobs asynchronously
    strandFactory.newStrand(SuspendableUtils.runnableToCallable(new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            // Get first input
            S s = from.receive();
            while (s != null) {
                final Channel<Channel<T>> resultWrapper = Channels.newChannel(1, Channels.OverflowPolicy.BLOCK, true, true);
                jobs.send(new Pair<>(s, resultWrapper));
                results.send(resultWrapper);
                // Get next input
                s = from.receive();
            }
            // No more inputs, close jobs channel and quit
            jobs.close();
        }
    })).start();
    // 3) Collect and transfer results asynchronously
    try {
        final Strand collector = strandFactory.newStrand(SuspendableUtils.runnableToCallable(new SuspendableRunnable() {

            @Override
            public void run() throws SuspendExecution, InterruptedException {
                // Get first result
                Channel<Channel<T>> resWrapper = results.receive();
                while (resWrapper != null) {
                    // Get wrapper
                    Channel<T> res = resWrapper.receive();
                    // Get first actual result
                    T out = res.receive();
                    while (out != null) {
                        // Send to output channel
                        to.send(out);
                        // Increment counter
                        transferred.incrementAndGet();
                        // Get next result
                        out = res.receive();
                    }
                    resWrapper = results.receive();
                }
            // No more results, quit
            }
        })).start();
        // TODO solve nasty instrumentation problems on Strand.join()
        if (collector.isFiber()) {
            Fiber f = (Fiber) collector.getUnderlying();
            f.join();
        } else
            collector.join();
    } catch (ExecutionException ee) {
        throw new AssertionError(ee);
    }
}
Also used : SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Channel(co.paralleluniverse.strands.channels.Channel) Fiber(co.paralleluniverse.fibers.Fiber) Strand(co.paralleluniverse.strands.Strand) ExecutionException(java.util.concurrent.ExecutionException) Pair(co.paralleluniverse.common.util.Pair)

Example 97 with Fiber

use of co.paralleluniverse.fibers.Fiber in project quasar by puniverse.

the class StrandFactoryBuilder method build.

/**
 * Creates and returns the {@link StrandFactory} that will create new strands based on this builder's settings.
 *
 * @return a new {@link StrandFactory}.
 */
public StrandFactory build() {
    if (fiber == null)
        throw new IllegalStateException("setFiber or setThread must be called before calling build");
    final boolean _fiber = fiber;
    final boolean _daemon = daemon;
    final FiberScheduler _fs = fs;
    final String _nameFormat = nameFormat;
    final Strand.UncaughtExceptionHandler _ueh = ueh;
    final int _stackSize = stackSize != null ? stackSize : 0;
    final Integer _priority = priority;
    final AtomicLong _count = (nameFormat != null) ? new AtomicLong(0) : null;
    return new StrandFactory() {

        @Override
        public Strand newStrand(SuspendableCallable<?> target) {
            final String name = _nameFormat != null ? String.format(_nameFormat, _count.getAndIncrement()) : null;
            final Strand s;
            if (_fiber) {
                s = _fs != null ? new Fiber(name, _fs, _stackSize, target) : new Fiber(name, _stackSize, target);
            } else {
                final Thread t = new Thread(null, Strand.toRunnable(target), name != null ? name : "Thread-" + nextThreadNum(), _stackSize);
                if (name != null)
                    t.setName(name);
                t.setDaemon(_daemon);
                if (_priority != null)
                    t.setPriority(_priority);
                s = Strand.of(t);
            }
            if (_ueh != null)
                s.setUncaughtExceptionHandler(_ueh);
            return s;
        }
    };
}
Also used : Fiber(co.paralleluniverse.fibers.Fiber) AtomicLong(java.util.concurrent.atomic.AtomicLong) FiberScheduler(co.paralleluniverse.fibers.FiberScheduler)

Example 98 with Fiber

use of co.paralleluniverse.fibers.Fiber in project quasar by puniverse.

the class InitialSizeTest method testWithSize.

private void testWithSize(int stackSize) {
    Fiber c = new Fiber(null, null, stackSize, this);
    // assertEquals(getStackSize(c), stackSize);
    boolean res = TestsHelper.exec(c);
    assertEquals(res, false);
    res = TestsHelper.exec(c);
    assertEquals(res, true);
    assertTrue(getStackSize(c) > 10);
}
Also used : Fiber(co.paralleluniverse.fibers.Fiber)

Example 99 with Fiber

use of co.paralleluniverse.fibers.Fiber in project quasar by puniverse.

the class InterfaceTest method testSuspend.

@Test
public void testSuspend() {
    // final I i = new C();
    Fiber co = new Fiber((String) null, null, new SuspendableRunnable() {

        @Override
        public final void run() throws SuspendExecution {
            // next line causes an error because of incomplete merge in TypeInterpreter
            // SomeInterface i = System.currentTimeMillis() > 0 ? new C() : new C2();
            SomeInterface i = new C();
            System.out.println("i = " + i);
            i.doStuff();
        }
    });
    while (!TestsHelper.exec(co)) System.out.println("State=" + co.getState());
    System.out.println("State=" + co.getState());
}
Also used : SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 100 with Fiber

use of co.paralleluniverse.fibers.Fiber in project quasar by puniverse.

the class LeakTest method leaky.

@Test
public void leaky() throws Exception {
    Fiber co = new Fiber((String) null, null, this);
    leaked = "leaked";
    TestsHelper.exec(co);
    TestsHelper.exec(co);
    TestsHelper.exec(co);
    Field stackField = Fiber.class.getDeclaredField("stack");
    stackField.setAccessible(true);
    Field objectsField = Stack.class.getDeclaredField("dataObject");
    objectsField.setAccessible(true);
    List<Object> stack = Arrays.asList((Object[]) objectsField.get(stackField.get(co)));
    // System.out.println(stack);
    assertThat(stack, not(hasItem(leaked)));
// assertThat(stack, everyItem(nullValue()));
// WeakReference<String> ref = new WeakReference<>(leaked);
// leaked = null;
// System.gc();
// System.gc();
// assertNull(ref.get());
}
Also used : Field(java.lang.reflect.Field) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Aggregations

Fiber (co.paralleluniverse.fibers.Fiber)105 Test (org.junit.Test)88 SuspendableRunnable (co.paralleluniverse.strands.SuspendableRunnable)73 SuspendExecution (co.paralleluniverse.fibers.SuspendExecution)20 Strand (co.paralleluniverse.strands.Strand)8 IntChannel (co.paralleluniverse.strands.channels.IntChannel)4 QueueCapacityExceededException (co.paralleluniverse.strands.queues.QueueCapacityExceededException)4 ExecutionException (java.util.concurrent.ExecutionException)3 Ignore (org.junit.Ignore)3 Timeout (co.paralleluniverse.strands.Timeout)2 Channel (co.paralleluniverse.strands.channels.Channel)2 ReceivePort (co.paralleluniverse.strands.channels.ReceivePort)2 TimeoutException (java.util.concurrent.TimeoutException)2 ActorRef (co.paralleluniverse.actors.ActorRef)1 AbstractServerHandler (co.paralleluniverse.actors.behaviors.AbstractServerHandler)1 ServerActor (co.paralleluniverse.actors.behaviors.ServerActor)1 Function2 (co.paralleluniverse.common.util.Function2)1 Pair (co.paralleluniverse.common.util.Pair)1 FiberScheduler (co.paralleluniverse.fibers.FiberScheduler)1 FiberWriter (co.paralleluniverse.fibers.FiberWriter)1