/** * @return ExponentialBackofWaitStrategy {@see ExponentialBackofWaitStrategy} */ static <T> ExponentialBackofWaitStrategy<T> exponentialBackOff() { return new ExponentialBackofWaitStrategy<>(); }
@Override public T take(final com.oath.cyclops.async.wait.WaitStrategy.Takeable<T> t) throws InterruptedException { T result; while ((result = t.take()) == null) { } return result; }
/** * @return DirectWaitStrategy {@see DirectWaitStrategy} */ static <T> DirectWaitStrategy<T> direct() { return new DirectWaitStrategy<>(); } }
@Override public boolean offer(final WaitStrategy.Offerable o) throws InterruptedException { while (!o.offer()) { } return true; }
/** * @return NoWaitRetry strategy {@see NoWaitRetry} */ static <T> NoWaitRetry<T> noWaitRetry() { return new NoWaitRetry<>(); }
/** * @return SpinWait strategy {@see SpinWait} */ static <T> SpinWait<T> spinWait() { return new SpinWait<>(); }
/** * @return YieldWait strategy {@see YieldWait} */ static <T> YieldWait<T> yieldWait() { return new YieldWait<>(); }
@Override public T take(final com.oath.cyclops.async.wait.WaitStrategy.Takeable<T> t) throws InterruptedException { return t.take(); }
@Override public boolean offer(final com.oath.cyclops.async.wait.WaitStrategy.Offerable o) throws InterruptedException { return o.offer(); }
/** * Creates an async.Queue backed by a JDK Wait Free unbounded ConcurrentLinkedQueue * Wait strategy used is NoWaitRetry by default for both Consumers and Producers * (both Consumers and Producers will repeatedly retry until successful). Use * withConsumerWaitStrategy & withProducerWaitStrategy methods on the returned queue to change the * wait strategy * <pre> * {@code * queue.withConsumerWaitStrategy(new DirectWaitStrategy()) * .withProducerWaitStrategy(new YieldWait()); * }</pre> * * * @return Factory for unbounded wait free queue backed by ConcurrentLinkedQueue */ public static <T> QueueFactory<T> unboundedNonBlockingQueue() { return () -> new Queue<T>( new ConcurrentLinkedQueue<>(), new NoWaitRetry<>(), new NoWaitRetry<>()); }
/** * Queue accepts a BlockingQueue to make use of Blocking semantics * * * @param queue * BlockingQueue to back this Queue */ public Queue(final BlockingQueue<T> queue) { this(queue, new DirectWaitStrategy<T>(), new DirectWaitStrategy<T>()); }
@Override public T take(final WaitStrategy.Takeable<T> t) throws InterruptedException { double currentBackoff = backoffNanos; T result; while ((result = t.take()) == null) { LockSupport.parkNanos((long) currentBackoff); currentBackoff = currentBackoff * coefficient; } return result; }
@Override public boolean offer(final WaitStrategy.Offerable o) throws InterruptedException { double currentBackoff = backoffNanos; while (!o.offer()) { LockSupport.parkNanos((long) currentBackoff); currentBackoff = currentBackoff * coefficient; } return true; }
/** * Creates an async.Queue backed by an Agrona ManyToOneConcurrentArrayQueue bounded by specified queueSize * Wait strategy used is NoWaitRetry by default for both Consumers and Producers * (both Consumers and Producers will repeatedly retry until successful). Use * withConsumerWaitStrategy & withProducerWaitStrategy methods on the returned queue to change the * wait strategy * <pre> * {@code * queue.withConsumerWaitStrategy(new DirectWaitStrategy()) * .withProducerWaitStrategy(new YieldWait()); * }</pre> * * @param queueSize upper bound for Queue * @return bounded wait free Queue Factory backed by an Agrona ManyToOneConcurrentArrayQueue */ public static <T> QueueFactory<T> boundedNonBlockingQueue(final int queueSize) { return () -> new Queue<T>( new ManyToOneConcurrentArrayQueue<>( queueSize), new NoWaitRetry<>(), new NoWaitRetry<>()); }
Queue(final BlockingQueue<T> queue, final Signal<Integer> sizeSignal) { this(queue, new DirectWaitStrategy<T>(), new DirectWaitStrategy<T>()); }
@Override public T take(final WaitStrategy.Takeable<T> t) throws InterruptedException { T result; while ((result = t.take()) == null) { Thread.yield(); } return result; }
@Override public boolean offer(final WaitStrategy.Offerable o) throws InterruptedException { while (!o.offer()) { Thread.yield(); } return true; }
/** * Creates an async.Queue backed by an Agrona OneToOneConcurrentArrayQueue bounded by specified queueSize * Wait strategy used is NoWaitRetry by default for both Consumers and Producers * (both Consumers and Producers will repeatedly retry until successful). Use * withConsumerWaitStrategy & withProducerWaitStrategy methods on the returned queue to change the * wait strategy * <pre> * {@code * queue.withConsumerWaitStrategy(new DirectWaitStrategy()) * .withProducerWaitStrategy(new YieldWait()); * }</pre> * * @param queueSize * @return */ public static <T> QueueFactory<T> singleWriterboundedNonBlockingQueue(final int queueSize) { return () -> new Queue<T>( new OneToOneConcurrentArrayQueue<>( queueSize), new NoWaitRetry<>(), new NoWaitRetry<>()); } /**
@Override public T take(final WaitStrategy.Takeable<T> t) throws InterruptedException { T result; while ((result = t.take()) == null) { LockSupport.parkNanos(1l); } return result; }
@Override public boolean offer(final WaitStrategy.Offerable o) throws InterruptedException { while (!o.offer()) { LockSupport.parkNanos(1l); } return true; }