| 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.exceptions.ProtocolViolationException; |
| 22 |
import io.reactivex.rxjava3.internal.util.BackpressureHelper; |
| 23 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 24 |
|
| 25 |
/** |
| 26 |
* Utility methods to validate Subscriptions in the various onSubscribe calls. |
| 27 |
*/ |
| 28 |
public enum SubscriptionHelper implements Subscription { |
| 29 |
/** |
| 30 |
* Represents a cancelled Subscription. |
| 31 |
* <p>Don't leak this instance! |
| 32 |
*/ |
| 33 |
CANCELLED |
| 34 |
; |
| 35 |
|
| 36 |
@Override |
| 37 |
public void request(long n) { |
| 38 |
// deliberately ignored |
| 39 |
} |
| 40 |
|
| 41 |
@Override |
| 42 |
public void cancel() { |
| 43 |
// deliberately ignored |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Verifies that current is null, next is not null, otherwise signals errors |
| 48 |
* to the RxJavaPlugins and returns false. |
| 49 |
* @param current the current Subscription, expected to be null |
| 50 |
* @param next the next Subscription, expected to be non-null |
| 51 |
* @return true if the validation succeeded |
| 52 |
*/ |
| 53 |
public static boolean validate(Subscription current, Subscription next) { |
| 54 |
if (next == null) { |
| 55 |
RxJavaPlugins.onError(new NullPointerException("next is null")); |
| 56 |
return false; |
| 57 |
} |
| 58 |
if (current != null) { |
| 59 |
next.cancel(); |
| 60 |
reportSubscriptionSet(); |
| 61 |
return false; |
| 62 |
} |
| 63 |
return true; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Reports that the subscription is already set to the RxJavaPlugins error handler, |
| 68 |
* which is an indication of a onSubscribe management bug. |
| 69 |
*/ |
| 70 |
public static void reportSubscriptionSet() { |
| 71 |
RxJavaPlugins.onError(new ProtocolViolationException("Subscription already set!")); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Validates that the n is positive. |
| 76 |
* @param n the request amount |
| 77 |
* @return false if n is non-positive. |
| 78 |
*/ |
| 79 |
public static boolean validate(long n) { |
| 80 |
if (n <= 0) { |
| 81 |
RxJavaPlugins.onError(new IllegalArgumentException("n > 0 required but it was " + n)); |
| 82 |
return false; |
| 83 |
} |
| 84 |
return true; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Reports to the plugin error handler that there were more values produced than requested, which |
| 89 |
* is a sign of internal backpressure handling bug. |
| 90 |
* @param n the overproduction amount |
| 91 |
*/ |
| 92 |
public static void reportMoreProduced(long n) { |
| 93 |
RxJavaPlugins.onError(new ProtocolViolationException("More produced than requested: " + n)); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Atomically sets the subscription on the field and cancels the |
| 98 |
* previous subscription if any. |
| 99 |
* @param field the target field to set the new subscription on |
| 100 |
* @param s the new subscription |
| 101 |
* @return true if the operation succeeded, false if the target field |
| 102 |
* holds the {@link #CANCELLED} instance. |
| 103 |
* @see #replace(AtomicReference, Subscription) |
| 104 |
*/ |
| 105 |
public static boolean set(AtomicReference<Subscription> field, Subscription s) { |
| 106 |
for (;;) { |
| 107 |
Subscription current = field.get(); |
| 108 |
if (current == CANCELLED) { |
| 109 |
if (s != null) { |
| 110 |
s.cancel(); |
| 111 |
} |
| 112 |
return false; |
| 113 |
} |
| 114 |
if (field.compareAndSet(current, s)) { |
| 115 |
if (current != null) { |
| 116 |
current.cancel(); |
| 117 |
} |
| 118 |
return true; |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Atomically sets the subscription on the field if it is still null. |
| 125 |
* <p>If the field is not null and doesn't contain the {@link #CANCELLED} |
| 126 |
* instance, the {@link #reportSubscriptionSet()} is called. |
| 127 |
* @param field the target field |
| 128 |
* @param s the new subscription to set |
| 129 |
* @return true if the operation succeeded, false if the target field was not null. |
| 130 |
*/ |
| 131 |
public static boolean setOnce(AtomicReference<Subscription> field, Subscription s) { |
| 132 |
Objects.requireNonNull(s, "s is null"); |
| 133 |
if (!field.compareAndSet(null, s)) { |
| 134 |
s.cancel(); |
| 135 |
if (field.get() != CANCELLED) { |
| 136 |
reportSubscriptionSet(); |
| 137 |
} |
| 138 |
return false; |
| 139 |
} |
| 140 |
return true; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Atomically sets the subscription on the field but does not |
| 145 |
* cancel the previous subscription. |
| 146 |
* @param field the target field to set the new subscription on |
| 147 |
* @param s the new subscription |
| 148 |
* @return true if the operation succeeded, false if the target field |
| 149 |
* holds the {@link #CANCELLED} instance. |
| 150 |
* @see #set(AtomicReference, Subscription) |
| 151 |
*/ |
| 152 |
public static boolean replace(AtomicReference<Subscription> field, Subscription s) { |
| 153 |
for (;;) { |
| 154 |
Subscription current = field.get(); |
| 155 |
if (current == CANCELLED) { |
| 156 |
if (s != null) { |
| 157 |
s.cancel(); |
| 158 |
} |
| 159 |
return false; |
| 160 |
} |
| 161 |
if (field.compareAndSet(current, s)) { |
| 162 |
return true; |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Atomically swaps in the common cancelled subscription instance |
| 169 |
* and cancels the previous subscription if any. |
| 170 |
* @param field the target field to dispose the contents of |
| 171 |
* @return true if the swap from the non-cancelled instance to the |
| 172 |
* common cancelled instance happened in the caller's thread (allows |
| 173 |
* further one-time actions). |
| 174 |
*/ |
| 175 |
public static boolean cancel(AtomicReference<Subscription> field) { |
| 176 |
Subscription current = field.get(); |
| 177 |
if (current != CANCELLED) { |
| 178 |
current = field.getAndSet(CANCELLED); |
| 179 |
if (current != CANCELLED) { |
| 180 |
if (current != null) { |
| 181 |
current.cancel(); |
| 182 |
} |
| 183 |
return true; |
| 184 |
} |
| 185 |
} |
| 186 |
return false; |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Atomically sets the new Subscription on the field and requests any accumulated amount |
| 191 |
* from the requested field. |
| 192 |
* @param field the target field for the new Subscription |
| 193 |
* @param requested the current requested amount |
| 194 |
* @param s the new Subscription, not null (verified) |
| 195 |
* @return true if the Subscription was set the first time |
| 196 |
*/ |
| 197 |
public static boolean deferredSetOnce(AtomicReference<Subscription> field, AtomicLong requested, |
| 198 |
Subscription s) { |
| 199 |
if (SubscriptionHelper.setOnce(field, s)) { |
| 200 |
long r = requested.getAndSet(0L); |
| 201 |
if (r != 0L) { |
| 202 |
s.request(r); |
| 203 |
} |
| 204 |
return true; |
| 205 |
} |
| 206 |
return false; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Atomically requests from the Subscription in the field if not null, otherwise accumulates |
| 211 |
* the request amount in the requested field to be requested once the field is set to non-null. |
| 212 |
* @param field the target field that may already contain a Subscription |
| 213 |
* @param requested the current requested amount |
| 214 |
* @param n the request amount, positive (verified) |
| 215 |
*/ |
| 216 |
public static void deferredRequest(AtomicReference<Subscription> field, AtomicLong requested, long n) { |
| 217 |
Subscription s = field.get(); |
| 218 |
if (s != null) { |
| 219 |
s.request(n); |
| 220 |
} else { |
| 221 |
if (SubscriptionHelper.validate(n)) { |
| 222 |
BackpressureHelper.add(requested, n); |
| 223 |
|
| 224 |
s = field.get(); |
| 225 |
if (s != null) { |
| 226 |
long r = requested.getAndSet(0L); |
| 227 |
if (r != 0L) { |
| 228 |
s.request(r); |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Atomically sets the subscription on the field if it is still null and issues a positive request |
| 237 |
* to the given {@link Subscription}. |
| 238 |
* <p> |
| 239 |
* If the field is not null and doesn't contain the {@link #CANCELLED} |
| 240 |
* instance, the {@link #reportSubscriptionSet()} is called. |
| 241 |
* @param field the target field |
| 242 |
* @param s the new subscription to set |
| 243 |
* @param request the amount to request, positive (not verified) |
| 244 |
* @return true if the operation succeeded, false if the target field was not null. |
| 245 |
* @since 2.1.11 |
| 246 |
*/ |
| 247 |
public static boolean setOnce(AtomicReference<Subscription> field, Subscription s, long request) { |
| 248 |
if (setOnce(field, s)) { |
| 249 |
s.request(request); |
| 250 |
return true; |
| 251 |
} |
| 252 |
return false; |
| 253 |
} |
| 254 |
} |