187 lines
5.7 KiB
Java
187 lines
5.7 KiB
Java
package io.reactivex.internal.subscriptions;
|
|
|
|
import io.reactivex.internal.functions.ObjectHelper;
|
|
import io.reactivex.internal.util.BackpressureHelper;
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
import java.util.concurrent.atomic.AtomicLong;
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
import org.reactivestreams.Subscription;
|
|
|
|
/* loaded from: classes2.dex */
|
|
public class SubscriptionArbiter extends AtomicInteger implements Subscription {
|
|
private static final long serialVersionUID = -2189523197179400958L;
|
|
Subscription actual;
|
|
volatile boolean cancelled;
|
|
long requested;
|
|
protected boolean unbounded;
|
|
final AtomicReference<Subscription> missedSubscription = new AtomicReference<>();
|
|
final AtomicLong missedRequested = new AtomicLong();
|
|
final AtomicLong missedProduced = new AtomicLong();
|
|
|
|
@Override // org.reactivestreams.Subscription
|
|
public void cancel() {
|
|
if (this.cancelled) {
|
|
return;
|
|
}
|
|
this.cancelled = true;
|
|
drain();
|
|
}
|
|
|
|
final void drain() {
|
|
if (getAndIncrement() != 0) {
|
|
return;
|
|
}
|
|
drainLoop();
|
|
}
|
|
|
|
final void drainLoop() {
|
|
Subscription subscription = null;
|
|
long j = 0;
|
|
int i = 1;
|
|
do {
|
|
Subscription subscription2 = this.missedSubscription.get();
|
|
if (subscription2 != null) {
|
|
subscription2 = this.missedSubscription.getAndSet(null);
|
|
}
|
|
long j2 = this.missedRequested.get();
|
|
if (j2 != 0) {
|
|
j2 = this.missedRequested.getAndSet(0L);
|
|
}
|
|
long j3 = this.missedProduced.get();
|
|
if (j3 != 0) {
|
|
j3 = this.missedProduced.getAndSet(0L);
|
|
}
|
|
Subscription subscription3 = this.actual;
|
|
if (this.cancelled) {
|
|
if (subscription3 != null) {
|
|
subscription3.cancel();
|
|
this.actual = null;
|
|
}
|
|
if (subscription2 != null) {
|
|
subscription2.cancel();
|
|
}
|
|
} else {
|
|
long j4 = this.requested;
|
|
if (j4 != Long.MAX_VALUE) {
|
|
j4 = BackpressureHelper.a(j4, j2);
|
|
if (j4 != Long.MAX_VALUE) {
|
|
long j5 = j4 - j3;
|
|
if (j5 < 0) {
|
|
SubscriptionHelper.reportMoreProduced(j5);
|
|
j5 = 0;
|
|
}
|
|
j4 = j5;
|
|
}
|
|
this.requested = j4;
|
|
}
|
|
if (subscription2 != null) {
|
|
if (subscription3 != null) {
|
|
subscription3.cancel();
|
|
}
|
|
this.actual = subscription2;
|
|
if (j4 != 0) {
|
|
j = BackpressureHelper.a(j, j4);
|
|
subscription = subscription2;
|
|
}
|
|
} else if (subscription3 != null && j2 != 0) {
|
|
j = BackpressureHelper.a(j, j2);
|
|
subscription = subscription3;
|
|
}
|
|
}
|
|
i = addAndGet(-i);
|
|
} while (i != 0);
|
|
if (j != 0) {
|
|
subscription.request(j);
|
|
}
|
|
}
|
|
|
|
public final boolean isCancelled() {
|
|
return this.cancelled;
|
|
}
|
|
|
|
public final boolean isUnbounded() {
|
|
return this.unbounded;
|
|
}
|
|
|
|
public final void produced(long j) {
|
|
if (this.unbounded) {
|
|
return;
|
|
}
|
|
if (get() != 0 || !compareAndSet(0, 1)) {
|
|
BackpressureHelper.a(this.missedProduced, j);
|
|
drain();
|
|
return;
|
|
}
|
|
long j2 = this.requested;
|
|
if (j2 != Long.MAX_VALUE) {
|
|
long j3 = j2 - j;
|
|
long j4 = 0;
|
|
if (j3 < 0) {
|
|
SubscriptionHelper.reportMoreProduced(j3);
|
|
} else {
|
|
j4 = j3;
|
|
}
|
|
this.requested = j4;
|
|
}
|
|
if (decrementAndGet() == 0) {
|
|
return;
|
|
}
|
|
drainLoop();
|
|
}
|
|
|
|
@Override // org.reactivestreams.Subscription
|
|
public final void request(long j) {
|
|
if (!SubscriptionHelper.validate(j) || this.unbounded) {
|
|
return;
|
|
}
|
|
if (get() != 0 || !compareAndSet(0, 1)) {
|
|
BackpressureHelper.a(this.missedRequested, j);
|
|
drain();
|
|
return;
|
|
}
|
|
long j2 = this.requested;
|
|
if (j2 != Long.MAX_VALUE) {
|
|
long a = BackpressureHelper.a(j2, j);
|
|
this.requested = a;
|
|
if (a == Long.MAX_VALUE) {
|
|
this.unbounded = true;
|
|
}
|
|
}
|
|
Subscription subscription = this.actual;
|
|
if (decrementAndGet() != 0) {
|
|
drainLoop();
|
|
}
|
|
if (subscription != null) {
|
|
subscription.request(j);
|
|
}
|
|
}
|
|
|
|
public final void setSubscription(Subscription subscription) {
|
|
if (this.cancelled) {
|
|
subscription.cancel();
|
|
return;
|
|
}
|
|
ObjectHelper.a(subscription, "s is null");
|
|
if (get() != 0 || !compareAndSet(0, 1)) {
|
|
Subscription andSet = this.missedSubscription.getAndSet(subscription);
|
|
if (andSet != null) {
|
|
andSet.cancel();
|
|
}
|
|
drain();
|
|
return;
|
|
}
|
|
Subscription subscription2 = this.actual;
|
|
if (subscription2 != null) {
|
|
subscription2.cancel();
|
|
}
|
|
this.actual = subscription;
|
|
long j = this.requested;
|
|
if (decrementAndGet() != 0) {
|
|
drainLoop();
|
|
}
|
|
if (j != 0) {
|
|
subscription.request(j);
|
|
}
|
|
}
|
|
}
|