io.reactivex.internal.util.BackpressureHelper Java Examples

The following examples show how to use io.reactivex.internal.util.BackpressureHelper. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FlowableInsertTimeout.java    From rxjava2-extras with Apache License 2.0 6 votes vote down vote up
@Override
public void request(long n) {
    if (SubscriptionHelper.validate(n)) {
        BackpressureHelper.add(requested, n);
        // modify request to upstream to account for inserted values
        // use a CAS loop because request can be called from any thread
        while (true) {
            long ins = inserted.get();
            long d = Math.min(ins, n);
            if (inserted.compareAndSet(ins, ins - d)) {
                if (n - d > 0) {
                    upstream.request(n - d);
                }
                break;
            }
        }
        drain();
    }
}
 
Example #2
Source File: FlowableRepeat.java    From rxjava2-extras with Apache License 2.0 6 votes vote down vote up
@Override
public void request(long n) {
    if (SubscriptionHelper.validate(n)) {
        if (BackpressureHelper.add(this, n) == 0) {
            long requested = n;
            long emitted = 0;
            do {
                emitted = requested;
                while (requested-- > 0 && !cancelled && (count == -1 || counter-- > 0)) {
                    child.onNext(value);
                }
            } while ((requested = this.addAndGet(-emitted)) > 0);
            if (count >= 0 && !cancelled) {
                child.onComplete();
            }
        }
    }
}
 
Example #3
Source File: FlowableRepeatingTransform.java    From rxjava2-extras with Apache License 2.0 6 votes vote down vote up
@Override
public void request(long n) {
    debug(this + " request " + n);
    if (SubscriptionHelper.validate(n)) {
        BackpressureHelper.add(requested, n);
        while (true) {
            Subscription p = parent.get();
            long d = deferredRequests.get();
            if (d == -1) {
                // parent exists so can request of it
                debug(this + " requesting from parent " + n);
                p.request(n);
                break;
            } else {
                long d2 = d + n;
                if (d2 < 0) {
                    d2 = Long.MAX_VALUE;
                }
                if (deferredRequests.compareAndSet(d, d2)) {
                    break;
                }
            }
        }
        drain();
    }
}
 
Example #4
Source File: FlowableInsertMaybe.java    From rxjava2-extras with Apache License 2.0 6 votes vote down vote up
@Override
public void request(long n) {
    if (SubscriptionHelper.validate(n)) {
        BackpressureHelper.add(requested, n);
        // modify request to upstream to account for inserted values
        // use a CAS loop because request can be called from any thread
        while (true) {
            long ins = inserted.get();
            long d = Math.min(ins, n);
            if (inserted.compareAndSet(ins, ins - d)) {
                if (n - d > 0) {
                    upstream.request(n - d);
                }
                break;
            }
        }
        drain();
    }
}
 
Example #5
Source File: FlowableStringSplitSimple.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
@Override
public void request(long n) {
    if (SubscriptionHelper.validate(n)) {
        BackpressureHelper.add(this, n);
        if (once.compareAndSet(false, true)) {
            if (n == Long.MAX_VALUE) {
                parent.request(Long.MAX_VALUE);
                unbounded = true;
            } else {
                parent.request(1);
            }
        }
        drain();
    }
}
 
Example #6
Source File: ResourceFlowableObserveOn.java    From akarnokd-misc with Apache License 2.0 5 votes vote down vote up
@Override
public void request(long n) {
    if (SubscriptionHelper.validate(n)) {
        BackpressureHelper.add(requested, n);
        schedule();
    }
}
 
Example #7
Source File: ResourceFlowableArray.java    From akarnokd-misc with Apache License 2.0 5 votes vote down vote up
@Override
public void cancel() {
    if (!cancelled) {
        cancelled = true;

        if (BackpressureHelper.add(this, 1) == 0) {
            T[] a = items;
            int n = a.length;
            Consumer<? super T> r = release;
            for (int i = index; i < n; i++) {
                releaseItem(a[i], r);
            }
        }
    }
}
 
Example #8
Source File: ResourceFlowableIterable.java    From akarnokd-misc with Apache License 2.0 5 votes vote down vote up
@Override
public void cancel() {
    if (!cancelled) {
        cancelled = true;

        if (BackpressureHelper.add(this, 1) == 0) {
            releaseRest(items, release);
        }
    }
}
 
Example #9
Source File: SingleFlatMapIterableFlowable.java    From akarnokd-misc with Apache License 2.0 5 votes vote down vote up
@Override
public void request(long n) {
    if (SubscriptionHelper.validate(n)) {
        BackpressureHelper.add(requested, n);
        drain();
    }
}
 
Example #10
Source File: FlowableCollectWhile.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
@Override
public void request(long n) {
    if (SubscriptionHelper.validate(n)) {
        BackpressureHelper.add(requested, n);
        parent.request(n);
        drain();
    }
}
 
Example #11
Source File: FlowableStateMachine.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
@Override
public void request(long n) {
    if (SubscriptionHelper.validate(n)) {
        BackpressureHelper.add(requested, n);
        drain();
    }
}
 
Example #12
Source File: FlowableMergeInterleave.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
@Override
public void request(long n) {
    if (SubscriptionHelper.validate(n)) {
        BackpressureHelper.add(requested, n);
        if (once.compareAndSet(false, true)) {
            sources.subscribe(this);
            subscription.request(maxConcurrent);
        }
        drain();
    }
}
 
Example #13
Source File: FlowableRepeatingTransform.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
@Override
public void request(long n) {
    debug(this + " request " + n);
    if (SubscriptionHelper.validate(n)) {
        BackpressureHelper.add(requested, n);
        while (true) {
            Requests<T> r = requests.get();
            Requests<T> r2;
            if (r.parent == null) {
                long d = r.deferred + n;
                if (d < 0) {
                    d = Long.MAX_VALUE;
                }
                r2 = new Requests<T>(r.parent, r.unreconciled, d, r.child);
                if (requests.compareAndSet(r, r2)) {
                    break;
                }
            } else {
                long x = n + r.deferred - r.unreconciled;
                long u = Math.max(0, -x);
                r2 = new Requests<T>(r.parent, u, 0, r.child);
                if (requests.compareAndSet(r, r2)) {
                    if (x > 0) {
                        r.parent.request(x);
                    }
                    break;
                }
            }
        }
        drain();
    }
}
 
Example #14
Source File: FlowableOnBackpressureBufferToFile.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
@Override
public void request(long n) {
    if (SubscriptionHelper.validate(n)) {
        BackpressureHelper.add(requested, n);
        scheduleDrain();
    }
}
 
Example #15
Source File: FlowableOnBackpressureBufferToFile.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
@Override
public void request(long n) {
    if (SubscriptionHelper.validate(n)) {
        BackpressureHelper.add(requested, n);
        parent.request(n);
        scheduleDrain();
    }
}
 
Example #16
Source File: FlowableMatch.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
@Override
public void request(long n) {
    if (SubscriptionHelper.validate(n)) {
        BackpressureHelper.add(requested, n);
        drain();
    }
}
 
Example #17
Source File: FlowableMaxRequest.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
@Override
public void request(long n) {
    if (SubscriptionHelper.validate(n)) {
        BackpressureHelper.add(requested, n);
        requestMore();
    }
}
 
Example #18
Source File: FlowableMinRequest.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
@Override
public void request(long n) {
    if (SubscriptionHelper.validate(n)) {
        BackpressureHelper.add(requested, n);
        drain();
    }
}
 
Example #19
Source File: TestCallStreamObserverProducer.java    From reactive-grpc with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
static long add(TestCallStreamObserverProducer o, long n) {
    for (;;) {
        long r = REQUESTED.get(o);
        if (r == Long.MAX_VALUE) {
            return Long.MAX_VALUE;
        }
        long u = BackpressureHelper.addCap(r, n);
        if ((REQUESTED).compareAndSet(o, r, u)) {
            return r;
        }
    }
}
 
Example #20
Source File: Burst.java    From rxjava2-extras with Apache License 2.0 4 votes vote down vote up
@Override
protected void subscribeActual(final Subscriber<? super T> subscriber) {
    subscriber.onSubscribe(new Subscription() {

        final Queue<T> q = new ConcurrentLinkedQueue<T>(items);
        final AtomicLong requested = new AtomicLong();
        volatile boolean cancelled;

        @Override
        public void request(long n) {
            if (cancelled) {
                // required by reactive-streams-jvm 3.6
                return;
            }
            if (SubscriptionHelper.validate(n)) {
                // just for testing, don't care about perf
                // so no attempt made to reduce volatile reads
                if (BackpressureHelper.add(requested, n) == 0) {
                    if (q.isEmpty()) {
                        return;
                    }
                    while (!q.isEmpty() && requested.get() > 0) {
                        T item = q.poll();
                        requested.decrementAndGet();
                        subscriber.onNext(item);
                    }
                    if (q.isEmpty()) {
                        if (error != null) {
                            subscriber.onError(error);
                        } else {
                            subscriber.onComplete();
                        }
                    }
                }
            }
        }

        @Override
        public void cancel() {
            cancelled = true;
        }
    });

}
 
Example #21
Source File: SomeAsyncApiBridge.java    From akarnokd-misc with Apache License 2.0 4 votes vote down vote up
@Override
public void request(long n) {
    BackpressureHelper.add(requested, n);
    drain();
}
 
Example #22
Source File: ThrottleLastTest.java    From akarnokd-misc with Apache License 2.0 4 votes vote down vote up
@Override
public void request(long n) {
    BackpressureHelper.add(this, n);
    worker.schedule(this);
}
 
Example #23
Source File: LockstepObserveOnTest.java    From akarnokd-misc with Apache License 2.0 4 votes vote down vote up
@Override
public void request(long n) {
    BackpressureHelper.add(requested, n);
    schedule();
}