@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();
@Override public void setSoloEffect(final SoloEffect effect) throws SuspendExecution, InterruptedException { soloEffect.set(effect); changedCh.send(ping); } }
@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();
Object receiveNode(long timeout, TimeUnit unit) throws SuspendExecution, InterruptedException { if (timeout <= 0 || unit == null) return receiveNode(); maybeSetCurrentStrandAsOwner(); Object n; final long start = System.nanoTime(); long left = unit.toNanos(timeout); sync.lock(); try { while ((n = queue.pk()) == null) { sync.await(left, TimeUnit.NANOSECONDS); left = start + unit.toNanos(timeout) - System.nanoTime(); if (left <= 0) return null; } } finally { sync.unlock(); } return n; }
@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();
v.ch.close(t); } finally { Var.record("run", "Fiber %s for var %s terminated", this, var);
@Override public void maybeSetCurrentStrandAsOwner() { super.maybeSetCurrentStrandAsOwner(); }
public Message receive() throws SuspendExecution, InterruptedException { final Object n = receiveNode(); final Message m = queue.value(n); queue.deq(n); return m; }
@Override public void send(Message message) { if (!queue.enq(message)) throw new QueueCapacityExceededException(); signal(); }
protected void maybeSetCurrentStrandAsOwner() { if (owner == null) setStrand(Strand.currentStrand()); else sync.verifyOwner(); }
Object receiveNode() throws SuspendExecution, InterruptedException { maybeSetCurrentStrandAsOwner(); Object n; sync.lock(); while ((n = queue.pk()) == null) sync.await(); sync.unlock(); return n; }
public Message receive(long timeout, TimeUnit unit) throws SuspendExecution, InterruptedException { final Object n = receiveNode(timeout, unit); if (n == null) return null; // timeout final Message m = queue.value(n); queue.deq(n); return m; }
/** * Sets a new value for this {@code Var}. The Var can be set multiple times and by multiple strands. * * @param val the new value. */ public void set(T val) { try { record("set", "Set %s to %s", this, val); ch.send(val == null ? (T) NULL : val); notifyRegistered(); } catch (Throwable e) { throw new AssertionError(e); } }
@Suspendable public T receive() { try { return channel.receive(); } catch (Exception e) { throw new VertxException(e); } }
@Override public <T extends ReceivePort<? extends M>> void add(final T... items) throws SuspendExecution, InterruptedException { if (items != null && items.length > 0) { final List<T> itemsCopy = ImmutableList.copyOf(items); // Freeze for this call states.swap(new Function<Map<? extends ReceivePort<? extends M>, State>, Map<? extends ReceivePort<? extends M>, State>>() { @Override public Map<? extends ReceivePort<? extends M>, State> apply(final Map<? extends ReceivePort<? extends M>, State> currStates) { final Map<ReceivePort<? extends M>, State> newStates = new HashMap<>(currStates); for (final ReceivePort<? extends M> port : itemsCopy) newStates.put(port, new State(Mode.NORMAL, false)); return ImmutableMap.copyOf(newStates); // RO } }); changedCh.send(ping); } }
@Suspendable public T receive(long timeout) { try { return channel.receive(timeout, TimeUnit.MILLISECONDS); } catch (Exception e) { throw new VertxException(e); } } }
@Override public <T extends ReceivePort<? extends M>> void remove(final T... items) throws SuspendExecution, InterruptedException { if (items == null || items.length == 0) states.set(ImmutableMap.<T, State>of()); // Reset else { final List<T> itemsCopy = ImmutableList.copyOf(items); // Freeze for this call states.swap(new Function<Map<? extends ReceivePort<? extends M>, State>, Map<? extends ReceivePort<? extends M>, State>>() { @Override public Map<? extends ReceivePort<? extends M>, State> apply(final Map<? extends ReceivePort<? extends M>, State> currStates) { final Map<ReceivePort<? extends M>, State> newStates = new HashMap<>(currStates); for (final ReceivePort<? extends M> port : itemsCopy) newStates.remove(port); return ImmutableMap.copyOf(newStates); // RO } }); changedCh.send(ping); } }
private void sequentialTransfer() throws InterruptedException, SuspendExecution { S s = from.receive(); while (s != null) { // Build result channel final Channel<T> res = resultChannelBuilder.run(); // Process transformer.call(s, res); T out = res.receive(); while(out != null) { // Send to output channel to.send(out); // Increment counter transferred.incrementAndGet(); // Get next result out = res.receive(); } s = from.receive(); } } }
@Override public <T extends ReceivePort<? extends M>> void setState(final State state, final T... items) throws SuspendExecution, InterruptedException { final ImmutableList<T> itemsCopy = ImmutableList.copyOf(items); states.swap(new Function<Map<? extends ReceivePort<? extends M>, State>, Map<? extends ReceivePort<? extends M>, State>>() { @Override public Map<? extends ReceivePort<? extends M>, State> apply(final Map<? extends ReceivePort<? extends M>, State> currStates) { final Map<ReceivePort<? extends M>, State> newStates = new HashMap<>(currStates); for (final ReceivePort<? extends M> port : (items != null && items.length > 0) ? itemsCopy : ImmutableList.copyOf(currStates.keySet())) if (newStates.containsKey(port)) newStates.put ( port, new State ( state.mode != null ? state.mode : currStates.get(port).mode, state.solo != null ? state.solo : currStates.get(port).solo ) ); return ImmutableMap.copyOf(newStates); // RO } }); changedCh.send(ping); }