package kotlinx.coroutines.reactive;

/* JADX INFO: compiled from: r8-map-id-ae6aab8d9a1033945587ffdd1bc6222159522c1d66e66a2c4cecb065134dd0a6 */
/* JADX INFO: loaded from: E:\ios\vf_dex\classes3.dex */
@kotlinx.coroutines.InternalCoroutinesApi
@kotlin.Metadata(d1 = {"\u0000~\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\u0010\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u0000\n\u0002\b\u0007\n\u0002\u0010\u0003\n\u0002\b\u0005\n\u0002\u0010\u000b\n\u0002\b\u0006\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u0001\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0004\n\u0002\u0010\t\n\u0002\b\b\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0005\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0006\n\u0002\u0018\u0002\n\u0002\b\u0002\b\u0007\u0018\u0000*\u0006\b\u0000\u0010\u0001 \u00002\b\u0012\u0004\u0012\u00020\u00030\u00022\b\u0012\u0004\u0012\u00028\u00000\u00042\u00020\u0005J%\u0010\n\u001a\u00020\u00032\n\u0010\u0007\u001a\u0006\u0012\u0002\b\u00030\u00062\b\u0010\t\u001a\u0004\u0018\u00010\bH\u0002¢\u0006\u0004\b\n\u0010\u000bJ%\u0010\r\u001a\u0004\u0018\u00010\b2\b\u0010\t\u001a\u0004\u0018\u00010\b2\b\u0010\f\u001a\u0004\u0018\u00010\bH\u0002¢\u0006\u0004\b\r\u0010\u000eJ\u0019\u0010\u0011\u001a\u0004\u0018\u00010\u00102\u0006\u0010\u000f\u001a\u00028\u0000H\u0002¢\u0006\u0004\b\u0011\u0010\u0012J\u000f\u0010\u0013\u001a\u00020\u0003H\u0002¢\u0006\u0004\b\u0013\u0010\u0014J!\u0010\u0018\u001a\u00020\u00032\b\u0010\u0015\u001a\u0004\u0018\u00010\u00102\u0006\u0010\u0017\u001a\u00020\u0016H\u0002¢\u0006\u0004\b\u0018\u0010\u0019J!\u0010\u001a\u001a\u00020\u00032\b\u0010\u0015\u001a\u0004\u0018\u00010\u00102\u0006\u0010\u0017\u001a\u00020\u0016H\u0002¢\u0006\u0004\b\u001a\u0010\u0019J\u0019\u0010\u001b\u001a\u00020\u00162\b\u0010\u0015\u001a\u0004\u0018\u00010\u0010H\u0016¢\u0006\u0004\b\u001b\u0010\u001cJ%\u0010 \u001a\u00020\u001f2\u0014\u0010\u001e\u001a\u0010\u0012\u0006\u0012\u0004\u0018\u00010\u0010\u0012\u0004\u0012\u00020\u00030\u001dH\u0016¢\u0006\u0004\b \u0010!J\u001d\u0010#\u001a\b\u0012\u0004\u0012\u00020\u00030\"2\u0006\u0010\t\u001a\u00028\u0000H\u0016¢\u0006\u0004\b#\u0010$J\u0018\u0010%\u001a\u00020\u00032\u0006\u0010\t\u001a\u00028\u0000H\u0096@¢\u0006\u0004\b%\u0010&J\u0017\u0010)\u001a\u00020\u00032\u0006\u0010(\u001a\u00020'H\u0016¢\u0006\u0004\b)\u0010*J\u0017\u0010,\u001a\u00020\u00032\u0006\u0010+\u001a\u00020\u0003H\u0014¢\u0006\u0004\b,\u0010-J\u001f\u0010.\u001a\u00020\u00032\u0006\u0010\u0015\u001a\u00020\u00102\u0006\u0010\u0017\u001a\u00020\u0016H\u0014¢\u0006\u0004\b.\u0010\u0019J\u000f\u0010/\u001a\u00020\u0003H\u0016¢\u0006\u0004\b/\u0010\u0014R\u001a\u00103\u001a\b\u0012\u0004\u0012\u00028\u0000008\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b1\u00102R&\u00108\u001a\u0014\u0012\u0004\u0012\u00020\u0010\u0012\u0004\u0012\u000205\u0012\u0004\u0012\u00020\u0003048\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b6\u00107R\u0016\u00109\u001a\u00020\u00168\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b9\u0010:R\u0014\u0010>\u001a\u00020;8\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b<\u0010=R\u001a\u0010B\u001a\b\u0012\u0004\u0012\u00028\u00000?8VX\u0096\u0004¢\u0006\u0006\u001a\u0004\b@\u0010AR\u0014\u0010E\u001a\u00020\u00168VX\u0096\u0004¢\u0006\u0006\u001a\u0004\bC\u0010DR\u000b\u0010G\u001a\u00020F8\u0002X\u0082\u0004¨\u0006H"}, d2 = {"Lkotlinx/coroutines/reactive/PublisherCoroutine;", "T", "Lkotlinx/coroutines/AbstractCoroutine;", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "Lkotlinx/coroutines/channels/ProducerScope;", "Lorg/reactivestreams/Subscription;", "Lkotlinx/coroutines/selects/SelectInstance;", "select", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "element", "A1", "(Lkotlinx/coroutines/selects/SelectInstance;Ljava/lang/Object;)V", "selectResult", "z1", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", "elem", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "t1", "(Ljava/lang/Object;)Ljava/lang/Throwable;", "C1", "()V", "cause", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "handled", "u1", "(Ljava/lang/Throwable;Z)V", "B1", "p", "(Ljava/lang/Throwable;)Z", "Lkotlin/Function1;", "handler", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "x1", "(Lkotlin/jvm/functions/Function1;)Ljava/lang/Void;", "Lkotlinx/coroutines/channels/ChannelResult;", "g", "(Ljava/lang/Object;)Ljava/lang/Object;", "q", "(Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "n", "request", "(J)V", "value", "y1", "(Lkotlin/Unit;)V", "l1", "cancel", "Lorg/reactivestreams/Subscriber;", "d", "Lorg/reactivestreams/Subscriber;", "subscriber", "Lkotlin/Function2;", "Lkotlin/coroutines/CoroutineContext;", "e", "Lkotlin/jvm/functions/Function2;", "exceptionOnCancelHandler", "cancelled", "Z", "Lkotlinx/coroutines/sync/Mutex;", "f", "Lkotlinx/coroutines/sync/Mutex;", "mutex", "Lkotlinx/coroutines/channels/SendChannel;", "getChannel", "()Lkotlinx/coroutines/channels/SendChannel;", "channel", "m", "()Z", "isClosedForSend", "Lkotlinx/atomicfu/AtomicLong;", "_nRequested", "kotlinx-coroutines-reactive"}, k = 1, mv = {2, 1, 0}, xi = 48)
@kotlin.jvm.internal.SourceDebugExtension
public final class PublisherCoroutine<T> extends kotlinx.coroutines.AbstractCoroutine<kotlin.Unit> implements kotlinx.coroutines.channels.ProducerScope<T>, org.reactivestreams.Subscription {
    private static final /* synthetic */ java.util.concurrent.atomic.AtomicLongFieldUpdater g = java.util.concurrent.atomic.AtomicLongFieldUpdater.newUpdater(kotlinx.coroutines.reactive.PublisherCoroutine.class, "_nRequested$volatile");
    private volatile /* synthetic */ long _nRequested$volatile;
    private volatile boolean cancelled;

    /* JADX INFO: renamed from: d, reason: from kotlin metadata */
    private final org.reactivestreams.Subscriber subscriber;

    /* JADX INFO: renamed from: e, reason: from kotlin metadata */
    private final kotlin.jvm.functions.Function2 exceptionOnCancelHandler;

    /* JADX INFO: renamed from: f, reason: from kotlin metadata */
    private final kotlinx.coroutines.sync.Mutex mutex;

    /* JADX INFO: Access modifiers changed from: private */
    public final void A1(kotlinx.coroutines.selects.SelectInstance select, java.lang.Object element) {
        if (kotlinx.coroutines.sync.Mutex.DefaultImpls.b(this.mutex, null, 1, null)) {
            select.d(kotlin.Unit.a);
        } else {
            kotlinx.coroutines.BuildersKt.d(this, (kotlin.coroutines.CoroutineContext) null, (kotlinx.coroutines.CoroutineStart) null, new kotlinx.coroutines.reactive.PublisherCoroutine$registerSelectForSend$1(this, select, null), 3, (java.lang.Object) null);
        }
    }

    private final void B1(java.lang.Throwable cause, boolean handled) {
        while (true) {
            long j = g.get(this);
            if (j == -2) {
                return;
            }
            if (j < 0) {
                throw new java.lang.IllegalStateException("Check failed.");
            }
            kotlinx.coroutines.reactive.PublisherCoroutine<T> publisherCoroutine = this;
            if (g.compareAndSet(publisherCoroutine, j, -1L)) {
                if (j == 0) {
                    publisherCoroutine.u1(cause, handled);
                    return;
                } else {
                    if (kotlinx.coroutines.sync.Mutex.DefaultImpls.b(publisherCoroutine.mutex, null, 1, null)) {
                        publisherCoroutine.u1(cause, handled);
                        return;
                    }
                    return;
                }
            }
            this = publisherCoroutine;
        }
    }

    private final void C1() {
        kotlinx.coroutines.sync.Mutex.DefaultImpls.c(this.mutex, null, 1, null);
        if (v() && kotlinx.coroutines.sync.Mutex.DefaultImpls.b(this.mutex, null, 1, null)) {
            u1(h0(), i0());
        }
    }

    private final java.lang.Throwable t1(java.lang.Object elem) {
        kotlinx.coroutines.reactive.PublisherCoroutine<T> publisherCoroutine;
        if (elem == null) {
            C1();
            throw new java.lang.NullPointerException("Attempted to emit `null` inside a reactive publisher");
        }
        if (!isActive()) {
            C1();
            return o1();
        }
        try {
            this.subscriber.onNext(elem);
            while (true) {
                long j = g.get(this);
                if (j < 0 || j == Long.MAX_VALUE) {
                    break;
                }
                long j2 = j - 1;
                publisherCoroutine = this;
                if (!g.compareAndSet(publisherCoroutine, j, j2)) {
                    this = publisherCoroutine;
                } else if (j2 == 0) {
                    return null;
                }
                publisherCoroutine.C1();
                return null;
            }
            publisherCoroutine = this;
            publisherCoroutine.C1();
            return null;
        } catch (java.lang.Throwable th) {
            this.cancelled = true;
            boolean zP = p(th);
            C1();
            if (zP) {
                return th;
            }
            this.exceptionOnCancelHandler.invoke(th, getContext());
            return o1();
        }
    }

    private final void u1(java.lang.Throwable cause, boolean handled) {
        try {
            if (g.get(this) != -2) {
                g.set(this, -2L);
                if (this.cancelled) {
                    if (cause != null && !handled) {
                        this.exceptionOnCancelHandler.invoke(cause, getContext());
                    }
                } else if (cause == null) {
                    try {
                        this.subscriber.onComplete();
                    } catch (java.lang.Throwable th) {
                        kotlinx.coroutines.CoroutineExceptionHandlerKt.a(getContext(), th);
                    }
                } else {
                    try {
                        this.subscriber.onError(cause);
                    } catch (java.lang.Throwable th2) {
                        if (th2 != cause) {
                            kotlin.ExceptionsKt.a(cause, th2);
                        }
                        kotlinx.coroutines.CoroutineExceptionHandlerKt.a(getContext(), cause);
                    }
                }
            }
        } finally {
            kotlinx.coroutines.sync.Mutex.DefaultImpls.c(this.mutex, null, 1, null);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final java.lang.Object z1(java.lang.Object element, java.lang.Object selectResult) throws java.lang.Throwable {
        java.lang.Throwable thT1 = t1(element);
        if (thT1 == null) {
            return this;
        }
        throw thT1;
    }

    public void cancel() {
        this.cancelled = true;
        super/*kotlinx.coroutines.JobSupport*/.e((java.util.concurrent.CancellationException) null);
    }

    public java.lang.Object g(java.lang.Object element) {
        if (!kotlinx.coroutines.sync.Mutex.DefaultImpls.b(this.mutex, null, 1, null)) {
            return kotlinx.coroutines.channels.ChannelResult.b.b();
        }
        java.lang.Throwable thT1 = t1(element);
        return thT1 == null ? kotlinx.coroutines.channels.ChannelResult.b.c(kotlin.Unit.a) : kotlinx.coroutines.channels.ChannelResult.b.a(thT1);
    }

    protected void l1(java.lang.Throwable cause, boolean handled) {
        B1(cause, handled);
    }

    public boolean m() {
        return !isActive();
    }

    public boolean p(java.lang.Throwable cause) {
        return N(cause);
    }

    /* JADX WARN: Code duplicated, block: B:7:0x0013  */
    public java.lang.Object q(java.lang.Object obj, kotlin.coroutines.Continuation continuation) throws java.lang.Throwable {
        kotlinx.coroutines.reactive.PublisherCoroutine$send$1 publisherCoroutine$send$1;
        if (continuation instanceof kotlinx.coroutines.reactive.PublisherCoroutine$send$1) {
            publisherCoroutine$send$1 = (kotlinx.coroutines.reactive.PublisherCoroutine$send$1) continuation;
            int i = publisherCoroutine$send$1.r;
            if ((i & Integer.MIN_VALUE) != 0) {
                publisherCoroutine$send$1.r = i - Integer.MIN_VALUE;
            } else {
                publisherCoroutine$send$1 = new kotlinx.coroutines.reactive.PublisherCoroutine$send$1(this, continuation);
            }
        } else {
            publisherCoroutine$send$1 = new kotlinx.coroutines.reactive.PublisherCoroutine$send$1(this, continuation);
        }
        java.lang.Object obj2 = publisherCoroutine$send$1.p;
        java.lang.Object objG = kotlin.coroutines.intrinsics.IntrinsicsKt.g();
        int i2 = publisherCoroutine$send$1.r;
        if (i2 == 0) {
            kotlin.ResultKt.b(obj2);
            kotlinx.coroutines.sync.Mutex mutex = this.mutex;
            publisherCoroutine$send$1.n = this;
            publisherCoroutine$send$1.o = obj;
            publisherCoroutine$send$1.r = 1;
            if (kotlinx.coroutines.sync.Mutex.DefaultImpls.a(mutex, null, publisherCoroutine$send$1, 1, null) == objG) {
                return objG;
            }
        } else {
            if (i2 != 1) {
                throw new java.lang.IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            obj = publisherCoroutine$send$1.o;
            this = (kotlinx.coroutines.reactive.PublisherCoroutine) publisherCoroutine$send$1.n;
            kotlin.ResultKt.b(obj2);
        }
        java.lang.Throwable thT1 = this.t1(obj);
        if (thT1 == null) {
            return kotlin.Unit.a;
        }
        throw thT1;
    }

    public void request(long n) {
        if (n <= 0) {
            N(new java.lang.IllegalArgumentException("non-positive subscription request " + n));
            return;
        }
        while (true) {
            long j = g.get(this);
            if (j < 0) {
                return;
            }
            long j2 = j + n;
            long j5 = (j2 < 0 || n == Long.MAX_VALUE) ? Long.MAX_VALUE : j2;
            if (j == j5) {
                return;
            }
            kotlinx.coroutines.reactive.PublisherCoroutine<T> publisherCoroutine = this;
            if (g.compareAndSet(publisherCoroutine, j, j5)) {
                if (j == 0) {
                    publisherCoroutine.C1();
                    return;
                }
                return;
            }
            this = publisherCoroutine;
        }
    }

    /* JADX INFO: renamed from: x1, reason: merged with bridge method [inline-methods] */
    public java.lang.Void f(kotlin.jvm.functions.Function1 handler) {
        throw new java.lang.UnsupportedOperationException("PublisherCoroutine doesn't support invokeOnClose");
    }

    /* JADX INFO: Access modifiers changed from: protected */
    /* JADX INFO: renamed from: y1, reason: merged with bridge method [inline-methods] */
    public void m1(kotlin.Unit value) {
        B1(null, false);
    }

    public kotlinx.coroutines.channels.SendChannel getChannel() {
        return this;
    }
}
