Debian Codegraph @

src/main/java/io/reactivex/rxjava3/processors/SerializedProcessor.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.processors;
15
16 import org.reactivestreams.*;
17
18 import io.reactivex.rxjava3.annotations.Nullable;
19 import io.reactivex.rxjava3.internal.util.*;
20 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
21
22 /**
23 * Serializes calls to the Subscriber methods.
24 * <p>All other Publisher and Subject methods are thread-safe by design.
25 *
26 * @param <T> the item value type
27 */
28 /* public */ final class SerializedProcessor<T> extends FlowableProcessor<T> {
29 /** The actual subscriber to serialize Subscriber calls to. */
30 final FlowableProcessor<T> actual;
31 /** Indicates an emission is going on, guarded by this. */
32 boolean emitting;
33 /** If not null, it holds the missed NotificationLite events. */
34 AppendOnlyLinkedArrayList<Object> queue;
35 /** Indicates a terminal event has been received and all further events will be dropped. */
36 volatile boolean done;
37
38 /**
39 * Constructor that wraps an actual subject.
40 * @param actual the subject wrapped
41 */
42 SerializedProcessor(final FlowableProcessor<T> actual) {
43 this.actual = actual;
44 }
45
46 @Override
47 protected void subscribeActual(Subscriber<? super T> s) {
48 actual.subscribe(s);
49 }
50
51 @Override
52 public void onSubscribe(Subscription s) {
53 boolean cancel;
54 if (!done) {
55 synchronized (this) {
56 if (done) {
57 cancel = true;
58 } else {
59 if (emitting) {
60 AppendOnlyLinkedArrayList<Object> q = queue;
61 if (q == null) {
62 q = new AppendOnlyLinkedArrayList<>(4);
63 queue = q;
64 }
65 q.add(NotificationLite.subscription(s));
66 return;
67 }
68 emitting = true;
69 cancel = false;
70 }
71 }
72 } else {
73 cancel = true;
74 }
75 if (cancel) {
76 s.cancel();
77 } else {
78 actual.onSubscribe(s);
79 emitLoop();
80 }
81 }
82
83 @Override
84 public void onNext(T t) {
85 if (done) {
86 return;
87 }
88 synchronized (this) {
89 if (done) {
90 return;
91 }
92 if (emitting) {
93 AppendOnlyLinkedArrayList<Object> q = queue;
94 if (q == null) {
95 q = new AppendOnlyLinkedArrayList<>(4);
96 queue = q;
97 }
98 q.add(NotificationLite.next(t));
99 return;
100 }
101 emitting = true;
102 }
103 actual.onNext(t);
104 emitLoop();
105 }
106
107 @Override
108 public void onError(Throwable t) {
109 if (done) {
110 RxJavaPlugins.onError(t);
111 return;
112 }
113 boolean reportError;
114 synchronized (this) {
115 if (done) {
116 reportError = true;
117 } else {
118 done = true;
119 if (emitting) {
120 AppendOnlyLinkedArrayList<Object> q = queue;
121 if (q == null) {
122 q = new AppendOnlyLinkedArrayList<>(4);
123 queue = q;
124 }
125 q.setFirst(NotificationLite.error(t));
126 return;
127 }
128 reportError = false;
129 emitting = true;
130 }
131 }
132 if (reportError) {
133 RxJavaPlugins.onError(t);
134 return;
135 }
136 actual.onError(t);
137 }
138
139 @Override
140 public void onComplete() {
141 if (done) {
142 return;
143 }
144 synchronized (this) {
145 if (done) {
146 return;
147 }
148 done = true;
149 if (emitting) {
150 AppendOnlyLinkedArrayList<Object> q = queue;
151 if (q == null) {
152 q = new AppendOnlyLinkedArrayList<>(4);
153 queue = q;
154 }
155 q.add(NotificationLite.complete());
156 return;
157 }
158 emitting = true;
159 }
160 actual.onComplete();
161 }
162
163 /** Loops until all notifications in the queue has been processed. */
164 void emitLoop() {
165 for (;;) {
166 AppendOnlyLinkedArrayList<Object> q;
167 synchronized (this) {
168 q = queue;
169 if (q == null) {
170 emitting = false;
171 return;
172 }
173 queue = null;
174 }
175
176 q.accept(actual);
177 }
178 }
179
180 @Override
181 public boolean hasSubscribers() {
182 return actual.hasSubscribers();
183 }
184
185 @Override
186 public boolean hasThrowable() {
187 return actual.hasThrowable();
188 }
189
190 @Override
191 @Nullable
192 public Throwable getThrowable() {
193 return actual.getThrowable();
194 }
195
196 @Override
197 public boolean hasComplete() {
198 return actual.hasComplete();
199 }
200 }