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