Top Description Fields Constructors Methods
io.reactivex.rxjava3.internal.util

public final Class QueueDrainHelper

extends Object
Class Inheritance
Imports
java.util.Queue, java.util.concurrent.atomic.AtomicLong, org.reactivestreams.*, io.reactivex.rxjava3.core.Observer, io.reactivex.rxjava3.disposables.Disposable, io.reactivex.rxjava3.exceptions.*, io.reactivex.rxjava3.functions.BooleanSupplier, io.reactivex.rxjava3.operators.*

Utility class to help with the queue-drain serialization idiom.

Field Summary

Modifier and TypeField and Description
pack-priv static final long
pack-priv static final long

Constructor Summary

AccessConstructor and Description
private
QueueDrainHelper()

Utility class.

Method Summary

Modifier and TypeMethod and Description
public static <T, U> boolean
checkTerminated(boolean d, boolean empty, Subscriber<?> s, boolean delayError, SimpleQueue<?> q, QueueDrain<T, U> qd)

public static <T, U> boolean
checkTerminated(boolean d, boolean empty, Observer<?> observer, boolean delayError, SimpleQueue<?> q, Disposable disposable, ObservableQueueDrain<T, U> qd)

public static <
the value type of the queue
T
>
SimpleQueue<T>

Returns:

the queue instance
createQueue
(int
the capacity hint, negative value will create an array-based SPSC queue
capacityHint
)

Creates a queue: spsc-array if capacityHint is positive and spsc-linked-array if capacityHint is negative; in both cases, the capacity is the absolute value of prefetch.

public static <T, U> void
drainLoop(SimplePlainQueue<T> q, Observer<? super U> a, boolean delayError, Disposable dispose, ObservableQueueDrain<T, U> qd)

public static <
the queue value type
T
,
the emission value type
U
>
void
drainMaxLoop(SimplePlainQueue<T>
the queue
q
,
Subscriber<? super U>
the subscriber
a
,
boolean
true if errors should be delayed after all normal items
delayError
,
Disposable
the disposable to call when termination happens and cleanup is necessary
dispose
,
QueueDrain<T, U>
the QueueDrain instance that gives status information to the drain logic
qd
)

Drain the queue but give up with an error if there aren't enough requests.

pack-priv static boolean
public static <
the value type emitted
T
>
void
postComplete(Subscriber<? super T>
the target Subscriber to send events to
actual
,
Queue<T>
the queue to drain if in the post-complete state
queue
,
AtomicLong
holds the request amount and the post-completed flag
state
,
BooleanSupplier
a supplier that returns true if the drain has been cancelled
isCancelled
)

Signals the completion of the main sequence and switches to post-completion replay mode.

pack-priv static <
the value type
T
>
boolean

Returns:

true if the queue was completely drained or the drain process was cancelled
postCompleteDrain
(long
the current request amount
n
,
Subscriber<? super T>
the target Subscriber to send events to
actual
,
Queue<T>
the queue to drain if in the post-complete state
queue
,
AtomicLong
holds the request amount and the post-completed flag
state
,
BooleanSupplier
a supplier that returns true if the drain has been cancelled
isCancelled
)

Drains the queue based on the outstanding requests in post-completed mode (only!).

public static <
the value type emitted
T
>
boolean

Returns:

true if the state indicates a completion state.
postCompleteRequest
(long
the request amount, positive (not validated)
n
,
Subscriber<? super T>
the target Subscriber to send events to
actual
,
Queue<T>
the queue to drain if in the post-complete state
queue
,
AtomicLong
holds the request amount and the post-completed flag
state
,
BooleanSupplier
a supplier that returns true if the drain has been cancelled
isCancelled
)

Accumulates requests (not validated) and handles the completed mode draining of the queue based on the requests.

public static void
request(Subscription
the Subscription to request from
s
,
int
the prefetch value
prefetch
)

Requests Long#MAX_VALUE if prefetch is negative or the exact amount if prefetch is positive.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

COMPLETED_MASKback to summary
pack-priv static final long COMPLETED_MASK
REQUESTED_MASKback to summary
pack-priv static final long REQUESTED_MASK

Constructor Detail

QueueDrainHelperback to summary
private QueueDrainHelper()

Utility class.

Method Detail

checkTerminatedback to summary
public static <T, U> boolean checkTerminated(boolean d, boolean empty, Subscriber<?> s, boolean delayError, SimpleQueue<?> q, QueueDrain<T, U> qd)
checkTerminatedback to summary
public static <T, U> boolean checkTerminated(boolean d, boolean empty, Observer<?> observer, boolean delayError, SimpleQueue<?> q, Disposable disposable, ObservableQueueDrain<T, U> qd)
createQueueback to summary
public static <T> SimpleQueue<T> createQueue(int capacityHint)

Creates a queue: spsc-array if capacityHint is positive and spsc-linked-array if capacityHint is negative; in both cases, the capacity is the absolute value of prefetch.

Parameters
<T>
the value type of the queue
capacityHint:int

the capacity hint, negative value will create an array-based SPSC queue

Returns:SimpleQueue<T>

the queue instance

drainLoopback to summary
public static <T, U> void drainLoop(SimplePlainQueue<T> q, Observer<? super U> a, boolean delayError, Disposable dispose, ObservableQueueDrain<T, U> qd)
drainMaxLoopback to summary
public static <T, U> void drainMaxLoop(SimplePlainQueue<T> q, Subscriber<? super U> a, boolean delayError, Disposable dispose, QueueDrain<T, U> qd)

Drain the queue but give up with an error if there aren't enough requests.

Parameters
<T>
the queue value type
<U>
the emission value type
q:SimplePlainQueue<T>

the queue

a:Subscriber<? super U>

the subscriber

delayError:boolean

true if errors should be delayed after all normal items

dispose:Disposable

the disposable to call when termination happens and cleanup is necessary

qd:QueueDrain<T, U>

the QueueDrain instance that gives status information to the drain logic

isCancelledback to summary
pack-priv static boolean isCancelled(BooleanSupplier cancelled)
postCompleteback to summary
public static <T> void postComplete(Subscriber<? super T> actual, Queue<T> queue, AtomicLong state, BooleanSupplier isCancelled)

Signals the completion of the main sequence and switches to post-completion replay mode.

Don't modify the queue after calling this method!

Post-completion backpressure handles the case when a source produces values based on requests when it is active but more values are available even after its completion. In this case, the onComplete() can't just emit the contents of the queue but has to coordinate with the requested amounts. This requires two distinct modes: active and completed. In active mode, requests flow through and the queue is not accessed but in completed mode, requests no-longer reach the upstream but help in draining the queue.

The algorithm utilizes the most significant bit (bit 63) of a long value (AtomicLong) since request amount only goes up to Long#MAX_VALUE (bits 0-62) and negative values aren't allowed.

Parameters
<T>
the value type emitted
actual:Subscriber<? super T>

the target Subscriber to send events to

queue:Queue<T>

the queue to drain if in the post-complete state

state:AtomicLong

holds the request amount and the post-completed flag

isCancelled:BooleanSupplier

a supplier that returns true if the drain has been cancelled

postCompleteDrainback to summary
pack-priv static <T> boolean postCompleteDrain(long n, Subscriber<? super T> actual, Queue<T> queue, AtomicLong state, BooleanSupplier isCancelled)

Drains the queue based on the outstanding requests in post-completed mode (only!).

Parameters
<T>
the value type
n:long

the current request amount

actual:Subscriber<? super T>

the target Subscriber to send events to

queue:Queue<T>

the queue to drain if in the post-complete state

state:AtomicLong

holds the request amount and the post-completed flag

isCancelled:BooleanSupplier

a supplier that returns true if the drain has been cancelled

Returns:boolean

true if the queue was completely drained or the drain process was cancelled

postCompleteRequestback to summary
public static <T> boolean postCompleteRequest(long n, Subscriber<? super T> actual, Queue<T> queue, AtomicLong state, BooleanSupplier isCancelled)

Accumulates requests (not validated) and handles the completed mode draining of the queue based on the requests.

Post-completion backpressure handles the case when a source produces values based on requests when it is active but more values are available even after its completion. In this case, the onComplete() can't just emit the contents of the queue but has to coordinate with the requested amounts. This requires two distinct modes: active and completed. In active mode, requests flow through and the queue is not accessed but in completed mode, requests no-longer reach the upstream but help in draining the queue.

Parameters
<T>
the value type emitted
n:long

the request amount, positive (not validated)

actual:Subscriber<? super T>

the target Subscriber to send events to

queue:Queue<T>

the queue to drain if in the post-complete state

state:AtomicLong

holds the request amount and the post-completed flag

isCancelled:BooleanSupplier

a supplier that returns true if the drain has been cancelled

Returns:boolean

true if the state indicates a completion state.

requestback to summary
public static void request(Subscription s, int prefetch)

Requests Long#MAX_VALUE if prefetch is negative or the exact amount if prefetch is positive.

Parameters
s:Subscription

the Subscription to request from

prefetch:int

the prefetch value