| 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.completable; |
| 15 |
|
| 16 |
import java.util.concurrent.atomic.*; |
| 17 |
|
| 18 |
import org.reactivestreams.*; |
| 19 |
|
| 20 |
import io.reactivex.rxjava3.core.*; |
| 21 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 22 |
import io.reactivex.rxjava3.exceptions.*; |
| 23 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 24 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper; |
| 25 |
import io.reactivex.rxjava3.operators.*; |
| 26 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 27 |
|
| 28 |
public final class CompletableConcat extends Completable { |
| 29 |
final Publisher<? extends CompletableSource> sources; |
| 30 |
final int prefetch; |
| 31 |
|
| 32 |
public CompletableConcat(Publisher<? extends CompletableSource> sources, int prefetch) { |
| 33 |
this.sources = sources; |
| 34 |
this.prefetch = prefetch; |
| 35 |
} |
| 36 |
|
| 37 |
@Override |
| 38 |
public void subscribeActual(CompletableObserver observer) { |
| 39 |
sources.subscribe(new CompletableConcatSubscriber(observer, prefetch)); |
| 40 |
} |
| 41 |
|
| 42 |
static final class CompletableConcatSubscriber |
| 43 |
extends AtomicInteger |
| 44 |
implements FlowableSubscriber<CompletableSource>, Disposable { |
| 45 |
private static final long serialVersionUID = 9032184911934499404L; |
| 46 |
|
| 47 |
final CompletableObserver downstream; |
| 48 |
|
| 49 |
final int prefetch; |
| 50 |
|
| 51 |
final int limit; |
| 52 |
|
| 53 |
final ConcatInnerObserver inner; |
| 54 |
|
| 55 |
final AtomicBoolean once; |
| 56 |
|
| 57 |
int sourceFused; |
| 58 |
|
| 59 |
int consumed; |
| 60 |
|
| 61 |
SimpleQueue<CompletableSource> queue; |
| 62 |
|
| 63 |
Subscription upstream; |
| 64 |
|
| 65 |
volatile boolean done; |
| 66 |
|
| 67 |
volatile boolean active; |
| 68 |
|
| 69 |
CompletableConcatSubscriber(CompletableObserver actual, int prefetch) { |
| 70 |
this.downstream = actual; |
| 71 |
this.prefetch = prefetch; |
| 72 |
this.inner = new ConcatInnerObserver(this); |
| 73 |
this.once = new AtomicBoolean(); |
| 74 |
this.limit = prefetch - (prefetch >> 2); |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public void onSubscribe(Subscription s) { |
| 79 |
if (SubscriptionHelper.validate(this.upstream, s)) { |
| 80 |
this.upstream = s; |
| 81 |
|
| 82 |
long r = prefetch == Integer.MAX_VALUE ? Long.MAX_VALUE : prefetch; |
| 83 |
|
| 84 |
if (s instanceof QueueSubscription) { |
| 85 |
@SuppressWarnings("unchecked") |
| 86 |
QueueSubscription<CompletableSource> qs = (QueueSubscription<CompletableSource>) s; |
| 87 |
|
| 88 |
int m = qs.requestFusion(QueueSubscription.ANY); |
| 89 |
|
| 90 |
if (m == QueueSubscription.SYNC) { |
| 91 |
sourceFused = m; |
| 92 |
queue = qs; |
| 93 |
done = true; |
| 94 |
downstream.onSubscribe(this); |
| 95 |
drain(); |
| 96 |
return; |
| 97 |
} |
| 98 |
if (m == QueueSubscription.ASYNC) { |
| 99 |
sourceFused = m; |
| 100 |
queue = qs; |
| 101 |
downstream.onSubscribe(this); |
| 102 |
s.request(r); |
| 103 |
return; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
if (prefetch == Integer.MAX_VALUE) { |
| 108 |
queue = new SpscLinkedArrayQueue<>(Flowable.bufferSize()); |
| 109 |
} else { |
| 110 |
queue = new SpscArrayQueue<>(prefetch); |
| 111 |
} |
| 112 |
|
| 113 |
downstream.onSubscribe(this); |
| 114 |
|
| 115 |
s.request(r); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
@Override |
| 120 |
public void onNext(CompletableSource t) { |
| 121 |
if (sourceFused == QueueSubscription.NONE) { |
| 122 |
if (!queue.offer(t)) { |
| 123 |
onError(new MissingBackpressureException()); |
| 124 |
return; |
| 125 |
} |
| 126 |
} |
| 127 |
drain(); |
| 128 |
} |
| 129 |
|
| 130 |
@Override |
| 131 |
public void onError(Throwable t) { |
| 132 |
if (once.compareAndSet(false, true)) { |
| 133 |
DisposableHelper.dispose(inner); |
| 134 |
downstream.onError(t); |
| 135 |
} else { |
| 136 |
RxJavaPlugins.onError(t); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
@Override |
| 141 |
public void onComplete() { |
| 142 |
done = true; |
| 143 |
drain(); |
| 144 |
} |
| 145 |
|
| 146 |
@Override |
| 147 |
public void dispose() { |
| 148 |
upstream.cancel(); |
| 149 |
DisposableHelper.dispose(inner); |
| 150 |
} |
| 151 |
|
| 152 |
@Override |
| 153 |
public boolean isDisposed() { |
| 154 |
return DisposableHelper.isDisposed(inner.get()); |
| 155 |
} |
| 156 |
|
| 157 |
void drain() { |
| 158 |
if (getAndIncrement() != 0) { |
| 159 |
return; |
| 160 |
} |
| 161 |
|
| 162 |
for (;;) { |
| 163 |
if (isDisposed()) { |
| 164 |
return; |
| 165 |
} |
| 166 |
|
| 167 |
if (!active) { |
| 168 |
|
| 169 |
boolean d = done; |
| 170 |
|
| 171 |
CompletableSource cs; |
| 172 |
|
| 173 |
try { |
| 174 |
cs = queue.poll(); |
| 175 |
} catch (Throwable ex) { |
| 176 |
Exceptions.throwIfFatal(ex); |
| 177 |
innerError(ex); |
| 178 |
return; |
| 179 |
} |
| 180 |
|
| 181 |
boolean empty = cs == null; |
| 182 |
|
| 183 |
if (d && empty) { |
| 184 |
// errors never set done or call drain. |
| 185 |
downstream.onComplete(); |
| 186 |
return; |
| 187 |
} |
| 188 |
|
| 189 |
if (!empty) { |
| 190 |
active = true; |
| 191 |
cs.subscribe(inner); |
| 192 |
request(); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
if (decrementAndGet() == 0) { |
| 197 |
break; |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
void request() { |
| 203 |
if (sourceFused != QueueSubscription.SYNC) { |
| 204 |
int p = consumed + 1; |
| 205 |
if (p == limit) { |
| 206 |
consumed = 0; |
| 207 |
upstream.request(p); |
| 208 |
} else { |
| 209 |
consumed = p; |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
void innerError(Throwable e) { |
| 215 |
if (once.compareAndSet(false, true)) { |
| 216 |
upstream.cancel(); |
| 217 |
downstream.onError(e); |
| 218 |
} else { |
| 219 |
RxJavaPlugins.onError(e); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
void innerComplete() { |
| 224 |
active = false; |
| 225 |
drain(); |
| 226 |
} |
| 227 |
|
| 228 |
static final class ConcatInnerObserver extends AtomicReference<Disposable> implements CompletableObserver { |
| 229 |
private static final long serialVersionUID = -5454794857847146511L; |
| 230 |
|
| 231 |
final CompletableConcatSubscriber parent; |
| 232 |
|
| 233 |
ConcatInnerObserver(CompletableConcatSubscriber parent) { |
| 234 |
this.parent = parent; |
| 235 |
} |
| 236 |
|
| 237 |
@Override |
| 238 |
public void onSubscribe(Disposable d) { |
| 239 |
DisposableHelper.replace(this, d); |
| 240 |
} |
| 241 |
|
| 242 |
@Override |
| 243 |
public void onError(Throwable e) { |
| 244 |
parent.innerError(e); |
| 245 |
} |
| 246 |
|
| 247 |
@Override |
| 248 |
public void onComplete() { |
| 249 |
parent.innerComplete(); |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
} |