Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/observers/QueueDrainObserver.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.observers;
15
16 import java.util.concurrent.atomic.AtomicInteger;
17
18 import io.reactivex.rxjava3.core.Observer;
19 import io.reactivex.rxjava3.disposables.Disposable;
20 import io.reactivex.rxjava3.internal.util.*;
21 import io.reactivex.rxjava3.operators.SimplePlainQueue;
22
23 /**
24 * Abstract base class for subscribers that hold another subscriber, a queue
25 * and requires queue-drain behavior.
26 *
27 * @param <T> the source type to which this subscriber will be subscribed
28 * @param <U> the value type in the queue
29 * @param <V> the value type the child subscriber accepts
30 */
31 public abstract class QueueDrainObserver<T, U, V> extends QueueDrainSubscriberPad2 implements Observer<T>, ObservableQueueDrain<U, V> {
32 protected final Observer<? super V> downstream;
33 protected final SimplePlainQueue<U> queue;
34
35 protected volatile boolean cancelled;
36
37 protected volatile boolean done;
38 protected Throwable error;
39
40 public QueueDrainObserver(Observer<? super V> actual, SimplePlainQueue<U> queue) {
41 this.downstream = actual;
42 this.queue = queue;
43 }
44
45 @Override
46 public final boolean cancelled() {
47 return cancelled;
48 }
49
50 @Override
51 public final boolean done() {
52 return done;
53 }
54
55 @Override
56 public final boolean enter() {
57 return wip.getAndIncrement() == 0;
58 }
59
60 protected final void fastPathEmit(U value, boolean delayError, Disposable dispose) {
61 final Observer<? super V> observer = downstream;
62 final SimplePlainQueue<U> q = queue;
63
64 if (wip.get() == 0 && wip.compareAndSet(0, 1)) {
65 accept(observer, value);
66 if (leave(-1) == 0) {
67 return;
68 }
69 } else {
70 q.offer(value);
71 if (!enter()) {
72 return;
73 }
74 }
75 QueueDrainHelper.drainLoop(q, observer, delayError, dispose, this);
76 }
77
78 /**
79 * Makes sure the fast-path emits in order.
80 * @param value the value to emit or queue up
81 * @param delayError if true, errors are delayed until the source has terminated
82 * @param disposable the resource to dispose if the drain terminates
83 */
84 protected final void fastPathOrderedEmit(U value, boolean delayError, Disposable disposable) {
85 final Observer<? super V> observer = downstream;
86 final SimplePlainQueue<U> q = queue;
87
88 if (wip.get() == 0 && wip.compareAndSet(0, 1)) {
89 if (q.isEmpty()) {
90 accept(observer, value);
91 if (leave(-1) == 0) {
92 return;
93 }
94 } else {
95 q.offer(value);
96 }
97 } else {
98 q.offer(value);
99 if (!enter()) {
100 return;
101 }
102 }
103 QueueDrainHelper.drainLoop(q, observer, delayError, disposable, this);
104 }
105
106 @Override
107 public final Throwable error() {
108 return error;
109 }
110
111 @Override
112 public final int leave(int m) {
113 return wip.addAndGet(m);
114 }
115
116 @Override
117 public void accept(Observer<? super V> a, U v) {
118 // ignored by default
119 }
120 }
121
122 // -------------------------------------------------------------------
123 // Padding superclasses
124 //-------------------------------------------------------------------
125
126 /** Pads the header away from other fields. */
127 class QueueDrainSubscriberPad0 {
128 volatile long p1, p2, p3, p4, p5, p6, p7;
129 volatile long p8, p9, p10, p11, p12, p13, p14, p15;
130 }
131
132 /** The wip counter. */
133 class QueueDrainSubscriberWip extends QueueDrainSubscriberPad0 {
134 final AtomicInteger wip = new AtomicInteger();
135 }
136
137 /** Pads away the wip from the other fields. */
138 class QueueDrainSubscriberPad2 extends QueueDrainSubscriberWip {
139 volatile long p1a, p2a, p3a, p4a, p5a, p6a, p7a;
140 volatile long p8a, p9a, p10a, p11a, p12a, p13a, p14a, p15a;
141 }
142