| 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.subscriptions; |
| 15 |
|
| 16 |
import java.util.Objects; |
| 17 |
import java.util.concurrent.atomic.*; |
| 18 |
|
| 19 |
import org.reactivestreams.Subscription; |
| 20 |
|
| 21 |
import io.reactivex.rxjava3.internal.util.BackpressureHelper; |
| 22 |
|
| 23 |
/** |
| 24 |
* Arbitrates requests and cancellation between Subscriptions. |
| 25 |
*/ |
| 26 |
public class SubscriptionArbiter extends AtomicInteger implements Subscription { |
| 27 |
|
| 28 |
private static final long serialVersionUID = -2189523197179400958L; |
| 29 |
|
| 30 |
/** |
| 31 |
* The current subscription which may null if no Subscriptions have been set. |
| 32 |
*/ |
| 33 |
Subscription actual; |
| 34 |
|
| 35 |
/** |
| 36 |
* The current outstanding request amount. |
| 37 |
*/ |
| 38 |
long requested; |
| 39 |
|
| 40 |
final AtomicReference<Subscription> missedSubscription; |
| 41 |
|
| 42 |
final AtomicLong missedRequested; |
| 43 |
|
| 44 |
final AtomicLong missedProduced; |
| 45 |
|
| 46 |
final boolean cancelOnReplace; |
| 47 |
|
| 48 |
volatile boolean cancelled; |
| 49 |
|
| 50 |
protected boolean unbounded; |
| 51 |
|
| 52 |
public SubscriptionArbiter(boolean cancelOnReplace) { |
| 53 |
this.cancelOnReplace = cancelOnReplace; |
| 54 |
missedSubscription = new AtomicReference<>(); |
| 55 |
missedRequested = new AtomicLong(); |
| 56 |
missedProduced = new AtomicLong(); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Atomically sets a new subscription. |
| 61 |
* @param s the subscription to set, not null (verified) |
| 62 |
*/ |
| 63 |
public final void setSubscription(Subscription s) { |
| 64 |
if (cancelled) { |
| 65 |
s.cancel(); |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
Objects.requireNonNull(s, "s is null"); |
| 70 |
|
| 71 |
if (get() == 0 && compareAndSet(0, 1)) { |
| 72 |
Subscription a = actual; |
| 73 |
|
| 74 |
if (a != null && cancelOnReplace) { |
| 75 |
a.cancel(); |
| 76 |
} |
| 77 |
|
| 78 |
actual = s; |
| 79 |
|
| 80 |
long r = requested; |
| 81 |
|
| 82 |
if (decrementAndGet() != 0) { |
| 83 |
drainLoop(); |
| 84 |
} |
| 85 |
|
| 86 |
if (r != 0L) { |
| 87 |
s.request(r); |
| 88 |
} |
| 89 |
|
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
Subscription a = missedSubscription.getAndSet(s); |
| 94 |
if (a != null && cancelOnReplace) { |
| 95 |
a.cancel(); |
| 96 |
} |
| 97 |
drain(); |
| 98 |
} |
| 99 |
|
| 100 |
@Override |
| 101 |
public final void request(long n) { |
| 102 |
if (SubscriptionHelper.validate(n)) { |
| 103 |
if (unbounded) { |
| 104 |
return; |
| 105 |
} |
| 106 |
if (get() == 0 && compareAndSet(0, 1)) { |
| 107 |
long r = requested; |
| 108 |
|
| 109 |
if (r != Long.MAX_VALUE) { |
| 110 |
r = BackpressureHelper.addCap(r, n); |
| 111 |
requested = r; |
| 112 |
if (r == Long.MAX_VALUE) { |
| 113 |
unbounded = true; |
| 114 |
} |
| 115 |
} |
| 116 |
Subscription a = actual; |
| 117 |
|
| 118 |
if (decrementAndGet() != 0) { |
| 119 |
drainLoop(); |
| 120 |
} |
| 121 |
|
| 122 |
if (a != null) { |
| 123 |
a.request(n); |
| 124 |
} |
| 125 |
|
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
BackpressureHelper.add(missedRequested, n); |
| 130 |
|
| 131 |
drain(); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
public final void produced(long n) { |
| 136 |
if (unbounded) { |
| 137 |
return; |
| 138 |
} |
| 139 |
if (get() == 0 && compareAndSet(0, 1)) { |
| 140 |
long r = requested; |
| 141 |
|
| 142 |
if (r != Long.MAX_VALUE) { |
| 143 |
long u = r - n; |
| 144 |
if (u < 0L) { |
| 145 |
SubscriptionHelper.reportMoreProduced(u); |
| 146 |
u = 0; |
| 147 |
} |
| 148 |
requested = u; |
| 149 |
} |
| 150 |
|
| 151 |
if (decrementAndGet() == 0) { |
| 152 |
return; |
| 153 |
} |
| 154 |
|
| 155 |
drainLoop(); |
| 156 |
|
| 157 |
return; |
| 158 |
} |
| 159 |
|
| 160 |
BackpressureHelper.add(missedProduced, n); |
| 161 |
|
| 162 |
drain(); |
| 163 |
} |
| 164 |
|
| 165 |
@Override |
| 166 |
public void cancel() { |
| 167 |
if (!cancelled) { |
| 168 |
cancelled = true; |
| 169 |
|
| 170 |
drain(); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
final void drain() { |
| 175 |
if (getAndIncrement() != 0) { |
| 176 |
return; |
| 177 |
} |
| 178 |
drainLoop(); |
| 179 |
} |
| 180 |
|
| 181 |
final void drainLoop() { |
| 182 |
int missed = 1; |
| 183 |
|
| 184 |
long requestAmount = 0L; |
| 185 |
Subscription requestTarget = null; |
| 186 |
|
| 187 |
for (; ; ) { |
| 188 |
|
| 189 |
Subscription ms = missedSubscription.get(); |
| 190 |
|
| 191 |
if (ms != null) { |
| 192 |
ms = missedSubscription.getAndSet(null); |
| 193 |
} |
| 194 |
|
| 195 |
long mr = missedRequested.get(); |
| 196 |
if (mr != 0L) { |
| 197 |
mr = missedRequested.getAndSet(0L); |
| 198 |
} |
| 199 |
|
| 200 |
long mp = missedProduced.get(); |
| 201 |
if (mp != 0L) { |
| 202 |
mp = missedProduced.getAndSet(0L); |
| 203 |
} |
| 204 |
|
| 205 |
Subscription a = actual; |
| 206 |
|
| 207 |
if (cancelled) { |
| 208 |
if (a != null) { |
| 209 |
a.cancel(); |
| 210 |
actual = null; |
| 211 |
} |
| 212 |
if (ms != null) { |
| 213 |
ms.cancel(); |
| 214 |
} |
| 215 |
} else { |
| 216 |
long r = requested; |
| 217 |
if (r != Long.MAX_VALUE) { |
| 218 |
long u = BackpressureHelper.addCap(r, mr); |
| 219 |
|
| 220 |
if (u != Long.MAX_VALUE) { |
| 221 |
long v = u - mp; |
| 222 |
if (v < 0L) { |
| 223 |
SubscriptionHelper.reportMoreProduced(v); |
| 224 |
v = 0; |
| 225 |
} |
| 226 |
r = v; |
| 227 |
} else { |
| 228 |
r = u; |
| 229 |
} |
| 230 |
requested = r; |
| 231 |
} |
| 232 |
|
| 233 |
if (ms != null) { |
| 234 |
if (a != null && cancelOnReplace) { |
| 235 |
a.cancel(); |
| 236 |
} |
| 237 |
actual = ms; |
| 238 |
if (r != 0L) { |
| 239 |
requestAmount = BackpressureHelper.addCap(requestAmount, r); |
| 240 |
requestTarget = ms; |
| 241 |
} |
| 242 |
} else if (a != null && mr != 0L) { |
| 243 |
requestAmount = BackpressureHelper.addCap(requestAmount, mr); |
| 244 |
requestTarget = a; |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
missed = addAndGet(-missed); |
| 249 |
if (missed == 0) { |
| 250 |
if (requestAmount != 0L) { |
| 251 |
requestTarget.request(requestAmount); |
| 252 |
} |
| 253 |
return; |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Returns true if the arbiter runs in unbounded mode. |
| 260 |
* @return true if the arbiter runs in unbounded mode |
| 261 |
*/ |
| 262 |
public final boolean isUnbounded() { |
| 263 |
return unbounded; |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Returns true if the arbiter has been cancelled. |
| 268 |
* @return true if the arbiter has been cancelled |
| 269 |
*/ |
| 270 |
public final boolean isCancelled() { |
| 271 |
return cancelled; |
| 272 |
} |
| 273 |
} |