| 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.*; |
| 17 |
import java.util.concurrent.atomic.AtomicReference; |
| 18 |
import java.util.concurrent.locks.*; |
| 19 |
|
| 20 |
import org.reactivestreams.Subscription; |
| 21 |
|
| 22 |
import io.reactivex.rxjava3.core.*; |
| 23 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 24 |
import io.reactivex.rxjava3.exceptions.*; |
| 25 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper; |
| 26 |
import io.reactivex.rxjava3.internal.util.*; |
| 27 |
import io.reactivex.rxjava3.operators.SpscArrayQueue; |
| 28 |
|
| 29 |
public final class BlockingFlowableIterable<T> implements Iterable<T> { |
| 30 |
final Flowable<T> source; |
| 31 |
|
| 32 |
final int bufferSize; |
| 33 |
|
| 34 |
public BlockingFlowableIterable(Flowable<T> source, int bufferSize) { |
| 35 |
this.source = source; |
| 36 |
this.bufferSize = bufferSize; |
| 37 |
} |
| 38 |
|
| 39 |
@Override |
| 40 |
public Iterator<T> iterator() { |
| 41 |
BlockingFlowableIterator<T> it = new BlockingFlowableIterator<>(bufferSize); |
| 42 |
source.subscribe(it); |
| 43 |
return it; |
| 44 |
} |
| 45 |
|
| 46 |
static final class BlockingFlowableIterator<T> |
| 47 |
extends AtomicReference<Subscription> |
| 48 |
implements FlowableSubscriber<T>, Iterator<T>, Runnable, Disposable { |
| 49 |
|
| 50 |
private static final long serialVersionUID = 6695226475494099826L; |
| 51 |
|
| 52 |
final SpscArrayQueue<T> queue; |
| 53 |
|
| 54 |
final long batchSize; |
| 55 |
|
| 56 |
final long limit; |
| 57 |
|
| 58 |
final Lock lock; |
| 59 |
|
| 60 |
final Condition condition; |
| 61 |
|
| 62 |
long produced; |
| 63 |
|
| 64 |
volatile boolean done; |
| 65 |
volatile Throwable error; |
| 66 |
|
| 67 |
BlockingFlowableIterator(int batchSize) { |
| 68 |
this.queue = new SpscArrayQueue<>(batchSize); |
| 69 |
this.batchSize = batchSize; |
| 70 |
this.limit = batchSize - (batchSize >> 2); |
| 71 |
this.lock = new ReentrantLock(); |
| 72 |
this.condition = lock.newCondition(); |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public boolean hasNext() { |
| 77 |
for (;;) { |
| 78 |
if (isDisposed()) { |
| 79 |
Throwable e = error; |
| 80 |
if (e != null) { |
| 81 |
throw ExceptionHelper.wrapOrThrow(e); |
| 82 |
} |
| 83 |
return false; |
| 84 |
} |
| 85 |
boolean d = done; |
| 86 |
boolean empty = queue.isEmpty(); |
| 87 |
if (d) { |
| 88 |
Throwable e = error; |
| 89 |
if (e != null) { |
| 90 |
throw ExceptionHelper.wrapOrThrow(e); |
| 91 |
} else |
| 92 |
if (empty) { |
| 93 |
return false; |
| 94 |
} |
| 95 |
} |
| 96 |
if (empty) { |
| 97 |
BlockingHelper.verifyNonBlocking(); |
| 98 |
lock.lock(); |
| 99 |
try { |
| 100 |
while (!done && queue.isEmpty() && !isDisposed()) { |
| 101 |
condition.await(); |
| 102 |
} |
| 103 |
} catch (InterruptedException ex) { |
| 104 |
run(); |
| 105 |
throw ExceptionHelper.wrapOrThrow(ex); |
| 106 |
} finally { |
| 107 |
lock.unlock(); |
| 108 |
} |
| 109 |
} else { |
| 110 |
return true; |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
@Override |
| 116 |
public T next() { |
| 117 |
if (hasNext()) { |
| 118 |
T v = queue.poll(); |
| 119 |
|
| 120 |
long p = produced + 1; |
| 121 |
if (p == limit) { |
| 122 |
produced = 0; |
| 123 |
get().request(p); |
| 124 |
} else { |
| 125 |
produced = p; |
| 126 |
} |
| 127 |
|
| 128 |
return v; |
| 129 |
} |
| 130 |
throw new NoSuchElementException(); |
| 131 |
} |
| 132 |
|
| 133 |
@Override |
| 134 |
public void onSubscribe(Subscription s) { |
| 135 |
SubscriptionHelper.setOnce(this, s, batchSize); |
| 136 |
} |
| 137 |
|
| 138 |
@Override |
| 139 |
public void onNext(T t) { |
| 140 |
if (!queue.offer(t)) { |
| 141 |
SubscriptionHelper.cancel(this); |
| 142 |
|
| 143 |
onError(new MissingBackpressureException("Queue full?!")); |
| 144 |
} else { |
| 145 |
signalConsumer(); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
@Override |
| 150 |
public void onError(Throwable t) { |
| 151 |
error = t; |
| 152 |
done = true; |
| 153 |
signalConsumer(); |
| 154 |
} |
| 155 |
|
| 156 |
@Override |
| 157 |
public void onComplete() { |
| 158 |
done = true; |
| 159 |
signalConsumer(); |
| 160 |
} |
| 161 |
|
| 162 |
void signalConsumer() { |
| 163 |
lock.lock(); |
| 164 |
try { |
| 165 |
condition.signalAll(); |
| 166 |
} finally { |
| 167 |
lock.unlock(); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
@Override |
| 172 |
public void run() { |
| 173 |
SubscriptionHelper.cancel(this); |
| 174 |
signalConsumer(); |
| 175 |
} |
| 176 |
|
| 177 |
@Override // otherwise default method which isn't available in Java 7 |
| 178 |
public void remove() { |
| 179 |
throw new UnsupportedOperationException("remove"); |
| 180 |
} |
| 181 |
|
| 182 |
@Override |
| 183 |
public void dispose() { |
| 184 |
SubscriptionHelper.cancel(this); |
| 185 |
signalConsumer(); // Just in case it is currently blocking in hasNext. |
| 186 |
} |
| 187 |
|
| 188 |
@Override |
| 189 |
public boolean isDisposed() { |
| 190 |
return get() == SubscriptionHelper.CANCELLED; |
| 191 |
} |
| 192 |
} |
| 193 |
} |