Debian Codegraph @

src/test/java/io/reactivex/rxjava3/tck/RefCountProcessor.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.tck;
15
16 import java.util.concurrent.atomic.*;
17
18 import org.reactivestreams.*;
19
20 import io.reactivex.rxjava3.core.FlowableSubscriber;
21 import io.reactivex.rxjava3.internal.subscriptions.*;
22 import io.reactivex.rxjava3.processors.FlowableProcessor;
23
24 /**
25 * A FlowableProcessor wrapper that disposes the Subscription set via
26 * onSubscribe if the number of subscribers reaches zero.
27 *
28 * @param <T> the upstream and downstream value type
29 * @since 2.1.8
30 */
31 /* public */final class RefCountProcessor<T> extends FlowableProcessor<T> implements Subscription {
32
33 final FlowableProcessor<T> actual;
34
35 final AtomicReference<Subscription> upstream;
36
37 final AtomicReference<RefCountSubscriber<T>[]> subscribers;
38
39 @SuppressWarnings("rawtypes")
40 static final RefCountSubscriber[] EMPTY = new RefCountSubscriber[0];
41
42 @SuppressWarnings("rawtypes")
43 static final RefCountSubscriber[] TERMINATED = new RefCountSubscriber[0];
44
45 @SuppressWarnings("unchecked")
46 RefCountProcessor(FlowableProcessor<T> actual) {
47 this.actual = actual;
48 this.upstream = new AtomicReference<>();
49 this.subscribers = new AtomicReference<>(EMPTY);
50 }
51
52 @Override
53 public void onSubscribe(Subscription s) {
54 if (SubscriptionHelper.setOnce(upstream, s)) {
55 actual.onSubscribe(this);
56 }
57 }
58
59 @Override
60 public void onNext(T t) {
61 actual.onNext(t);
62 }
63
64 @Override
65 public void onError(Throwable t) {
66 upstream.lazySet(SubscriptionHelper.CANCELLED);
67 actual.onError(t);
68 }
69
70 @Override
71 public void onComplete() {
72 upstream.lazySet(SubscriptionHelper.CANCELLED);
73 actual.onComplete();
74 }
75
76 @Override
77 protected void subscribeActual(Subscriber<? super T> s) {
78 RefCountSubscriber<T> rcs = new RefCountSubscriber<>(s, this);
79 if (!add(rcs)) {
80 EmptySubscription.error(new IllegalStateException("RefCountProcessor terminated"), s);
81 return;
82 }
83 actual.subscribe(rcs);
84 }
85
86 @Override
87 public boolean hasComplete() {
88 return actual.hasComplete();
89 }
90
91 @Override
92 public boolean hasThrowable() {
93 return actual.hasThrowable();
94 }
95
96 @Override
97 public Throwable getThrowable() {
98 return actual.getThrowable();
99 }
100
101 @Override
102 public boolean hasSubscribers() {
103 return actual.hasSubscribers();
104 }
105
106 @Override
107 public void cancel() {
108 SubscriptionHelper.cancel(upstream);
109 }
110
111 @Override
112 public void request(long n) {
113 upstream.get().request(n);
114 }
115
116 boolean add(RefCountSubscriber<T> rcs) {
117 for (;;) {
118 RefCountSubscriber<T>[] a = subscribers.get();
119 if (a == TERMINATED) {
120 return false;
121 }
122 int n = a.length;
123 @SuppressWarnings("unchecked")
124 RefCountSubscriber<T>[] b = new RefCountSubscriber[n + 1];
125 System.arraycopy(a, 0, b, 0, n);
126 b[n] = rcs;
127 if (subscribers.compareAndSet(a, b)) {
128 return true;
129 }
130 }
131 }
132
133 @SuppressWarnings("unchecked")
134 void remove(RefCountSubscriber<T> rcs) {
135 for (;;) {
136 RefCountSubscriber<T>[] a = subscribers.get();
137 int n = a.length;
138 if (n == 0) {
139 break;
140 }
141 int j = -1;
142
143 for (int i = 0; i < n; i++) {
144 if (rcs == a[i]) {
145 j = i;
146 break;
147 }
148 }
149
150 if (j < 0) {
151 break;
152 }
153
154 RefCountSubscriber<T>[] b;
155 if (n == 1) {
156 b = TERMINATED;
157 } else {
158 b = new RefCountSubscriber[n - 1];
159 System.arraycopy(a, 0, b, 0, j);
160 System.arraycopy(a, j + 1, b, j, n - j - 1);
161 }
162 if (subscribers.compareAndSet(a, b)) {
163 if (b == TERMINATED) {
164 cancel();
165 }
166 break;
167 }
168 }
169 }
170
171 static final class RefCountSubscriber<T> extends AtomicBoolean implements FlowableSubscriber<T>, Subscription {
172
173 private static final long serialVersionUID = -4317488092687530631L;
174
175 final Subscriber<? super T> downstream;
176
177 final RefCountProcessor<T> parent;
178
179 Subscription upstream;
180
181 RefCountSubscriber(Subscriber<? super T> actual, RefCountProcessor<T> parent) {
182 this.downstream = actual;
183 this.parent = parent;
184 }
185
186 @Override
187 public void request(long n) {
188 upstream.request(n);
189 }
190
191 @Override
192 public void cancel() {
193 lazySet(true);
194 upstream.cancel();
195 parent.remove(this);
196 }
197
198 @Override
199 public void onSubscribe(Subscription s) {
200 this.upstream = s;
201 downstream.onSubscribe(this);
202 }
203
204 @Override
205 public void onNext(T t) {
206 downstream.onNext(t);
207 }
208
209 @Override
210 public void onError(Throwable t) {
211 downstream.onError(t);
212 }
213
214 @Override
215 public void onComplete() {
216 downstream.onComplete();
217 }
218 }
219 }