Repository navigation
Expand file tree
/
Copy pathQueueDrainSubscriber.java
More file actions
251 lines (219 loc) · 7.65 KB
/
Copy pathQueueDrainSubscriber.java
File metadata and controls
251 lines (219 loc) · 7.65 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
/**
* Copyright 2016 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 io.reactivex.internal.subscribers.flowable;
import java.util.concurrent.atomic.*;
import org.reactivestreams.Subscriber;
import io.reactivex.disposables.Disposable;
import io.reactivex.internal.fuseable.SimpleQueue;
import io.reactivex.internal.subscriptions.SubscriptionHelper;
import io.reactivex.internal.util.*;
/**
* Abstract base class for subscribers that hold another subscriber, a queue
* and requires queue-drain behavior.
*
* @param <T> the source type to which this subscriber will be subscribed
* @param <U> the value type in the queue
* @param <V> the value type the child subscriber accepts
*/
public abstract class QueueDrainSubscriber<T, U, V> extends QueueDrainSubscriberPad4 implements Subscriber<T>, QueueDrain<U, V> {
protected final Subscriber<? super V> actual;
protected final SimpleQueue<U> queue;
protected volatile boolean cancelled;
protected volatile boolean done;
protected Throwable error;
public QueueDrainSubscriber(Subscriber<? super V> actual, SimpleQueue<U> queue) {
this.actual = actual;
this.queue = queue;
}
@Override
public final boolean cancelled() {
return cancelled;
}
@Override
public final boolean done() {
return done;
}
@Override
public final boolean enter() {
return wip.getAndIncrement() == 0;
}
public final boolean fastEnter() {
return wip.get() == 0 && wip.compareAndSet(0, 1);
}
protected final void fastPathEmit(U value, boolean delayError) {
final Subscriber<? super V> s = actual;
final SimpleQueue<U> q = queue;
if (wip.get() == 0 && wip.compareAndSet(0, 1)) {
long r = requested.get();
if (r != 0L) {
if (accept(s, value)) {
if (r != Long.MAX_VALUE) {
produced(1);
}
}
if (leave(-1) == 0) {
return;
}
}
q.offer(value);
} else {
q.offer(value);
if (!enter()) {
return;
}
}
QueueDrainHelper.drainLoop(q, s, delayError, this);
}
protected final void fastPathEmitMax(U value, boolean delayError, Disposable dispose) {
final Subscriber<? super V> s = actual;
final SimpleQueue<U> q = queue;
if (wip.get() == 0 && wip.compareAndSet(0, 1)) {
long r = requested.get();
if (r != 0L) {
if (accept(s, value)) {
if (r != Long.MAX_VALUE) {
produced(1);
}
}
if (leave(-1) == 0) {
return;
}
} else {
dispose.dispose();
s.onError(new IllegalStateException("Could not emit buffer due to lack of requests"));
return;
}
} else {
q.offer(value);
if (!enter()) {
return;
}
}
QueueDrainHelper.drainMaxLoop(q, s, delayError, dispose, this);
}
protected final void fastPathOrderedEmitMax(U value, boolean delayError, Disposable dispose) {
final Subscriber<? super V> s = actual;
final SimpleQueue<U> q = queue;
if (wip.get() == 0 && wip.compareAndSet(0, 1)) {
long r = requested.get();
if (r != 0L) {
if (q.isEmpty()) {
if (accept(s, value)) {
if (r != Long.MAX_VALUE) {
produced(1);
}
}
if (leave(-1) == 0) {
return;
}
} else {
q.offer(value);
}
} else {
cancelled = true;
dispose.dispose();
s.onError(new IllegalStateException("Could not emit buffer due to lack of requests"));
return;
}
} else {
q.offer(value);
if (!enter()) {
return;
}
}
QueueDrainHelper.drainMaxLoop(q, s, delayError, dispose, this);
}
/**
* Makes sure the fast-path emits in order.
* @param value the value to emit or queue up
* @param delayError if true, errors are delayed until the source has terminated
*/
protected final void fastPathOrderedEmit(U value, boolean delayError) {
final Subscriber<? super V> s = actual;
final SimpleQueue<U> q = queue;
if (wip.get() == 0 && wip.compareAndSet(0, 1)) {
if (q.isEmpty()) {
long r = requested.get();
if (r != 0L) {
if (accept(s, value)) {
if (r != Long.MAX_VALUE) {
produced(1);
}
}
if (leave(-1) == 0) {
return;
}
}
}
q.offer(value);
} else {
q.offer(value);
if (!enter()) {
return;
}
}
QueueDrainHelper.drainLoop(q, s, delayError, this);
}
@Override
public final Throwable error() {
return error;
}
@Override
public final int leave(int m) {
return wip.addAndGet(m);
}
@Override
public final long requested() {
return requested.get();
}
@Override
public final long produced(long n) {
return requested.addAndGet(-n);
}
public final void requested(long n) {
if (SubscriptionHelper.validate(n)) {
BackpressureHelper.add(requested, n);
}
}
public void drain(boolean delayError) {
if (enter()) {
QueueDrainHelper.drainLoop(queue, actual, delayError, this);
}
}
}
// -------------------------------------------------------------------
// Padding superclasses
//-------------------------------------------------------------------
/** Pads the header away from other fields. */
class QueueDrainSubscriberPad0 {
volatile long p1, p2, p3, p4, p5, p6, p7;
volatile long p8, p9, p10, p11, p12, p13, p14, p15;
}
/** The WIP counter. */
class QueueDrainSubscriberWip extends QueueDrainSubscriberPad0 {
final AtomicInteger wip = new AtomicInteger();
}
/** Pads away the wip from the other fields. */
class QueueDrainSubscriberPad2 extends QueueDrainSubscriberWip {
volatile long p1a, p2a, p3a, p4a, p5a, p6a, p7a;
volatile long p8a, p9a, p10a, p11a, p12a, p13a, p14a, p15a;
}
/** Contains the requested field. */
class QueueDrainSubscriberPad3 extends QueueDrainSubscriberPad2 {
final AtomicLong requested = new AtomicLong();
}
/** Pads away the requested from the other fields. */
class QueueDrainSubscriberPad4 extends QueueDrainSubscriberPad3 {
volatile long q1, q2, q3, q4, q5, q6, q7;
volatile long q8, q9, q10, q11, q12, q13, q14, q15;
}