| 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.maybe; |
| 15 |
|
| 16 |
import java.util.Objects; |
| 17 |
import java.util.concurrent.ConcurrentLinkedQueue; |
| 18 |
import java.util.concurrent.atomic.*; |
| 19 |
|
| 20 |
import org.reactivestreams.Subscriber; |
| 21 |
|
| 22 |
import io.reactivex.rxjava3.annotations.Nullable; |
| 23 |
import io.reactivex.rxjava3.core.*; |
| 24 |
import io.reactivex.rxjava3.disposables.*; |
| 25 |
import io.reactivex.rxjava3.internal.subscriptions.*; |
| 26 |
import io.reactivex.rxjava3.internal.util.*; |
| 27 |
import io.reactivex.rxjava3.operators.SimpleQueue; |
| 28 |
|
| 29 |
/** |
| 30 |
* Run all MaybeSources of an array at once and signal their values as they become available. |
| 31 |
* |
| 32 |
* @param <T> the value type |
| 33 |
*/ |
| 34 |
public final class MaybeMergeArray<T> extends Flowable<T> { |
| 35 |
|
| 36 |
final MaybeSource<? extends T>[] sources; |
| 37 |
|
| 38 |
public MaybeMergeArray(MaybeSource<? extends T>[] sources) { |
| 39 |
this.sources = sources; |
| 40 |
} |
| 41 |
|
| 42 |
@Override |
| 43 |
protected void subscribeActual(Subscriber<? super T> s) { |
| 44 |
MaybeSource<? extends T>[] maybes = sources; |
| 45 |
int n = maybes.length; |
| 46 |
|
| 47 |
SimpleQueueWithConsumerIndex<Object> queue; |
| 48 |
|
| 49 |
if (n <= bufferSize()) { |
| 50 |
queue = new MpscFillOnceSimpleQueue<>(n); |
| 51 |
} else { |
| 52 |
queue = new ClqSimpleQueue<>(); |
| 53 |
} |
| 54 |
MergeMaybeObserver<T> parent = new MergeMaybeObserver<>(s, n, queue); |
| 55 |
|
| 56 |
s.onSubscribe(parent); |
| 57 |
|
| 58 |
AtomicThrowable e = parent.errors; |
| 59 |
|
| 60 |
for (MaybeSource<? extends T> source : maybes) { |
| 61 |
if (parent.isCancelled() || e.get() != null) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
source.subscribe(parent); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
static final class MergeMaybeObserver<T> |
| 70 |
extends BasicIntQueueSubscription<T> implements MaybeObserver<T> { |
| 71 |
|
| 72 |
private static final long serialVersionUID = -660395290758764731L; |
| 73 |
|
| 74 |
final Subscriber<? super T> downstream; |
| 75 |
|
| 76 |
final CompositeDisposable set; |
| 77 |
|
| 78 |
final AtomicLong requested; |
| 79 |
|
| 80 |
final SimpleQueueWithConsumerIndex<Object> queue; |
| 81 |
|
| 82 |
final AtomicThrowable errors; |
| 83 |
|
| 84 |
final int sourceCount; |
| 85 |
|
| 86 |
volatile boolean cancelled; |
| 87 |
|
| 88 |
boolean outputFused; |
| 89 |
|
| 90 |
long consumed; |
| 91 |
|
| 92 |
MergeMaybeObserver(Subscriber<? super T> actual, int sourceCount, SimpleQueueWithConsumerIndex<Object> queue) { |
| 93 |
this.downstream = actual; |
| 94 |
this.sourceCount = sourceCount; |
| 95 |
this.set = new CompositeDisposable(); |
| 96 |
this.requested = new AtomicLong(); |
| 97 |
this.errors = new AtomicThrowable(); |
| 98 |
this.queue = queue; |
| 99 |
} |
| 100 |
|
| 101 |
@Override |
| 102 |
public int requestFusion(int mode) { |
| 103 |
if ((mode & ASYNC) != 0) { |
| 104 |
outputFused = true; |
| 105 |
return ASYNC; |
| 106 |
} |
| 107 |
return NONE; |
| 108 |
} |
| 109 |
|
| 110 |
@Nullable |
| 111 |
@SuppressWarnings("unchecked") |
| 112 |
@Override |
| 113 |
public T poll() { |
| 114 |
for (;;) { |
| 115 |
Object o = queue.poll(); |
| 116 |
if (o != NotificationLite.COMPLETE) { |
| 117 |
return (T)o; |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
@Override |
| 123 |
public boolean isEmpty() { |
| 124 |
return queue.isEmpty(); |
| 125 |
} |
| 126 |
|
| 127 |
@Override |
| 128 |
public void clear() { |
| 129 |
queue.clear(); |
| 130 |
} |
| 131 |
|
| 132 |
@Override |
| 133 |
public void request(long n) { |
| 134 |
if (SubscriptionHelper.validate(n)) { |
| 135 |
BackpressureHelper.add(requested, n); |
| 136 |
drain(); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
@Override |
| 141 |
public void cancel() { |
| 142 |
if (!cancelled) { |
| 143 |
cancelled = true; |
| 144 |
set.dispose(); |
| 145 |
if (getAndIncrement() == 0) { |
| 146 |
queue.clear(); |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
@Override |
| 152 |
public void onSubscribe(Disposable d) { |
| 153 |
set.add(d); |
| 154 |
} |
| 155 |
|
| 156 |
@Override |
| 157 |
public void onSuccess(T value) { |
| 158 |
queue.offer(value); |
| 159 |
drain(); |
| 160 |
} |
| 161 |
|
| 162 |
@Override |
| 163 |
public void onError(Throwable e) { |
| 164 |
if (errors.tryAddThrowableOrReport(e)) { |
| 165 |
set.dispose(); |
| 166 |
queue.offer(NotificationLite.COMPLETE); |
| 167 |
drain(); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
@Override |
| 172 |
public void onComplete() { |
| 173 |
queue.offer(NotificationLite.COMPLETE); |
| 174 |
drain(); |
| 175 |
} |
| 176 |
|
| 177 |
boolean isCancelled() { |
| 178 |
return cancelled; |
| 179 |
} |
| 180 |
|
| 181 |
@SuppressWarnings("unchecked") |
| 182 |
void drainNormal() { |
| 183 |
int missed = 1; |
| 184 |
Subscriber<? super T> a = downstream; |
| 185 |
SimpleQueueWithConsumerIndex<Object> q = queue; |
| 186 |
long e = consumed; |
| 187 |
|
| 188 |
for (;;) { |
| 189 |
|
| 190 |
long r = requested.get(); |
| 191 |
|
| 192 |
while (e != r) { |
| 193 |
if (cancelled) { |
| 194 |
q.clear(); |
| 195 |
return; |
| 196 |
} |
| 197 |
|
| 198 |
Throwable ex = errors.get(); |
| 199 |
if (ex != null) { |
| 200 |
q.clear(); |
| 201 |
errors.tryTerminateConsumer(downstream); |
| 202 |
return; |
| 203 |
} |
| 204 |
|
| 205 |
if (q.consumerIndex() == sourceCount) { |
| 206 |
a.onComplete(); |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
Object v = q.poll(); |
| 211 |
|
| 212 |
if (v == null) { |
| 213 |
break; |
| 214 |
} |
| 215 |
|
| 216 |
if (v != NotificationLite.COMPLETE) { |
| 217 |
a.onNext((T)v); |
| 218 |
|
| 219 |
e++; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
if (e == r) { |
| 224 |
Throwable ex = errors.get(); |
| 225 |
if (ex != null) { |
| 226 |
q.clear(); |
| 227 |
errors.tryTerminateConsumer(downstream); |
| 228 |
return; |
| 229 |
} |
| 230 |
|
| 231 |
while (q.peek() == NotificationLite.COMPLETE) { |
| 232 |
q.drop(); |
| 233 |
} |
| 234 |
|
| 235 |
if (q.consumerIndex() == sourceCount) { |
| 236 |
a.onComplete(); |
| 237 |
return; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
consumed = e; |
| 242 |
missed = addAndGet(-missed); |
| 243 |
if (missed == 0) { |
| 244 |
break; |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
} |
| 249 |
|
| 250 |
void drainFused() { |
| 251 |
int missed = 1; |
| 252 |
Subscriber<? super T> a = downstream; |
| 253 |
SimpleQueueWithConsumerIndex<Object> q = queue; |
| 254 |
|
| 255 |
for (;;) { |
| 256 |
if (cancelled) { |
| 257 |
q.clear(); |
| 258 |
return; |
| 259 |
} |
| 260 |
Throwable ex = errors.get(); |
| 261 |
if (ex != null) { |
| 262 |
q.clear(); |
| 263 |
a.onError(ex); |
| 264 |
return; |
| 265 |
} |
| 266 |
|
| 267 |
boolean d = q.producerIndex() == sourceCount; |
| 268 |
|
| 269 |
if (!q.isEmpty()) { |
| 270 |
a.onNext(null); |
| 271 |
} |
| 272 |
|
| 273 |
if (d) { |
| 274 |
a.onComplete(); |
| 275 |
return; |
| 276 |
} |
| 277 |
|
| 278 |
missed = addAndGet(-missed); |
| 279 |
if (missed == 0) { |
| 280 |
break; |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
} |
| 285 |
|
| 286 |
void drain() { |
| 287 |
if (getAndIncrement() != 0) { |
| 288 |
return; |
| 289 |
} |
| 290 |
|
| 291 |
if (outputFused) { |
| 292 |
drainFused(); |
| 293 |
} else { |
| 294 |
drainNormal(); |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
interface SimpleQueueWithConsumerIndex<T> extends SimpleQueue<T> { |
| 300 |
|
| 301 |
@Nullable |
| 302 |
@Override |
| 303 |
T poll(); |
| 304 |
|
| 305 |
T peek(); |
| 306 |
|
| 307 |
void drop(); |
| 308 |
|
| 309 |
int consumerIndex(); |
| 310 |
|
| 311 |
int producerIndex(); |
| 312 |
} |
| 313 |
|
| 314 |
static final class MpscFillOnceSimpleQueue<T> |
| 315 |
extends AtomicReferenceArray<T> |
| 316 |
implements SimpleQueueWithConsumerIndex<T> { |
| 317 |
|
| 318 |
private static final long serialVersionUID = -7969063454040569579L; |
| 319 |
final AtomicInteger producerIndex; |
| 320 |
|
| 321 |
int consumerIndex; |
| 322 |
|
| 323 |
MpscFillOnceSimpleQueue(int length) { |
| 324 |
super(length); |
| 325 |
this.producerIndex = new AtomicInteger(); |
| 326 |
} |
| 327 |
|
| 328 |
@Override |
| 329 |
public boolean offer(T value) { |
| 330 |
Objects.requireNonNull(value, "value is null"); |
| 331 |
int idx = producerIndex.getAndIncrement(); |
| 332 |
if (idx < length()) { |
| 333 |
lazySet(idx, value); |
| 334 |
return true; |
| 335 |
} |
| 336 |
return false; |
| 337 |
} |
| 338 |
|
| 339 |
@Override |
| 340 |
public boolean offer(T v1, T v2) { |
| 341 |
throw new UnsupportedOperationException(); |
| 342 |
} |
| 343 |
|
| 344 |
@Nullable |
| 345 |
@Override |
| 346 |
public T poll() { |
| 347 |
int ci = consumerIndex; |
| 348 |
if (ci == length()) { |
| 349 |
return null; |
| 350 |
} |
| 351 |
AtomicInteger pi = producerIndex; |
| 352 |
for (;;) { |
| 353 |
T v = get(ci); |
| 354 |
if (v != null) { |
| 355 |
consumerIndex = ci + 1; |
| 356 |
lazySet(ci, null); |
| 357 |
return v; |
| 358 |
} |
| 359 |
if (pi.get() == ci) { |
| 360 |
return null; |
| 361 |
} |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
@Override |
| 366 |
public T peek() { |
| 367 |
int ci = consumerIndex; |
| 368 |
if (ci == length()) { |
| 369 |
return null; |
| 370 |
} |
| 371 |
return get(ci); |
| 372 |
} |
| 373 |
|
| 374 |
@Override |
| 375 |
public void drop() { |
| 376 |
int ci = consumerIndex; |
| 377 |
lazySet(ci, null); |
| 378 |
consumerIndex = ci + 1; |
| 379 |
} |
| 380 |
|
| 381 |
@Override |
| 382 |
public boolean isEmpty() { |
| 383 |
return consumerIndex == producerIndex(); |
| 384 |
} |
| 385 |
|
| 386 |
@Override |
| 387 |
public void clear() { |
| 388 |
while (poll() != null && !isEmpty()) { } |
| 389 |
} |
| 390 |
|
| 391 |
@Override |
| 392 |
public int consumerIndex() { |
| 393 |
return consumerIndex; |
| 394 |
} |
| 395 |
|
| 396 |
@Override |
| 397 |
public int producerIndex() { |
| 398 |
return producerIndex.get(); |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
static final class ClqSimpleQueue<T> extends ConcurrentLinkedQueue<T> implements SimpleQueueWithConsumerIndex<T> { |
| 403 |
|
| 404 |
private static final long serialVersionUID = -4025173261791142821L; |
| 405 |
|
| 406 |
int consumerIndex; |
| 407 |
|
| 408 |
final AtomicInteger producerIndex; |
| 409 |
|
| 410 |
ClqSimpleQueue() { |
| 411 |
this.producerIndex = new AtomicInteger(); |
| 412 |
} |
| 413 |
|
| 414 |
@Override |
| 415 |
public boolean offer(T v1, T v2) { |
| 416 |
throw new UnsupportedOperationException(); |
| 417 |
} |
| 418 |
|
| 419 |
@Override |
| 420 |
public boolean offer(T e) { |
| 421 |
producerIndex.getAndIncrement(); |
| 422 |
return super.offer(e); |
| 423 |
} |
| 424 |
|
| 425 |
@Nullable |
| 426 |
@Override |
| 427 |
public T poll() { |
| 428 |
T v = super.poll(); |
| 429 |
if (v != null) { |
| 430 |
consumerIndex++; |
| 431 |
} |
| 432 |
return v; |
| 433 |
} |
| 434 |
|
| 435 |
@Override |
| 436 |
public int consumerIndex() { |
| 437 |
return consumerIndex; |
| 438 |
} |
| 439 |
|
| 440 |
@Override |
| 441 |
public int producerIndex() { |
| 442 |
return producerIndex.get(); |
| 443 |
} |
| 444 |
|
| 445 |
@Override |
| 446 |
public void drop() { |
| 447 |
poll(); |
| 448 |
} |
| 449 |
} |
| 450 |
} |