package kotlinx.coroutines.reactive;

/* JADX INFO: compiled from: r8-map-id-ae6aab8d9a1033945587ffdd1bc6222159522c1d66e66a2c4cecb065134dd0a6 */
/* JADX INFO: loaded from: E:\ios\vf_dex\classes3.dex */
@kotlin.Metadata(d1 = {"\u0000<\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0010\b\n\u0002\b\u0003\n\u0002\u0010\u0002\n\u0002\b\u0004\n\u0002\u0018\u0002\n\u0002\b\u0007\n\u0002\u0010\u0003\n\u0002\b\u0005\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\b\u0002\u0018\u0000*\u0004\b\u0000\u0010\u00012\b\u0012\u0004\u0012\u00028\u00000\u00022\b\u0012\u0004\u0012\u00028\u00000\u0003B\u000f\u0012\u0006\u0010\u0005\u001a\u00020\u0004¢\u0006\u0004\b\u0006\u0010\u0007J\u000f\u0010\t\u001a\u00020\bH\u0014¢\u0006\u0004\b\t\u0010\nJ\u000f\u0010\u000b\u001a\u00020\bH\u0014¢\u0006\u0004\b\u000b\u0010\nJ\u000f\u0010\f\u001a\u00020\bH\u0014¢\u0006\u0004\b\f\u0010\nJ\u0017\u0010\u000f\u001a\u00020\b2\u0006\u0010\u000e\u001a\u00020\rH\u0016¢\u0006\u0004\b\u000f\u0010\u0010J\u0017\u0010\u0012\u001a\u00020\b2\u0006\u0010\u0011\u001a\u00028\u0000H\u0016¢\u0006\u0004\b\u0012\u0010\u0013J\u000f\u0010\u0014\u001a\u00020\bH\u0016¢\u0006\u0004\b\u0014\u0010\nJ\u0017\u0010\u0017\u001a\u00020\b2\u0006\u0010\u0016\u001a\u00020\u0015H\u0016¢\u0006\u0004\b\u0017\u0010\u0018R\u0014\u0010\u0005\u001a\u00020\u00048\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\u0019\u0010\u001aR\u0013\u0010\u001c\u001a\n\u0012\u0006\u0012\u0004\u0018\u00010\r0\u001b8\u0002X\u0082\u0004R\u000b\u0010\u001e\u001a\u00020\u001d8\u0002X\u0082\u0004¨\u0006\u001f"}, d2 = {"Lkotlinx/coroutines/reactive/SubscriptionChannel;", "T", "Lkotlinx/coroutines/channels/BufferedChannel;", "Lorg/reactivestreams/Subscriber;", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "request", "<init>", "(I)V", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "X0", "()V", "W0", "P0", "Lorg/reactivestreams/Subscription;", "s", "onSubscribe", "(Lorg/reactivestreams/Subscription;)V", "t", "onNext", "(Ljava/lang/Object;)V", "onComplete", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "e", "onError", "(Ljava/lang/Throwable;)V", "m", "I", "Lkotlinx/atomicfu/AtomicRef;", "_subscription", "Lkotlinx/atomicfu/AtomicInt;", "_requested", "kotlinx-coroutines-reactive"}, k = 1, mv = {2, 1, 0}, xi = 48)
@kotlin.jvm.internal.SourceDebugExtension
final class SubscriptionChannel<T> extends kotlinx.coroutines.channels.BufferedChannel<T> implements org.reactivestreams.Subscriber<T> {
    private static final /* synthetic */ java.util.concurrent.atomic.AtomicReferenceFieldUpdater n = java.util.concurrent.atomic.AtomicReferenceFieldUpdater.newUpdater(kotlinx.coroutines.reactive.SubscriptionChannel.class, java.lang.Object.class, "_subscription$volatile");
    private static final /* synthetic */ java.util.concurrent.atomic.AtomicIntegerFieldUpdater o = java.util.concurrent.atomic.AtomicIntegerFieldUpdater.newUpdater(kotlinx.coroutines.reactive.SubscriptionChannel.class, "_requested$volatile");
    private volatile /* synthetic */ int _requested$volatile;
    private volatile /* synthetic */ java.lang.Object _subscription$volatile;

    /* JADX INFO: renamed from: m, reason: from kotlin metadata */
    private final int request;

    public SubscriptionChannel(int i) {
        super(Integer.MAX_VALUE, (kotlin.jvm.functions.Function1) null, 2, (kotlin.jvm.internal.DefaultConstructorMarker) null);
        this.request = i;
        if (i >= 0) {
            return;
        }
        throw new java.lang.IllegalArgumentException(("Invalid request size: " + i).toString());
    }

    protected void P0() {
        org.reactivestreams.Subscription subscription = (org.reactivestreams.Subscription) n.getAndSet(this, null);
        if (subscription != null) {
            subscription.cancel();
        }
    }

    protected void W0() {
        o.incrementAndGet(this);
    }

    protected void X0() {
        org.reactivestreams.Subscription subscription;
        int i;
        java.util.concurrent.atomic.AtomicIntegerFieldUpdater atomicIntegerFieldUpdater = o;
        while (true) {
            int i2 = atomicIntegerFieldUpdater.get(this);
            subscription = (org.reactivestreams.Subscription) n.get(this);
            i = i2 - 1;
            if (subscription == null || i >= 0) {
                if (o.compareAndSet(this, i2, i)) {
                    return;
                }
            } else if (i2 == this.request || o.compareAndSet(this, i2, this.request)) {
                break;
            }
        }
        subscription.request(this.request - i);
    }

    public void onComplete() {
        p((java.lang.Throwable) null);
    }

    public void onError(java.lang.Throwable e) {
        p(e);
    }

    public void onNext(java.lang.Object t) {
        o.decrementAndGet(this);
        g(t);
    }

    public void onSubscribe(org.reactivestreams.Subscription s) {
        n.set(this, s);
        while (!m()) {
            int i = o.get(this);
            if (i >= this.request) {
                return;
            }
            if (o.compareAndSet(this, i, this.request)) {
                s.request(this.request - i);
                return;
            }
        }
        s.cancel();
    }
}
