forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBackpressureUtils.java
More file actions
416 lines (380 loc) · 17.1 KB
/
BackpressureUtils.java
File metadata and controls
416 lines (380 loc) · 17.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/**
* Copyright 2015 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package rx.internal.operators;
import java.util.Queue;
import java.util.concurrent.atomic.*;
import rx.Subscriber;
import rx.functions.Func1;
import rx.internal.util.UtilityFunctions;
/**
* Utility functions for use with backpressure.
*
*/
public final class BackpressureUtils {
/** Utility class, no instances. */
private BackpressureUtils() {
throw new IllegalStateException("No instances!");
}
/**
* Adds {@code n} to {@code requested} field and returns the value prior to
* addition once the addition is successful (uses CAS semantics). If
* overflows then sets {@code requested} field to {@code Long.MAX_VALUE}.
*
* @param <T> the type of the target object on which the field updater operates
*
* @param requested
* atomic field updater for a request count
* @param object
* contains the field updated by the updater
* @param n
* the number of requests to add to the requested count
* @return requested value just prior to successful addition
* @deprecated Android has issues with reflection-based atomics
*/
@Deprecated
public static <T> long getAndAddRequest(AtomicLongFieldUpdater<T> requested, T object, long n) {
// add n to field but check for overflow
while (true) {
long current = requested.get(object);
long next = addCap(current, n);
if (requested.compareAndSet(object, current, next)) {
return current;
}
}
}
/**
* Adds {@code n} to {@code requested} and returns the value prior to addition once the
* addition is successful (uses CAS semantics). If overflows then sets
* {@code requested} field to {@code Long.MAX_VALUE}.
*
* @param requested
* atomic long that should be updated
* @param n
* the number of requests to add to the requested count
* @return requested value just prior to successful addition
*/
public static long getAndAddRequest(AtomicLong requested, long n) {
// add n to field but check for overflow
while (true) {
long current = requested.get();
long next = addCap(current, n);
if (requested.compareAndSet(current, next)) {
return current;
}
}
}
/**
* Multiplies two positive longs and caps the result at Long.MAX_VALUE.
* @param a the first value
* @param b the second value
* @return the capped product of a and b
*/
public static long multiplyCap(long a, long b) {
long u = a * b;
if (((a | b) >>> 31) != 0) {
if (b != 0L && (u / b != a)) {
u = Long.MAX_VALUE;
}
}
return u;
}
/**
* Adds two positive longs and caps the result at Long.MAX_VALUE.
* @param a the first value
* @param b the second value
* @return the capped sum of a and b
*/
public static long addCap(long a, long b) {
long u = a + b;
if (u < 0L) {
u = Long.MAX_VALUE;
}
return u;
}
/**
* Masks the most significant bit, i.e., 0x8000_0000_0000_0000L.
*/
static final long COMPLETED_MASK = Long.MIN_VALUE;
/**
* Masks the request amount bits, i.e., 0x7FFF_FFFF_FFFF_FFFF.
*/
static final long REQUESTED_MASK = Long.MAX_VALUE;
/**
* Signals the completion of the main sequence and switches to post-completion replay mode.
*
* <p>
* Don't modify the queue after calling this method!
*
* <p>
* 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 onCompleted() 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.
* <p>
* 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.
*
* @param <T> the value type to emit
* @param requested the holder of current requested amount
* @param queue the queue holding values to be emitted after completion
* @param actual the subscriber to receive the values
*/
public static <T> void postCompleteDone(AtomicLong requested, Queue<T> queue, Subscriber<? super T> actual) {
postCompleteDone(requested, queue, actual, UtilityFunctions.<T>identity());
}
/**
* Accumulates requests (validated) and handles the completed mode draining of the queue based on the requests.
*
* <p>
* 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 onCompleted() 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.
*
* @param <T> the value type to emit
* @param requested the holder of current requested amount
* @param n the value requested;
* @param queue the queue holding values to be emitted after completion
* @param actual the subscriber to receive the values
* @return true if in the active mode and the request amount of n can be relayed to upstream, false if
* in the post-completed mode and the queue is draining.
*/
public static <T> boolean postCompleteRequest(AtomicLong requested, long n, Queue<T> queue, Subscriber<? super T> actual) {
return postCompleteRequest(requested, n, queue, actual, UtilityFunctions.<T>identity());
}
/**
* Signals the completion of the main sequence and switches to post-completion replay mode
* and allows exit transformation on the queued values.
*
* <p>
* Don't modify the queue after calling this method!
*
* <p>
* 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 onCompleted() 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.
* <p>
* 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.
*
* @param <T> the value type in the queue
* @param <R> the value type to emit
* @param requested the holder of current requested amount
* @param queue the queue holding values to be emitted after completion
* @param actual the subscriber to receive the values
* @param exitTransform the transformation to apply on the dequeued value to get the value to be emitted
*/
public static <T, R> void postCompleteDone(AtomicLong requested, Queue<T> queue, Subscriber<? super R> actual, Func1<? super T, ? extends R> exitTransform) {
for (;;) {
long r = requested.get();
// switch to completed mode only once
if ((r & COMPLETED_MASK) != 0L) {
return;
}
//
long u = r | COMPLETED_MASK;
if (requested.compareAndSet(r, u)) {
// if we successfully switched to post-complete mode and there
// are requests available start draining the queue
if (r != 0L) {
// if the switch happened when there was outstanding requests, start draining
postCompleteDrain(requested, queue, actual, exitTransform);
}
return;
}
}
}
/**
* Accumulates requests (validated) and handles the completed mode draining of the queue based on the requests
* and allows exit transformation on the queued values.
*
* <p>
* 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 onCompleted() 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.
*
* @param <T> the value type in the queue
* @param <R> the value type to emit
* @param requested the holder of current requested amount
* @param n the value requested;
* @param queue the queue holding values to be emitted after completion
* @param actual the subscriber to receive the values
* @param exitTransform the transformation to apply on the dequeued value to get the value to be emitted
* @return true if in the active mode and the request amount of n can be relayed to upstream, false if
* in the post-completed mode and the queue is draining.
*/
public static <T, R> boolean postCompleteRequest(AtomicLong requested, long n, Queue<T> queue, Subscriber<? super R> actual, Func1<? super T, ? extends R> exitTransform) {
if (n < 0L) {
throw new IllegalArgumentException("n >= 0 required but it was " + n);
}
if (n == 0) {
return (requested.get() & COMPLETED_MASK) == 0;
}
for (;;) {
long r = requested.get();
// mask of the completed flag
long c = r & COMPLETED_MASK;
// mask of the requested amount
long u = r & REQUESTED_MASK;
// add the current requested amount and the new requested amount
// cap at Long.MAX_VALUE;
long v = addCap(u, n);
// restore the completed flag
v |= c;
if (requested.compareAndSet(r, v)) {
// if there was no outstanding request before and in
// the post-completed state, start draining
if (r == COMPLETED_MASK) {
postCompleteDrain(requested, queue, actual, exitTransform);
return false;
}
// returns true for active mode and false if the completed flag was set
return c == 0L;
}
}
}
/**
* Drains the queue based on the outstanding requests in post-completed mode (only!)
* and allows exit transformation on the queued values.
*
* @param <T> the value type in the queue
* @param <R> the value type to emit
* @param requested the holder of current requested amount
* @param queue the queue holding values to be emitted after completion
* @param subscriber the subscriber to receive the values
* @param exitTransform the transformation to apply on the dequeued value to get the value to be emitted
*/
static <T, R> void postCompleteDrain(AtomicLong requested, Queue<T> queue, Subscriber<? super R> subscriber, Func1<? super T, ? extends R> exitTransform) {
long r = requested.get();
// Run on a fast-path if the downstream is unbounded
if (r == Long.MAX_VALUE) {
for (;;) {
if (subscriber.isUnsubscribed()) {
return;
}
T v = queue.poll();
if (v == null) {
subscriber.onCompleted();
return;
}
subscriber.onNext(exitTransform.call(v));
}
}
/*
* Since we are supposed to be in the post-complete state,
* requested will have its top bit set.
* To allow direct comparison, we start with an emission value which has also
* this flag set, then increment it as usual.
* Since COMPLETED_MASK is essentially Long.MIN_VALUE,
* there won't be any overflow or sign flip.
*/
long e = COMPLETED_MASK;
for (;;) {
/*
* This is an improved queue-drain algorithm with a specialization
* in which we know the queue won't change anymore (i.e., done is always true
* when looking at the classical algorithm and there is no error).
*
* Note that we don't check for cancellation or emptiness upfront for two reasons:
* 1) if e != r, the loop will do this and we quit appropriately
* 2) if e == r, then either there was no outstanding requests or we emitted the requested amount
* and the execution simply falls to the e == r check below which checks for emptiness anyway.
*/
while (e != r) {
if (subscriber.isUnsubscribed()) {
return;
}
T v = queue.poll();
if (v == null) {
subscriber.onCompleted();
return;
}
subscriber.onNext(exitTransform.call(v));
e++;
}
/*
* If the emission count reaches the requested amount the same time the queue becomes empty
* this will make sure the subscriber is completed immediately instead of on the next request.
* This is also true if there are no outstanding requests (this the while loop doesn't run)
* and the queue is empty from the start.
*/
if (e == r) {
if (subscriber.isUnsubscribed()) {
return;
}
if (queue.isEmpty()) {
subscriber.onCompleted();
return;
}
}
/*
* Fast flow: see if more requests have arrived in the meantime.
* This avoids an atomic add (~40 cycles) and resumes the emission immediately.
*/
r = requested.get();
if (r == e) {
/*
* Atomically decrement the requested amount by the emission amount.
* We can't use the full emission value because of the completed flag,
* however, due to two's complement representation, the flag on requested
* is preserved.
*/
r = requested.addAndGet(-(e & REQUESTED_MASK));
// The requested amount actually reached zero, quit
if (r == COMPLETED_MASK) {
return;
}
// reset the emission count
e = COMPLETED_MASK;
}
}
}
/**
* Atomically subtracts a value from the requested amount unless it's at Long.MAX_VALUE.
* @param requested the requested amount holder
* @param n the value to subtract from the requested amount, has to be positive (not verified)
* @return the new requested amount
* @throws IllegalStateException if n is greater than the current requested amount, which
* indicates a bug in the request accounting logic
*/
public static long produced(AtomicLong requested, long n) {
for (;;) {
long current = requested.get();
if (current == Long.MAX_VALUE) {
return Long.MAX_VALUE;
}
long next = current - n;
if (next < 0L) {
throw new IllegalStateException("More produced than requested: " + next);
}
if (requested.compareAndSet(current, next)) {
return next;
}
}
}
}