forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFlowableVirtualCreateExecutor.java
More file actions
160 lines (131 loc) · 4.68 KB
/
Copy pathFlowableVirtualCreateExecutor.java
File metadata and controls
160 lines (131 loc) · 4.68 KB
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
/*
* Copyright (c) 2016-present, RxJava Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
* the License for the specific language governing permissions and limitations under the License.
*/
package io.reactivex.rxjava4.internal.virtual;
import java.io.Serial;
import java.util.Objects;
import java.util.concurrent.*;
import java.util.concurrent.Flow.*;
import java.util.concurrent.atomic.AtomicLong;
import io.reactivex.rxjava4.core.*;
import io.reactivex.rxjava4.disposables.*;
import io.reactivex.rxjava4.exceptions.Exceptions;
import io.reactivex.rxjava4.internal.util.BackpressureHelper;
/**
* Runs a generator callback on a virtual thread backed by a Worker of the given scheduler
* and signals events emitted by the generator considering any downstream backpressure.
*
* @param <T> the element type of the flow
* @since 4.0.0
*/
public final class FlowableVirtualCreateExecutor<T> extends Flowable<T> {
final VirtualGenerator<T> generator;
final ExecutorService executor;
final Scheduler scheduler;
public FlowableVirtualCreateExecutor(VirtualGenerator<T> generator, ExecutorService executor, Scheduler scheduler) {
this.generator = generator;
this.executor = executor;
this.scheduler = scheduler;
}
@Override
protected void subscribeActual(Subscriber<? super T> s) {
var parent = new ExecutorVirtualCreateSubscription<>(s, generator);
s.onSubscribe(parent);
if (executor != null) {
try {
executor.submit((Callable<Void>)parent);
} catch (RejectedExecutionException ex) {
s.onError(ex);
}
} else {
var worker = scheduler.createWorker();
parent.worker = worker;
worker.schedule(parent);
}
}
static final class ExecutorVirtualCreateSubscription<T> extends AtomicLong
implements Subscription, Callable<Void>, Runnable, VirtualEmitter<T> {
@Serial
private static final long serialVersionUID = -6959205135542203083L;
Subscriber<? super T> downstream;
final VirtualGenerator<T> generator;
final VirtualResumable consumerReady;
volatile boolean cancelled;
static final Throwable STOP = new Throwable("Downstream cancelled");
long produced;
Scheduler.Worker worker;
final DisposableContainer canceller;
ExecutorVirtualCreateSubscription(Subscriber<? super T> downstream, VirtualGenerator<T> generator) {
this.downstream = downstream;
this.generator = generator;
this.consumerReady = new VirtualResumable();
this.canceller = new CompositeDisposable();
}
@Override
public void run() {
call();
}
@Override
public Void call() {
try {
try {
generator.generate(this);
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
if (ex != STOP && !cancelled) {
downstream.onError(ex);
}
return null;
}
if (!cancelled) {
downstream.onComplete();
}
} finally {
downstream = null;
var w = worker;
worker = null;
if (w != null) {
w.dispose();
}
}
return null;
}
@Override
public void request(long n) {
BackpressureHelper.add(this, n);
consumerReady.resume();
}
@Override
public void cancel() {
cancelled = true;
canceller.dispose();
request(1);
}
@Override
public void emit(T item) throws Throwable {
Objects.requireNonNull(item, "item is null");
var p = produced;
while (get() == p && !cancelled) {
consumerReady.await();
}
if (cancelled) {
throw STOP;
}
downstream.onNext(item);
produced = p + 1;
}
@Override
public DisposableContainer canceller() {
return canceller;
}
}
}