public void addContinuation(final Continuation c) { queue.addContinuation(c); }
public void addContinuation(Continuation cont) { distributor.subscribers.forEach(q->q.addContinuation(cont)); }
public void close() { counter.completable = true; if (queue != null && counter.active.get() == 0) { if(counter.closing.compareAndSet(false,true)) { counter.closed = true; queue.addContinuation(new Continuation( () -> { throw new ClosedQueueException(); })); queue.close(); } } }
@Override public void onComplete() { counter.active.decrementAndGet(); counter.subscription.remove(subscription); if (queue != null && counter.active.get() == 0) { if (counter.completable) { if(counter.closing.compareAndSet(false,true)) { counter.closed = true; queue.addContinuation(new Continuation( () -> { final List current = new ArrayList(); while (queue.size() > 0) { try { current.add(queue.get()); }catch(ClosedQueueException e){ break; } } throw new ClosedQueueException( current); })); queue.close(); } } } }
default <R> R foldParallel(Function<? super Stream<T>,? extends R> fn){ Queue<T> queue = QueueFactories.<T>unboundedNonBlockingQueue().build().withTimeout(1); AtomicReference<Continuation> ref = new AtomicReference<>(null); Continuation cont = new Continuation(()->{ if(ref.get()==null && ref.compareAndSet(null,Continuation.empty())){ try { //use the first consuming thread to tell this Stream onto the Queue this.spliterator().forEachRemaining(queue::offer); }finally { queue.close(); } } return Continuation.empty(); }); ; queue.addContinuation(cont); return fn.apply(queue.jdkStream().parallel()); } default <R> R foldParallel(ForkJoinPool fj,Function<? super Stream<T>,? extends R> fn){
@Override public Stream<T> unwrapStream() { if (async == Type.NO_BACKPRESSURE) { Queue<T> queue = QueueFactories.<T>unboundedNonBlockingQueue() .build(); AtomicBoolean wip = new AtomicBoolean(false); Continuation cont = new Continuation(() -> { if (wip.compareAndSet(false, true)) { this.source.subscribeAll(queue::offer, i -> { queue.close(); }, () -> queue.close()); } return Continuation.empty(); }); queue.addContinuation(cont); return queue.stream(); } return StreamSupport.stream(new OperatorToIterable<>(source, this.defaultErrorHandler, async == BACKPRESSURE).spliterator(), false); }
public ReactiveSeq<T> changes(){ com.oath.cyclops.async.adapters.Queue<T> queue = QueueFactories.<T>unboundedNonBlockingQueue() .build(); Spliterator<T> copy = copy(); Continuation[] contRef ={null}; Signal<T> signal = new Signal<T>(null, queue); AtomicBoolean wip = new AtomicBoolean(false); Continuation cont = new Continuation(()->{ if(wip.compareAndSet(false,true)) { if(!copy.tryAdvance(signal::set)){ signal.close(); return Continuation.empty(); } wip.set(false); } return contRef[0]; }); contRef[0]= cont; queue.addContinuation(cont); return signal.getDiscrete().stream(); }
default Topic<T> broadcast(){ Queue<T> queue = QueueFactories.<T>unboundedNonBlockingQueue() .build() .withTimeout(1); Topic<T> topic = new Topic<T>(queue,QueueFactories.<T>unboundedNonBlockingQueue()); AtomicBoolean wip = new AtomicBoolean(false); Spliterator<T> split = this.spliterator(); Continuation ref[] = {null}; Continuation cont = new Continuation(()->{ if(wip.compareAndSet(false,true)){ try { //use the first consuming thread to tell this Stream onto the Queue if(!split.tryAdvance(topic::offer)){ topic.close(); return Continuation.empty(); } }finally { wip.set(false); } } return ref[0]; }); ref[0]=cont; queue.addContinuation(cont); return topic; }
@Override public Iterator<T> iterator() { if (async == Type.NO_BACKPRESSURE) { Queue<T> queue = QueueFactories.<T>unboundedNonBlockingQueue() .build(); AtomicBoolean wip = new AtomicBoolean(false); Subscription[] sub = {null}; Continuation cont = new Continuation(() -> { if (wip.compareAndSet(false, true)) { this.source.subscribeAll(queue::offer, i -> queue.close(), () -> queue.close()); } return Continuation.empty(); }); queue.addContinuation(cont); return queue.stream().iterator(); } return new OperatorToIterable<>(source, this.defaultErrorHandler, async == BACKPRESSURE).iterator(); }
queue.addContinuation(cont); return topic; queue.addContinuation(cont); return topic;
public void addContinuation(final Continuation c) { queue.addContinuation(c); }
queue.addContinuation(cont);
public void addContinuation(final Continuation c) { queue.addContinuation(c); }
queue.addContinuation(cont); return queue.stream();
queue.addContinuation(cont); return queue.stream();
public void addContinuation(Continuation cont) { distributor.subscribers.forEach(q->q.addContinuation(cont)); }
public void close() { counter.completable = true; if (queue != null && counter.active.get() == 0) { if(counter.closing.compareAndSet(false,true)) { counter.closed = true; queue.addContinuation(new Continuation( () -> { throw new ClosedQueueException(); })); queue.close(); } } }
public void close() { counter.completable = true; if (queue != null && counter.active.get() == 0) { if(counter.closing.compareAndSet(false,true)) { counter.closed = true; queue.addContinuation(new Continuation( () -> { throw new ClosedQueueException(); })); queue.close(); } } }
@Override default Queue<U> toQueue(final Function<Queue, Queue> fn) { final Queue<U> queue = fn.apply(getQueueFactory().build()); final Continuation continuation = thenSync(queue::add).self(s -> { if (this.getPopulator().isPoolingActive()) s.peekSync(v -> { throw new CompletedException( v); }); }).runContinuation(() -> {queue.close();}); queue.addContinuation(continuation); return queue; }
@Override default void addToQueue(final Queue queue) { final Continuation continuation = thenSync(queue::add).self(s -> { if (this.getPopulator() .isPoolingActive()) s.peekSync(v -> { throw new CompletedException( v); }); }).runContinuation(() -> {throw new ClosedQueueException(); } ); queue.addContinuation(continuation); }