/
AbstractSubscription.java
304 lines (268 loc) · 10.8 KB
/
AbstractSubscription.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* Copyright (c) 2023 Moataz Abdelnasser
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.github.mizosoft.methanol.internal.flow;
import static com.github.mizosoft.methanol.internal.Validate.castNonNull;
import static com.github.mizosoft.methanol.internal.flow.FlowSupport.getAndAddDemand;
import static com.github.mizosoft.methanol.internal.flow.FlowSupport.subtractAndGetDemand;
import static java.util.Objects.requireNonNull;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.util.concurrent.Executor;
import java.util.concurrent.Flow.Subscriber;
import java.util.concurrent.Flow.Subscription;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
/**
* An abstract {@link Subscription} that implements most of the machinery for execution and
* backpressure control.
*/
@SuppressWarnings("unused") // VarHandle indirection.
public abstract class AbstractSubscription<T> implements Subscription {
private static final Logger logger = System.getLogger(AbstractSubscription.class.getName());
/** Consumer is running or about to run. */
private static final int RUNNING = 0x1;
/** Consumer must continue to process potentially missed signals. */
private static final int KEEP_ALIVE = 0x2;
/** Subscriber::onSubscribe has been invoked. */
private static final int SUBSCRIBED = 0x4;
/** There's a pending error that must be passed downstream if not cancelled. */
private static final int ERROR = 0x8;
/** The subscription is cancelled. */
private static final int CANCELLED = 0x10;
private static final VarHandle SYNC;
private static final VarHandle PENDING_EXCEPTION;
private static final VarHandle DEMAND;
static {
var lookup = MethodHandles.lookup();
try {
SYNC = lookup.findVarHandle(AbstractSubscription.class, "sync", int.class);
DEMAND = lookup.findVarHandle(AbstractSubscription.class, "demand", long.class);
PENDING_EXCEPTION =
lookup.findVarHandle(AbstractSubscription.class, "pendingException", Throwable.class);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new ExceptionInInitializerError(e);
}
}
private final Subscriber<? super T> downstream;
private final Executor executor;
private volatile int sync;
private volatile long demand;
private volatile @MonotonicNonNull Throwable pendingException;
protected AbstractSubscription(Subscriber<? super T> downstream, Executor executor) {
this.downstream = requireNonNull(downstream);
this.executor = requireNonNull(executor);
}
@Override
public final void request(long n) {
if (n > 0) {
getAndAddDemand(this, DEMAND, n);
fireOrKeepAlive();
} else {
fireOrKeepAliveOnError(FlowSupport.illegalRequest());
}
}
@Override
public final void cancel() {
if ((getAndBitwiseOrSync(CANCELLED) & CANCELLED) == 0) {
guardedAbort(true);
consumePendingException();
}
}
public void fireOrKeepAlive() {
if ((sync & KEEP_ALIVE) == 0
&& (getAndBitwiseOrSync(RUNNING | KEEP_ALIVE) & (RUNNING | CANCELLED)) == 0) {
fire();
}
}
public void fireOrKeepAliveOnNext() {
if (demand > 0) {
fireOrKeepAlive();
}
}
public void fireOrKeepAliveOnError(Throwable exception) {
requireNonNull(exception);
// Make sure exceptions are reported even if they can't be passed on downstream. This is
// maintained by two sides: producer (this method) and consumer (terminal methods like cancel(),
// cancelOnError(...) and cancelOnComplete(...)).
boolean produced;
if ((produced = PENDING_EXCEPTION.compareAndSet(this, null, exception))
&& (getAndBitwiseOrSync(RUNNING | KEEP_ALIVE | ERROR) & (RUNNING | CANCELLED)) == 0) {
fire();
} else if (!produced) {
FlowSupport.onDroppedException(exception);
}
}
private void fire() {
try {
executor.execute(this::drain);
} catch (RuntimeException | Error e) {
// This is a problem because we cannot call downstream here as that would ruin the execution
// context guarantee. SubmissionPublisher's behaviour here is followed (cancel & rethrow).
logger.log(Level.ERROR, "couldn't execute subscription's signaller task", e);
cancel();
throw e;
}
}
/**
* Emits at most {@code emit} items to downstream using {@link #submitOnNext( Subscriber, Object)}
* as long as it returns {@code true}. The actual number of emitted items is returned, may be 0 in
* case of cancellation or if no items are emitted, perhaps due to lack thereof, or if {@code
* emit} itself is zero. If the underlying source is finished, the subscriber is completed with
* {@link #cancelOnComplete(Subscriber)}.
*/
protected abstract long emit(Subscriber<? super T> downstream, long emit);
/**
* Releases resources held by this subscription. {@code flowInterrupted} tells whether
* cancellation was due to flow interruption by downstream (e.g. calling {@code cancel()} or
* throwing from {@code onNext} or {@code onSubscribe}), or due to ending the normal flow of
* signals (onSubscribe -> onNext* -> (onError | onComplete)).
*/
protected void abort(boolean flowInterrupted) {}
private void guardedAbort(boolean flowInterrupted) {
try {
abort(flowInterrupted);
} catch (Throwable t) {
logger.log(Level.WARNING, "exception thrown during subscription cancellation", t);
}
}
private void consumePendingException() {
var exception =
(Throwable) PENDING_EXCEPTION.getAndSet(this, ConsumedPendingException.INSTANCE);
if (exception != null && exception != ConsumedPendingException.INSTANCE) {
FlowSupport.onDroppedException(exception);
}
}
/**
* Returns {@code true} if this subscription is cancelled. {@code false} result is immediately
* outdated. Can be used by implementation to halt producing items in case the subscription was
* asynchronously cancelled.
*/
protected final boolean isCancelled() {
return (sync & CANCELLED) != 0;
}
/**
* Returns {@code true} if the subscriber is to be completed exceptionally. {@code false} result
* is immediately outdated. Can be used by implementation to halt producing items in case the
* subscription was asynchronously signalled with an error.
*/
protected final boolean hasPendingErrors() {
return (sync & ERROR) != 0;
}
/**
* Calls downstream's {@code onError} with the given exception after cancelling this subscription.
* {@code flowInterrupted} tells whether the error interrupted the normal flow of signals.
*/
protected final void cancelOnError(
Subscriber<? super T> downstream, Throwable exception, boolean flowInterrupted) {
if ((getAndBitwiseOrSync(CANCELLED) & CANCELLED) == 0) {
guardedAbort(flowInterrupted);
if (flowInterrupted) { // Otherwise drain() has already consumed the pending exception.
consumePendingException();
}
try {
downstream.onError(exception);
} catch (Throwable t) {
logger.log(
Level.WARNING, () -> "exception thrown by subscriber's onError: " + downstream, t);
}
} else {
FlowSupport.onDroppedException(exception);
}
}
/** Calls downstream's {@code onComplete} after cancelling this subscription. */
protected final void cancelOnComplete(Subscriber<? super T> downstream) {
if ((getAndBitwiseOrSync(CANCELLED) & CANCELLED) == 0) {
guardedAbort(false);
consumePendingException();
try {
downstream.onComplete();
} catch (Throwable t) {
logger.log(
Level.WARNING, () -> "exception thrown by subscriber's onComplete: " + downstream, t);
}
}
}
/**
* Submits given item to the downstream, returning {@code false} and cancelling on failure. {@code
* false} is also returned if the subscription is already cancelled or has pending errors. On such
* cases, caller should stop emitting items.
*/
protected final boolean submitOnNext(Subscriber<? super T> downstream, T item) {
if ((sync & (ERROR | CANCELLED)) == 0) {
try {
downstream.onNext(item);
return true;
} catch (Throwable t) {
cancelOnError(downstream, t, true);
}
}
return false;
}
private void drain() {
int s;
var d = downstream;
subscribeOnDrain(d);
for (long x = 0L, r = demand; ((s = sync) & CANCELLED) == 0; ) {
long emitted;
int unsetBit;
if ((s & ERROR) != 0) {
var exception =
(Throwable) PENDING_EXCEPTION.getAndSet(this, ConsumedPendingException.INSTANCE);
cancelOnError(d, castNonNull(exception), false);
} else if ((emitted = emit(d, r - x)) > 0L) {
x += emitted;
} else if (x > 0) {
// Flush satisfied demand.
r = subtractAndGetDemand(this, DEMAND, x);
x = 0L;
} else if (r == (r = demand) // Check no new demand has arrived.
&& SYNC.compareAndSet(
this, s, s & ~(unsetBit = (s & KEEP_ALIVE) == 0 ? RUNNING : KEEP_ALIVE))
&& unsetBit == RUNNING) { // Exit or consume KEEP_ALIVE.
return;
}
}
}
private void subscribeOnDrain(Subscriber<? super T> downstream) {
if ((sync & SUBSCRIBED) == 0
&& (getAndBitwiseOrSync(SUBSCRIBED) & (SUBSCRIBED | CANCELLED)) == 0) {
try {
downstream.onSubscribe(this);
} catch (Throwable t) {
cancelOnError(downstream, t, true);
}
}
}
private int getAndBitwiseOrSync(int bits) {
return (int) SYNC.getAndBitwiseOr(this, bits);
}
protected long currentDemand() {
return demand;
}
private static final class ConsumedPendingException extends Exception {
static final ConsumedPendingException INSTANCE = new ConsumedPendingException();
private ConsumedPendingException() {
super("", null, false, false);
}
}
}