Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/subscribers/QueueDrainSubscriber.java

raw
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.subscribers;
15
16 import java.util.concurrent.atomic.*;
17
18 import org.reactivestreams.Subscriber;
19
20 import io.reactivex.rxjava3.core.FlowableSubscriber;
21 import io.reactivex.rxjava3.disposables.Disposable;
22 import io.reactivex.rxjava3.exceptions.MissingBackpressureException;
23 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
24 import io.reactivex.rxjava3.internal.util.*;
25 import io.reactivex.rxjava3.operators.SimplePlainQueue;
26
27 /**
28 * Abstract base class for subscribers that hold another subscriber, a queue
29 * and requires queue-drain behavior.
30 *
31 * @param <T> the source type to which this subscriber will be subscribed
32 * @param <U> the value type in the queue
33 * @param <V> the value type the child subscriber accepts
34 */
35 public abstract class QueueDrainSubscriber<T, U, V> extends QueueDrainSubscriberPad4 implements FlowableSubscriber<T>, QueueDrain<U, V> {
36
37 protected final Subscriber<? super V> downstream;
38
39 protected final SimplePlainQueue<U> queue;
40
41 protected volatile boolean cancelled;
42
43 protected volatile boolean done;
44 protected Throwable error;
45
46 public QueueDrainSubscriber(Subscriber<? super V> actual, SimplePlainQueue<U> queue) {
47 this.downstream = actual;
48 this.queue = queue;
49 }
50
51 @Override
52 public final boolean cancelled() {
53 return cancelled;
54 }
55
56 @Override
57 public final boolean done() {
58 return done;
59 }
60
61 @Override
62 public final boolean enter() {
63 return wip.getAndIncrement() == 0;
64 }
65
66 public final boolean fastEnter() {
67 return wip.get() == 0 && wip.compareAndSet(0, 1);
68 }
69
70 protected final void fastPathEmitMax(U value, boolean delayError, Disposable dispose) {
71 final Subscriber<? super V> s = downstream;
72 final SimplePlainQueue<U> q = queue;
73
74 if (fastEnter()) {
75 long r = requested.get();
76 if (r != 0L) {
77 if (accept(s, value)) {
78 if (r != Long.MAX_VALUE) {
79 produced(1);
80 }
81 }
82 if (leave(-1) == 0) {
83 return;
84 }
85 } else {
86 dispose.dispose();
87 s.onError(new MissingBackpressureException("Could not emit buffer due to lack of requests"));
88 return;
89 }
90 } else {
91 q.offer(value);
92 if (!enter()) {
93 return;
94 }
95 }
96 QueueDrainHelper.drainMaxLoop(q, s, delayError, dispose, this);
97 }
98
99 protected final void fastPathOrderedEmitMax(U value, boolean delayError, Disposable dispose) {
100 final Subscriber<? super V> s = downstream;
101 final SimplePlainQueue<U> q = queue;
102
103 if (fastEnter()) {
104 long r = requested.get();
105 if (r != 0L) {
106 if (q.isEmpty()) {
107 if (accept(s, value)) {
108 if (r != Long.MAX_VALUE) {
109 produced(1);
110 }
111 }
112 if (leave(-1) == 0) {
113 return;
114 }
115 } else {
116 q.offer(value);
117 }
118 } else {
119 cancelled = true;
120 dispose.dispose();
121 s.onError(new MissingBackpressureException("Could not emit buffer due to lack of requests"));
122 return;
123 }
124 } else {
125 q.offer(value);
126 if (!enter()) {
127 return;
128 }
129 }
130 QueueDrainHelper.drainMaxLoop(q, s, delayError, dispose, this);
131 }
132
133 @Override
134 public boolean accept(Subscriber<? super V> a, U v) {
135 return false;
136 }
137
138 @Override
139 public final Throwable error() {
140 return error;
141 }
142
143 @Override
144 public final int leave(int m) {
145 return wip.addAndGet(m);
146 }
147
148 @Override
149 public final long requested() {
150 return requested.get();
151 }
152
153 @Override
154 public final long produced(long n) {
155 return requested.addAndGet(-n);
156 }
157
158 public final void requested(long n) {
159 if (SubscriptionHelper.validate(n)) {
160 BackpressureHelper.add(requested, n);
161 }
162 }
163
164 }
165
166 // -------------------------------------------------------------------
167 // Padding superclasses
168 //-------------------------------------------------------------------
169
170 /** Pads the header away from other fields. */
171 class QueueDrainSubscriberPad0 {
172 volatile long p1, p2, p3, p4, p5, p6, p7;
173 volatile long p8, p9, p10, p11, p12, p13, p14, p15;
174 }
175
176 /** The WIP counter. */
177 class QueueDrainSubscriberWip extends QueueDrainSubscriberPad0 {
178 final AtomicInteger wip = new AtomicInteger();
179 }
180
181 /** Pads away the wip from the other fields. */
182 class QueueDrainSubscriberPad2 extends QueueDrainSubscriberWip {
183 volatile long p1a, p2a, p3a, p4a, p5a, p6a, p7a;
184 volatile long p8a, p9a, p10a, p11a, p12a, p13a, p14a, p15a;
185 }
186
187 /** Contains the requested field. */
188 class QueueDrainSubscriberPad3 extends QueueDrainSubscriberPad2 {
189 final AtomicLong requested = new AtomicLong();
190 }
191
192 /** Pads away the requested from the other fields. */
193 class QueueDrainSubscriberPad4 extends QueueDrainSubscriberPad3 {
194 volatile long q1, q2, q3, q4, q5, q6, q7;
195 volatile long q8, q9, q10, q11, q12, q13, q14, q15;
196 }