| 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.operators.flowable; |
| 15 |
|
| 16 |
import java.util.concurrent.atomic.*; |
| 17 |
|
| 18 |
import org.reactivestreams.*; |
| 19 |
|
| 20 |
import io.reactivex.rxjava3.core.*; |
| 21 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper; |
| 22 |
import io.reactivex.rxjava3.internal.util.BackpressureHelper; |
| 23 |
|
| 24 |
/** |
| 25 |
* Subscribes to the source Flowable on the specified Scheduler and makes |
| 26 |
* sure downstream requests are scheduled there as well. |
| 27 |
* |
| 28 |
* @param <T> the value type emitted |
| 29 |
*/ |
| 30 |
public final class FlowableSubscribeOn<T> extends AbstractFlowableWithUpstream<T , T> { |
| 31 |
|
| 32 |
final Scheduler scheduler; |
| 33 |
|
| 34 |
final boolean nonScheduledRequests; |
| 35 |
|
| 36 |
public FlowableSubscribeOn(Flowable<T> source, Scheduler scheduler, boolean nonScheduledRequests) { |
| 37 |
super(source); |
| 38 |
this.scheduler = scheduler; |
| 39 |
this.nonScheduledRequests = nonScheduledRequests; |
| 40 |
} |
| 41 |
|
| 42 |
@Override |
| 43 |
public void subscribeActual(final Subscriber<? super T> s) { |
| 44 |
Scheduler.Worker w = scheduler.createWorker(); |
| 45 |
final SubscribeOnSubscriber<T> sos = new SubscribeOnSubscriber<>(s, w, source, nonScheduledRequests); |
| 46 |
s.onSubscribe(sos); |
| 47 |
|
| 48 |
w.schedule(sos); |
| 49 |
} |
| 50 |
|
| 51 |
static final class SubscribeOnSubscriber<T> extends AtomicReference<Thread> |
| 52 |
implements FlowableSubscriber<T>, Subscription, Runnable { |
| 53 |
|
| 54 |
private static final long serialVersionUID = 8094547886072529208L; |
| 55 |
|
| 56 |
final Subscriber<? super T> downstream; |
| 57 |
|
| 58 |
final Scheduler.Worker worker; |
| 59 |
|
| 60 |
final AtomicReference<Subscription> upstream; |
| 61 |
|
| 62 |
final AtomicLong requested; |
| 63 |
|
| 64 |
final boolean nonScheduledRequests; |
| 65 |
|
| 66 |
Publisher<T> source; |
| 67 |
|
| 68 |
SubscribeOnSubscriber(Subscriber<? super T> actual, Scheduler.Worker worker, Publisher<T> source, boolean requestOn) { |
| 69 |
this.downstream = actual; |
| 70 |
this.worker = worker; |
| 71 |
this.source = source; |
| 72 |
this.upstream = new AtomicReference<>(); |
| 73 |
this.requested = new AtomicLong(); |
| 74 |
this.nonScheduledRequests = !requestOn; |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public void run() { |
| 79 |
lazySet(Thread.currentThread()); |
| 80 |
Publisher<T> src = source; |
| 81 |
source = null; |
| 82 |
src.subscribe(this); |
| 83 |
} |
| 84 |
|
| 85 |
@Override |
| 86 |
public void onSubscribe(Subscription s) { |
| 87 |
if (SubscriptionHelper.setOnce(this.upstream, s)) { |
| 88 |
long r = requested.getAndSet(0L); |
| 89 |
if (r != 0L) { |
| 90 |
requestUpstream(r, s); |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
@Override |
| 96 |
public void onNext(T t) { |
| 97 |
downstream.onNext(t); |
| 98 |
} |
| 99 |
|
| 100 |
@Override |
| 101 |
public void onError(Throwable t) { |
| 102 |
downstream.onError(t); |
| 103 |
worker.dispose(); |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public void onComplete() { |
| 108 |
downstream.onComplete(); |
| 109 |
worker.dispose(); |
| 110 |
} |
| 111 |
|
| 112 |
@Override |
| 113 |
public void request(final long n) { |
| 114 |
if (SubscriptionHelper.validate(n)) { |
| 115 |
Subscription s = this.upstream.get(); |
| 116 |
if (s != null) { |
| 117 |
requestUpstream(n, s); |
| 118 |
} else { |
| 119 |
BackpressureHelper.add(requested, n); |
| 120 |
s = this.upstream.get(); |
| 121 |
if (s != null) { |
| 122 |
long r = requested.getAndSet(0L); |
| 123 |
if (r != 0L) { |
| 124 |
requestUpstream(r, s); |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
void requestUpstream(final long n, final Subscription s) { |
| 132 |
if (nonScheduledRequests || Thread.currentThread() == get()) { |
| 133 |
s.request(n); |
| 134 |
} else { |
| 135 |
worker.schedule(new Request(s, n)); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
@Override |
| 140 |
public void cancel() { |
| 141 |
SubscriptionHelper.cancel(upstream); |
| 142 |
worker.dispose(); |
| 143 |
} |
| 144 |
|
| 145 |
static final class Request implements Runnable { |
| 146 |
final Subscription upstream; |
| 147 |
final long n; |
| 148 |
|
| 149 |
Request(Subscription s, long n) { |
| 150 |
this.upstream = s; |
| 151 |
this.n = n; |
| 152 |
} |
| 153 |
|
| 154 |
@Override |
| 155 |
public void run() { |
| 156 |
upstream.request(n); |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
} |