| 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.concurrent.atomic.*; |
| 17 |
|
| 18 |
import org.reactivestreams.*; |
| 19 |
|
| 20 |
import io.reactivex.rxjava3.core.FlowableSubscriber; |
| 21 |
import io.reactivex.rxjava3.exceptions.*; |
| 22 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper; |
| 23 |
import io.reactivex.rxjava3.internal.util.BackpressureHelper; |
| 24 |
import io.reactivex.rxjava3.operators.QueueSubscription; |
| 25 |
import io.reactivex.rxjava3.operators.SimpleQueue; |
| 26 |
import io.reactivex.rxjava3.operators.SpscArrayQueue; |
| 27 |
import io.reactivex.rxjava3.parallel.ParallelFlowable; |
| 28 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 29 |
|
| 30 |
/** |
| 31 |
* Dispatches the values from upstream in a round robin fashion to subscribers which are |
| 32 |
* ready to consume elements. A value from upstream is sent to only one of the subscribers. |
| 33 |
* |
| 34 |
* @param <T> the value type |
| 35 |
*/ |
| 36 |
public final class ParallelFromPublisher<T> extends ParallelFlowable<T> { |
| 37 |
final Publisher<? extends T> source; |
| 38 |
|
| 39 |
final int parallelism; |
| 40 |
|
| 41 |
final int prefetch; |
| 42 |
|
| 43 |
public ParallelFromPublisher(Publisher<? extends T> source, int parallelism, int prefetch) { |
| 44 |
this.source = source; |
| 45 |
this.parallelism = parallelism; |
| 46 |
this.prefetch = prefetch; |
| 47 |
} |
| 48 |
|
| 49 |
@Override |
| 50 |
public int parallelism() { |
| 51 |
return parallelism; |
| 52 |
} |
| 53 |
|
| 54 |
@Override |
| 55 |
public void subscribe(Subscriber<? super T>[] subscribers) { |
| 56 |
subscribers = RxJavaPlugins.onSubscribe(this, subscribers); |
| 57 |
|
| 58 |
if (!validate(subscribers)) { |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
source.subscribe(new ParallelDispatcher<>(subscribers, prefetch)); |
| 63 |
} |
| 64 |
|
| 65 |
static final class ParallelDispatcher<T> |
| 66 |
extends AtomicInteger |
| 67 |
implements FlowableSubscriber<T> { |
| 68 |
|
| 69 |
private static final long serialVersionUID = -4470634016609963609L; |
| 70 |
|
| 71 |
final Subscriber<? super T>[] subscribers; |
| 72 |
|
| 73 |
final AtomicLongArray requests; |
| 74 |
|
| 75 |
final long[] emissions; |
| 76 |
|
| 77 |
final int prefetch; |
| 78 |
|
| 79 |
final int limit; |
| 80 |
|
| 81 |
Subscription upstream; |
| 82 |
|
| 83 |
SimpleQueue<T> queue; |
| 84 |
|
| 85 |
Throwable error; |
| 86 |
|
| 87 |
volatile boolean done; |
| 88 |
|
| 89 |
int index; |
| 90 |
|
| 91 |
volatile boolean cancelled; |
| 92 |
|
| 93 |
/** |
| 94 |
* Counts how many subscribers were setup to delay triggering the |
| 95 |
* drain of upstream until all of them have been setup. |
| 96 |
*/ |
| 97 |
final AtomicInteger subscriberCount = new AtomicInteger(); |
| 98 |
|
| 99 |
int produced; |
| 100 |
|
| 101 |
int sourceMode; |
| 102 |
|
| 103 |
ParallelDispatcher(Subscriber<? super T>[] subscribers, int prefetch) { |
| 104 |
this.subscribers = subscribers; |
| 105 |
this.prefetch = prefetch; |
| 106 |
this.limit = prefetch - (prefetch >> 2); |
| 107 |
int m = subscribers.length; |
| 108 |
this.requests = new AtomicLongArray(m + m + 1); |
| 109 |
this.requests.lazySet(m + m, m); |
| 110 |
this.emissions = new long[m]; |
| 111 |
} |
| 112 |
|
| 113 |
@Override |
| 114 |
public void onSubscribe(Subscription s) { |
| 115 |
if (SubscriptionHelper.validate(this.upstream, s)) { |
| 116 |
this.upstream = s; |
| 117 |
|
| 118 |
if (s instanceof QueueSubscription) { |
| 119 |
@SuppressWarnings("unchecked") |
| 120 |
QueueSubscription<T> qs = (QueueSubscription<T>) s; |
| 121 |
|
| 122 |
int m = qs.requestFusion(QueueSubscription.ANY | QueueSubscription.BOUNDARY); |
| 123 |
|
| 124 |
if (m == QueueSubscription.SYNC) { |
| 125 |
sourceMode = m; |
| 126 |
queue = qs; |
| 127 |
done = true; |
| 128 |
setupSubscribers(); |
| 129 |
drain(); |
| 130 |
return; |
| 131 |
} else |
| 132 |
if (m == QueueSubscription.ASYNC) { |
| 133 |
sourceMode = m; |
| 134 |
queue = qs; |
| 135 |
|
| 136 |
setupSubscribers(); |
| 137 |
|
| 138 |
s.request(prefetch); |
| 139 |
|
| 140 |
return; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
queue = new SpscArrayQueue<>(prefetch); |
| 145 |
|
| 146 |
setupSubscribers(); |
| 147 |
|
| 148 |
s.request(prefetch); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
void setupSubscribers() { |
| 153 |
Subscriber<? super T>[] subs = subscribers; |
| 154 |
final int m = subs.length; |
| 155 |
|
| 156 |
for (int i = 0; i < m; i++) { |
| 157 |
subscriberCount.lazySet(i + 1); |
| 158 |
|
| 159 |
subs[i].onSubscribe(new RailSubscription(i, m)); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
final class RailSubscription implements Subscription { |
| 164 |
|
| 165 |
final int j; |
| 166 |
|
| 167 |
final int m; |
| 168 |
|
| 169 |
RailSubscription(int j, int m) { |
| 170 |
this.j = j; |
| 171 |
this.m = m; |
| 172 |
} |
| 173 |
|
| 174 |
@Override |
| 175 |
public void request(long n) { |
| 176 |
if (SubscriptionHelper.validate(n)) { |
| 177 |
AtomicLongArray ra = requests; |
| 178 |
for (;;) { |
| 179 |
long r = ra.get(j); |
| 180 |
if (r == Long.MAX_VALUE) { |
| 181 |
return; |
| 182 |
} |
| 183 |
long u = BackpressureHelper.addCap(r, n); |
| 184 |
if (ra.compareAndSet(j, r, u)) { |
| 185 |
break; |
| 186 |
} |
| 187 |
} |
| 188 |
if (subscriberCount.get() == m) { |
| 189 |
drain(); |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
@Override |
| 195 |
public void cancel() { |
| 196 |
if (requests.compareAndSet(m + j, 0L, 1L)) { |
| 197 |
ParallelDispatcher.this.cancel(m + m); |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
@Override |
| 203 |
public void onNext(T t) { |
| 204 |
if (sourceMode == QueueSubscription.NONE) { |
| 205 |
if (!queue.offer(t)) { |
| 206 |
upstream.cancel(); |
| 207 |
onError(new MissingBackpressureException("Queue is full?")); |
| 208 |
return; |
| 209 |
} |
| 210 |
} |
| 211 |
drain(); |
| 212 |
} |
| 213 |
|
| 214 |
@Override |
| 215 |
public void onError(Throwable t) { |
| 216 |
error = t; |
| 217 |
done = true; |
| 218 |
drain(); |
| 219 |
} |
| 220 |
|
| 221 |
@Override |
| 222 |
public void onComplete() { |
| 223 |
done = true; |
| 224 |
drain(); |
| 225 |
} |
| 226 |
|
| 227 |
void cancel(int m) { |
| 228 |
if (requests.decrementAndGet(m) == 0L) { |
| 229 |
cancelled = true; |
| 230 |
this.upstream.cancel(); |
| 231 |
|
| 232 |
if (getAndIncrement() == 0) { |
| 233 |
queue.clear(); |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
void drainAsync() { |
| 239 |
int missed = 1; |
| 240 |
|
| 241 |
SimpleQueue<T> q = queue; |
| 242 |
Subscriber<? super T>[] a = this.subscribers; |
| 243 |
AtomicLongArray r = this.requests; |
| 244 |
long[] e = this.emissions; |
| 245 |
int n = e.length; |
| 246 |
int idx = index; |
| 247 |
int consumed = produced; |
| 248 |
|
| 249 |
for (;;) { |
| 250 |
|
| 251 |
int notReady = 0; |
| 252 |
|
| 253 |
for (;;) { |
| 254 |
if (cancelled) { |
| 255 |
q.clear(); |
| 256 |
return; |
| 257 |
} |
| 258 |
|
| 259 |
boolean d = done; |
| 260 |
if (d) { |
| 261 |
Throwable ex = error; |
| 262 |
if (ex != null) { |
| 263 |
q.clear(); |
| 264 |
for (Subscriber<? super T> s : a) { |
| 265 |
s.onError(ex); |
| 266 |
} |
| 267 |
return; |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
boolean empty = q.isEmpty(); |
| 272 |
|
| 273 |
if (d && empty) { |
| 274 |
for (Subscriber<? super T> s : a) { |
| 275 |
s.onComplete(); |
| 276 |
} |
| 277 |
return; |
| 278 |
} |
| 279 |
|
| 280 |
if (empty) { |
| 281 |
break; |
| 282 |
} |
| 283 |
|
| 284 |
long requestAtIndex = r.get(idx); |
| 285 |
long emissionAtIndex = e[idx]; |
| 286 |
if (requestAtIndex != emissionAtIndex && r.get(n + idx) == 0) { |
| 287 |
|
| 288 |
T v; |
| 289 |
|
| 290 |
try { |
| 291 |
v = q.poll(); |
| 292 |
} catch (Throwable ex) { |
| 293 |
Exceptions.throwIfFatal(ex); |
| 294 |
upstream.cancel(); |
| 295 |
for (Subscriber<? super T> s : a) { |
| 296 |
s.onError(ex); |
| 297 |
} |
| 298 |
return; |
| 299 |
} |
| 300 |
|
| 301 |
if (v == null) { |
| 302 |
break; |
| 303 |
} |
| 304 |
|
| 305 |
a[idx].onNext(v); |
| 306 |
|
| 307 |
e[idx] = emissionAtIndex + 1; |
| 308 |
|
| 309 |
int c = ++consumed; |
| 310 |
if (c == limit) { |
| 311 |
consumed = 0; |
| 312 |
upstream.request(c); |
| 313 |
} |
| 314 |
notReady = 0; |
| 315 |
} else { |
| 316 |
notReady++; |
| 317 |
} |
| 318 |
|
| 319 |
idx++; |
| 320 |
if (idx == n) { |
| 321 |
idx = 0; |
| 322 |
} |
| 323 |
|
| 324 |
if (notReady == n) { |
| 325 |
break; |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
int w = get(); |
| 330 |
if (w == missed) { |
| 331 |
index = idx; |
| 332 |
produced = consumed; |
| 333 |
missed = addAndGet(-missed); |
| 334 |
if (missed == 0) { |
| 335 |
break; |
| 336 |
} |
| 337 |
} else { |
| 338 |
missed = w; |
| 339 |
} |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
void drainSync() { |
| 344 |
int missed = 1; |
| 345 |
|
| 346 |
SimpleQueue<T> q = queue; |
| 347 |
Subscriber<? super T>[] a = this.subscribers; |
| 348 |
AtomicLongArray r = this.requests; |
| 349 |
long[] e = this.emissions; |
| 350 |
int n = e.length; |
| 351 |
int idx = index; |
| 352 |
|
| 353 |
for (;;) { |
| 354 |
|
| 355 |
int notReady = 0; |
| 356 |
|
| 357 |
for (;;) { |
| 358 |
if (cancelled) { |
| 359 |
q.clear(); |
| 360 |
return; |
| 361 |
} |
| 362 |
|
| 363 |
boolean empty = q.isEmpty(); |
| 364 |
|
| 365 |
if (empty) { |
| 366 |
for (Subscriber<? super T> s : a) { |
| 367 |
s.onComplete(); |
| 368 |
} |
| 369 |
return; |
| 370 |
} |
| 371 |
|
| 372 |
long requestAtIndex = r.get(idx); |
| 373 |
long emissionAtIndex = e[idx]; |
| 374 |
if (requestAtIndex != emissionAtIndex && r.get(n + idx) == 0) { |
| 375 |
|
| 376 |
T v; |
| 377 |
|
| 378 |
try { |
| 379 |
v = q.poll(); |
| 380 |
} catch (Throwable ex) { |
| 381 |
Exceptions.throwIfFatal(ex); |
| 382 |
upstream.cancel(); |
| 383 |
for (Subscriber<? super T> s : a) { |
| 384 |
s.onError(ex); |
| 385 |
} |
| 386 |
return; |
| 387 |
} |
| 388 |
|
| 389 |
if (v == null) { |
| 390 |
for (Subscriber<? super T> s : a) { |
| 391 |
s.onComplete(); |
| 392 |
} |
| 393 |
return; |
| 394 |
} |
| 395 |
|
| 396 |
a[idx].onNext(v); |
| 397 |
|
| 398 |
e[idx] = emissionAtIndex + 1; |
| 399 |
|
| 400 |
notReady = 0; |
| 401 |
} else { |
| 402 |
notReady++; |
| 403 |
} |
| 404 |
|
| 405 |
idx++; |
| 406 |
if (idx == n) { |
| 407 |
idx = 0; |
| 408 |
} |
| 409 |
|
| 410 |
if (notReady == n) { |
| 411 |
break; |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
int w = get(); |
| 416 |
if (w == missed) { |
| 417 |
index = idx; |
| 418 |
missed = addAndGet(-missed); |
| 419 |
if (missed == 0) { |
| 420 |
break; |
| 421 |
} |
| 422 |
} else { |
| 423 |
missed = w; |
| 424 |
} |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
void drain() { |
| 429 |
if (getAndIncrement() != 0) { |
| 430 |
return; |
| 431 |
} |
| 432 |
|
| 433 |
if (sourceMode == QueueSubscription.SYNC) { |
| 434 |
drainSync(); |
| 435 |
} else { |
| 436 |
drainAsync(); |
| 437 |
} |
| 438 |
} |
| 439 |
} |
| 440 |
} |