Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableWindowBoundary.java

raw
1 /*
2 * Copyright (c) 2016-present, RxJava Contributors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 * the License for the specific language governing permissions and limitations under the License.
12 */
13
14 package io.reactivex.rxjava3.internal.operators.flowable;
15
16 import java.util.concurrent.atomic.*;
17
18 import org.reactivestreams.*;
19
20 import io.reactivex.rxjava3.core.*;
21 import io.reactivex.rxjava3.exceptions.MissingBackpressureException;
22 import io.reactivex.rxjava3.internal.queue.MpscLinkedQueue;
23 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
24 import io.reactivex.rxjava3.internal.util.*;
25 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
26 import io.reactivex.rxjava3.processors.UnicastProcessor;
27 import io.reactivex.rxjava3.subscribers.DisposableSubscriber;
28
29 public final class FlowableWindowBoundary<T, B> extends AbstractFlowableWithUpstream<T, Flowable<T>> {
30 final Publisher<B> other;
31 final int capacityHint;
32
33 public FlowableWindowBoundary(Flowable<T> source, Publisher<B> other, int capacityHint) {
34 super(source);
35 this.other = other;
36 this.capacityHint = capacityHint;
37 }
38
39 @Override
40 protected void subscribeActual(Subscriber<? super Flowable<T>> subscriber) {
41 WindowBoundaryMainSubscriber<T, B> parent = new WindowBoundaryMainSubscriber<>(subscriber, capacityHint);
42
43 subscriber.onSubscribe(parent);
44
45 parent.innerNext();
46
47 other.subscribe(parent.boundarySubscriber);
48
49 source.subscribe(parent);
50 }
51
52 static final class WindowBoundaryMainSubscriber<T, B>
53 extends AtomicInteger
54 implements FlowableSubscriber<T>, Subscription, Runnable {
55
56 private static final long serialVersionUID = 2233020065421370272L;
57
58 final Subscriber<? super Flowable<T>> downstream;
59
60 final int capacityHint;
61
62 final WindowBoundaryInnerSubscriber<T, B> boundarySubscriber;
63
64 final AtomicReference<Subscription> upstream;
65
66 final AtomicInteger windows;
67
68 final MpscLinkedQueue<Object> queue;
69
70 final AtomicThrowable errors;
71
72 final AtomicBoolean stopWindows;
73
74 final AtomicLong requested;
75
76 static final Object NEXT_WINDOW = new Object();
77
78 volatile boolean done;
79
80 UnicastProcessor<T> window;
81
82 long emitted;
83
84 WindowBoundaryMainSubscriber(Subscriber<? super Flowable<T>> downstream, int capacityHint) {
85 this.downstream = downstream;
86 this.capacityHint = capacityHint;
87 this.boundarySubscriber = new WindowBoundaryInnerSubscriber<>(this);
88 this.upstream = new AtomicReference<>();
89 this.windows = new AtomicInteger(1);
90 this.queue = new MpscLinkedQueue<>();
91 this.errors = new AtomicThrowable();
92 this.stopWindows = new AtomicBoolean();
93 this.requested = new AtomicLong();
94 }
95
96 @Override
97 public void onSubscribe(Subscription s) {
98 SubscriptionHelper.setOnce(upstream, s, Long.MAX_VALUE);
99 }
100
101 @Override
102 public void onNext(T t) {
103 queue.offer(t);
104 drain();
105 }
106
107 @Override
108 public void onError(Throwable e) {
109 boundarySubscriber.dispose();
110 if (errors.tryAddThrowableOrReport(e)) {
111 done = true;
112 drain();
113 }
114 }
115
116 @Override
117 public void onComplete() {
118 boundarySubscriber.dispose();
119 done = true;
120 drain();
121 }
122
123 @Override
124 public void cancel() {
125 if (stopWindows.compareAndSet(false, true)) {
126 boundarySubscriber.dispose();
127 if (windows.decrementAndGet() == 0) {
128 SubscriptionHelper.cancel(upstream);
129 }
130 }
131 }
132
133 @Override
134 public void request(long n) {
135 BackpressureHelper.add(requested, n);
136 }
137
138 @Override
139 public void run() {
140 if (windows.decrementAndGet() == 0) {
141 SubscriptionHelper.cancel(upstream);
142 }
143 }
144
145 void innerNext() {
146 queue.offer(NEXT_WINDOW);
147 drain();
148 }
149
150 void innerError(Throwable e) {
151 SubscriptionHelper.cancel(upstream);
152 if (errors.tryAddThrowableOrReport(e)) {
153 done = true;
154 drain();
155 }
156 }
157
158 void innerComplete() {
159 SubscriptionHelper.cancel(upstream);
160 done = true;
161 drain();
162 }
163
164 @SuppressWarnings("unchecked")
165 void drain() {
166 if (getAndIncrement() != 0) {
167 return;
168 }
169
170 int missed = 1;
171 Subscriber<? super Flowable<T>> downstream = this.downstream;
172 MpscLinkedQueue<Object> queue = this.queue;
173 AtomicThrowable errors = this.errors;
174 long emitted = this.emitted;
175
176 for (;;) {
177
178 for (;;) {
179 if (windows.get() == 0) {
180 queue.clear();
181 window = null;
182 return;
183 }
184
185 UnicastProcessor<T> w = window;
186
187 boolean d = done;
188
189 if (d && errors.get() != null) {
190 queue.clear();
191 Throwable ex = errors.terminate();
192 if (w != null) {
193 window = null;
194 w.onError(ex);
195 }
196 downstream.onError(ex);
197 return;
198 }
199
200 Object v = queue.poll();
201
202 boolean empty = v == null;
203
204 if (d && empty) {
205 Throwable ex = errors.terminate();
206 if (ex == null) {
207 if (w != null) {
208 window = null;
209 w.onComplete();
210 }
211 downstream.onComplete();
212 } else {
213 if (w != null) {
214 window = null;
215 w.onError(ex);
216 }
217 downstream.onError(ex);
218 }
219 return;
220 }
221
222 if (empty) {
223 break;
224 }
225
226 if (v != NEXT_WINDOW) {
227 w.onNext((T)v);
228 continue;
229 }
230
231 if (w != null) {
232 window = null;
233 w.onComplete();
234 }
235
236 if (!stopWindows.get()) {
237 w = UnicastProcessor.create(capacityHint, this);
238 window = w;
239 windows.getAndIncrement();
240
241 if (emitted != requested.get()) {
242 emitted++;
243 FlowableWindowSubscribeIntercept<T> intercept = new FlowableWindowSubscribeIntercept<>(w);
244 downstream.onNext(intercept);
245 if (intercept.tryAbandon()) {
246 w.onComplete();
247 }
248 } else {
249 SubscriptionHelper.cancel(upstream);
250 boundarySubscriber.dispose();
251 errors.tryAddThrowableOrReport(new MissingBackpressureException("Could not deliver a window due to lack of requests"));
252 done = true;
253 }
254 }
255 }
256
257 this.emitted = emitted;
258 missed = addAndGet(-missed);
259 if (missed == 0) {
260 break;
261 }
262 }
263 }
264 }
265
266 static final class WindowBoundaryInnerSubscriber<T, B> extends DisposableSubscriber<B> {
267
268 final WindowBoundaryMainSubscriber<T, B> parent;
269
270 boolean done;
271
272 WindowBoundaryInnerSubscriber(WindowBoundaryMainSubscriber<T, B> parent) {
273 this.parent = parent;
274 }
275
276 @Override
277 public void onNext(B t) {
278 if (done) {
279 return;
280 }
281 parent.innerNext();
282 }
283
284 @Override
285 public void onError(Throwable t) {
286 if (done) {
287 RxJavaPlugins.onError(t);
288 return;
289 }
290 done = true;
291 parent.innerError(t);
292 }
293
294 @Override
295 public void onComplete() {
296 if (done) {
297 return;
298 }
299 done = true;
300 parent.innerComplete();
301 }
302 }
303 }