| 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 org.reactivestreams.Subscriber; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.annotations.*; |
| 19 |
|
| 20 |
/** |
| 21 |
* A subscription that signals a single value eventually. |
| 22 |
* <p> |
| 23 |
* Note that the class leaks all methods of {@link java.util.concurrent.atomic.AtomicLong}. |
| 24 |
* Use {@link #complete(Object)} to signal the single value. |
| 25 |
* <p> |
| 26 |
* The this atomic integer stores a bit field:<br> |
| 27 |
* bit 0: indicates that there is a value available<br> |
| 28 |
* bit 1: indicates that there was a request made<br> |
| 29 |
* bit 2: indicates there was a cancellation, exclusively set<br> |
| 30 |
* bit 3: indicates in fusion mode but no value yet, exclusively set<br> |
| 31 |
* bit 4: indicates in fusion mode and value is available, exclusively set<br> |
| 32 |
* bit 5: indicates in fusion mode and value has been consumed, exclusively set<br> |
| 33 |
* Where exclusively set means any other bits are 0 when that bit is set. |
| 34 |
* @param <T> the value type |
| 35 |
*/ |
| 36 |
public class DeferredScalarSubscription<@NonNull T> extends BasicIntQueueSubscription<T> { |
| 37 |
|
| 38 |
private static final long serialVersionUID = -2151279923272604993L; |
| 39 |
|
| 40 |
/** The Subscriber to emit the value to. */ |
| 41 |
protected final Subscriber<? super T> downstream; |
| 42 |
|
| 43 |
/** The value is stored here if there is no request yet or in fusion mode. */ |
| 44 |
protected T value; |
| 45 |
|
| 46 |
/** Indicates this Subscription has no value and not requested yet. */ |
| 47 |
static final int NO_REQUEST_NO_VALUE = 0; |
| 48 |
/** Indicates this Subscription has a value but not requested yet. */ |
| 49 |
static final int NO_REQUEST_HAS_VALUE = 1; |
| 50 |
/** Indicates this Subscription has been requested but there is no value yet. */ |
| 51 |
static final int HAS_REQUEST_NO_VALUE = 2; |
| 52 |
/** Indicates this Subscription has both request and value. */ |
| 53 |
static final int HAS_REQUEST_HAS_VALUE = 3; |
| 54 |
|
| 55 |
/** Indicates the Subscription has been cancelled. */ |
| 56 |
static final int CANCELLED = 4; |
| 57 |
|
| 58 |
/** Indicates this Subscription is in fusion mode and is currently empty. */ |
| 59 |
static final int FUSED_EMPTY = 8; |
| 60 |
/** Indicates this Subscription is in fusion mode and has a value. */ |
| 61 |
static final int FUSED_READY = 16; |
| 62 |
/** Indicates this Subscription is in fusion mode and its value has been consumed. */ |
| 63 |
static final int FUSED_CONSUMED = 32; |
| 64 |
|
| 65 |
/** |
| 66 |
* Creates a DeferredScalarSubscription by wrapping the given Subscriber. |
| 67 |
* @param downstream the Subscriber to wrap, not null (not verified) |
| 68 |
*/ |
| 69 |
public DeferredScalarSubscription(Subscriber<? super T> downstream) { |
| 70 |
this.downstream = downstream; |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
public final void request(long n) { |
| 75 |
if (SubscriptionHelper.validate(n)) { |
| 76 |
for (;;) { |
| 77 |
int state = get(); |
| 78 |
// if the any bits 1-31 are set, we are either in fusion mode (FUSED_*) |
| 79 |
// or request has been called (HAS_REQUEST_*) |
| 80 |
if ((state & ~NO_REQUEST_HAS_VALUE) != 0) { |
| 81 |
return; |
| 82 |
} |
| 83 |
if (state == NO_REQUEST_HAS_VALUE) { |
| 84 |
if (compareAndSet(NO_REQUEST_HAS_VALUE, HAS_REQUEST_HAS_VALUE)) { |
| 85 |
T v = value; |
| 86 |
if (v != null) { |
| 87 |
value = null; |
| 88 |
Subscriber<? super T> a = downstream; |
| 89 |
a.onNext(v); |
| 90 |
if (get() != CANCELLED) { |
| 91 |
a.onComplete(); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
return; |
| 96 |
} |
| 97 |
if (compareAndSet(NO_REQUEST_NO_VALUE, HAS_REQUEST_NO_VALUE)) { |
| 98 |
return; |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Completes this subscription by indicating the given value should |
| 106 |
* be emitted when the first request arrives. |
| 107 |
* <p>Make sure this is called exactly once. |
| 108 |
* @param v the value to signal, not null (not validated) |
| 109 |
*/ |
| 110 |
public final void complete(T v) { |
| 111 |
int state = get(); |
| 112 |
for (;;) { |
| 113 |
if (state == FUSED_EMPTY) { |
| 114 |
value = v; |
| 115 |
lazySet(FUSED_READY); |
| 116 |
|
| 117 |
Subscriber<? super T> a = downstream; |
| 118 |
a.onNext(null); |
| 119 |
if (get() != CANCELLED) { |
| 120 |
a.onComplete(); |
| 121 |
} |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
// if state is >= CANCELLED or bit zero is set (*_HAS_VALUE) case, return |
| 126 |
if ((state & ~HAS_REQUEST_NO_VALUE) != 0) { |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
if (state == HAS_REQUEST_NO_VALUE) { |
| 131 |
lazySet(HAS_REQUEST_HAS_VALUE); |
| 132 |
Subscriber<? super T> a = downstream; |
| 133 |
a.onNext(v); |
| 134 |
if (get() != CANCELLED) { |
| 135 |
a.onComplete(); |
| 136 |
} |
| 137 |
return; |
| 138 |
} |
| 139 |
value = v; |
| 140 |
if (compareAndSet(NO_REQUEST_NO_VALUE, NO_REQUEST_HAS_VALUE)) { |
| 141 |
return; |
| 142 |
} |
| 143 |
state = get(); |
| 144 |
if (state == CANCELLED) { |
| 145 |
value = null; |
| 146 |
return; |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
@Override |
| 152 |
public final int requestFusion(int mode) { |
| 153 |
if ((mode & ASYNC) != 0) { |
| 154 |
lazySet(FUSED_EMPTY); |
| 155 |
return ASYNC; |
| 156 |
} |
| 157 |
return NONE; |
| 158 |
} |
| 159 |
|
| 160 |
@Nullable |
| 161 |
@Override |
| 162 |
public final T poll() { |
| 163 |
if (get() == FUSED_READY) { |
| 164 |
lazySet(FUSED_CONSUMED); |
| 165 |
T v = value; |
| 166 |
value = null; |
| 167 |
return v; |
| 168 |
} |
| 169 |
return null; |
| 170 |
} |
| 171 |
|
| 172 |
@Override |
| 173 |
public final boolean isEmpty() { |
| 174 |
return get() != FUSED_READY; |
| 175 |
} |
| 176 |
|
| 177 |
@Override |
| 178 |
public final void clear() { |
| 179 |
lazySet(FUSED_CONSUMED); |
| 180 |
value = null; |
| 181 |
} |
| 182 |
|
| 183 |
@Override |
| 184 |
public void cancel() { |
| 185 |
set(CANCELLED); |
| 186 |
value = null; |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Returns true if this Subscription has been cancelled. |
| 191 |
* @return true if this Subscription has been cancelled |
| 192 |
*/ |
| 193 |
public final boolean isCancelled() { |
| 194 |
return get() == CANCELLED; |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Atomically sets a cancelled state and returns true if |
| 199 |
* the current thread did it successfully. |
| 200 |
* @return true if the current thread cancelled |
| 201 |
*/ |
| 202 |
public final boolean tryCancel() { |
| 203 |
return getAndSet(CANCELLED) != CANCELLED; |
| 204 |
} |
| 205 |
} |