Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/parallel/ParallelReduceFull.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.parallel;
15
16 import java.util.Objects;
17 import java.util.concurrent.atomic.*;
18
19 import org.reactivestreams.*;
20
21 import io.reactivex.rxjava3.core.*;
22 import io.reactivex.rxjava3.exceptions.Exceptions;
23 import io.reactivex.rxjava3.functions.BiFunction;
24 import io.reactivex.rxjava3.internal.subscriptions.*;
25 import io.reactivex.rxjava3.internal.util.AtomicThrowable;
26 import io.reactivex.rxjava3.parallel.ParallelFlowable;
27 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
28
29 /**
30 * Reduces all 'rails' into a single value which then gets reduced into a single
31 * Publisher sequence.
32 *
33 * @param <T> the value type
34 */
35 public final class ParallelReduceFull<T> extends Flowable<T> {
36
37 final ParallelFlowable<? extends T> source;
38
39 final BiFunction<T, T, T> reducer;
40
41 public ParallelReduceFull(ParallelFlowable<? extends T> source, BiFunction<T, T, T> reducer) {
42 this.source = source;
43 this.reducer = reducer;
44 }
45
46 @Override
47 protected void subscribeActual(Subscriber<? super T> s) {
48 ParallelReduceFullMainSubscriber<T> parent = new ParallelReduceFullMainSubscriber<>(s, source.parallelism(), reducer);
49 s.onSubscribe(parent);
50
51 source.subscribe(parent.subscribers);
52 }
53
54 static final class ParallelReduceFullMainSubscriber<T> extends DeferredScalarSubscription<T> {
55
56 private static final long serialVersionUID = -5370107872170712765L;
57
58 final ParallelReduceFullInnerSubscriber<T>[] subscribers;
59
60 final BiFunction<T, T, T> reducer;
61
62 final AtomicReference<SlotPair<T>> current = new AtomicReference<>();
63
64 final AtomicInteger remaining = new AtomicInteger();
65
66 final AtomicThrowable error = new AtomicThrowable();
67
68 ParallelReduceFullMainSubscriber(Subscriber<? super T> subscriber, int n, BiFunction<T, T, T> reducer) {
69 super(subscriber);
70 @SuppressWarnings("unchecked")
71 ParallelReduceFullInnerSubscriber<T>[] a = new ParallelReduceFullInnerSubscriber[n];
72 for (int i = 0; i < n; i++) {
73 a[i] = new ParallelReduceFullInnerSubscriber<>(this, reducer);
74 }
75 this.subscribers = a;
76 this.reducer = reducer;
77 remaining.lazySet(n);
78 }
79
80 SlotPair<T> addValue(T value) {
81 for (;;) {
82 SlotPair<T> curr = current.get();
83
84 if (curr == null) {
85 curr = new SlotPair<>();
86 if (!current.compareAndSet(null, curr)) {
87 continue;
88 }
89 }
90
91 int c = curr.tryAcquireSlot();
92 if (c < 0) {
93 current.compareAndSet(curr, null);
94 continue;
95 }
96 if (c == 0) {
97 curr.first = value;
98 } else {
99 curr.second = value;
100 }
101
102 if (curr.releaseSlot()) {
103 current.compareAndSet(curr, null);
104 return curr;
105 }
106 return null;
107 }
108 }
109
110 @Override
111 public void cancel() {
112 for (ParallelReduceFullInnerSubscriber<T> inner : subscribers) {
113 inner.cancel();
114 }
115 }
116
117 void innerError(Throwable ex) {
118 if (error.compareAndSet(null, ex)) {
119 cancel();
120 downstream.onError(ex);
121 } else {
122 if (ex != error.get()) {
123 RxJavaPlugins.onError(ex);
124 }
125 }
126 }
127
128 void innerComplete(T value) {
129 if (value != null) {
130 for (;;) {
131 SlotPair<T> sp = addValue(value);
132
133 if (sp != null) {
134
135 try {
136 value = Objects.requireNonNull(reducer.apply(sp.first, sp.second), "The reducer returned a null value");
137 } catch (Throwable ex) {
138 Exceptions.throwIfFatal(ex);
139 innerError(ex);
140 return;
141 }
142
143 } else {
144 break;
145 }
146 }
147 }
148
149 if (remaining.decrementAndGet() == 0) {
150 SlotPair<T> sp = current.get();
151 current.lazySet(null);
152
153 if (sp != null) {
154 complete(sp.first);
155 } else {
156 downstream.onComplete();
157 }
158 }
159 }
160 }
161
162 static final class ParallelReduceFullInnerSubscriber<T>
163 extends AtomicReference<Subscription>
164 implements FlowableSubscriber<T> {
165
166 private static final long serialVersionUID = -7954444275102466525L;
167
168 final ParallelReduceFullMainSubscriber<T> parent;
169
170 final BiFunction<T, T, T> reducer;
171
172 T value;
173
174 boolean done;
175
176 ParallelReduceFullInnerSubscriber(ParallelReduceFullMainSubscriber<T> parent, BiFunction<T, T, T> reducer) {
177 this.parent = parent;
178 this.reducer = reducer;
179 }
180
181 @Override
182 public void onSubscribe(Subscription s) {
183 SubscriptionHelper.setOnce(this, s, Long.MAX_VALUE);
184 }
185
186 @Override
187 public void onNext(T t) {
188 if (!done) {
189 T v = value;
190
191 if (v == null) {
192 value = t;
193 } else {
194
195 try {
196 v = Objects.requireNonNull(reducer.apply(v, t), "The reducer returned a null value");
197 } catch (Throwable ex) {
198 Exceptions.throwIfFatal(ex);
199 get().cancel();
200 onError(ex);
201 return;
202 }
203
204 value = v;
205 }
206 }
207 }
208
209 @Override
210 public void onError(Throwable t) {
211 if (done) {
212 RxJavaPlugins.onError(t);
213 return;
214 }
215 done = true;
216 parent.innerError(t);
217 }
218
219 @Override
220 public void onComplete() {
221 if (!done) {
222 done = true;
223 parent.innerComplete(value);
224 }
225 }
226
227 void cancel() {
228 SubscriptionHelper.cancel(this);
229 }
230 }
231
232 static final class SlotPair<T> extends AtomicInteger {
233
234 private static final long serialVersionUID = 473971317683868662L;
235
236 T first;
237
238 T second;
239
240 final AtomicInteger releaseIndex = new AtomicInteger();
241
242 int tryAcquireSlot() {
243 for (;;) {
244 int acquired = get();
245 if (acquired >= 2) {
246 return -1;
247 }
248
249 if (compareAndSet(acquired, acquired + 1)) {
250 return acquired;
251 }
252 }
253 }
254
255 boolean releaseSlot() {
256 return releaseIndex.incrementAndGet() == 2;
257 }
258 }
259 }