package kotlinx.coroutines; import androidx.concurrent.futures.AbstractResolvableFuture$SafeAtomicHelper$$ExternalSyntheticBackportWithForwarding0; import com.facebook.internal.AnalyticsEvents; import com.google.android.gms.drive.DriveFile; import java.util.concurrent.CancellationException; import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; import kotlin.KotlinNothingValueException; import kotlin.coroutines.Continuation; import kotlin.coroutines.CoroutineContext; import kotlin.coroutines.intrinsics.IntrinsicsKt__IntrinsicsKt; import kotlin.coroutines.jvm.internal.CoroutineStackFrame; import kotlin.jvm.functions.Function1; import kotlin.jvm.internal.Intrinsics; import kotlinx.coroutines.Job; import kotlinx.coroutines.internal.DispatchedContinuation; import kotlinx.coroutines.internal.Segment; import kotlinx.coroutines.internal.Symbol; /* loaded from: classes5.dex */ public class CancellableContinuationImpl extends DispatchedTask implements CancellableContinuation, CoroutineStackFrame, Waiter { private volatile int _decisionAndIndex; private volatile Object _parentHandle; private volatile Object _state; public final CoroutineContext context; public final Continuation delegate; public static final AtomicIntegerFieldUpdater _decisionAndIndex$FU = AtomicIntegerFieldUpdater.newUpdater(CancellableContinuationImpl.class, "_decisionAndIndex"); public static final AtomicReferenceFieldUpdater _state$FU = AtomicReferenceFieldUpdater.newUpdater(CancellableContinuationImpl.class, Object.class, "_state"); public static final AtomicReferenceFieldUpdater _parentHandle$FU = AtomicReferenceFieldUpdater.newUpdater(CancellableContinuationImpl.class, Object.class, "_parentHandle"); @Override // kotlin.coroutines.Continuation public CoroutineContext getContext() { return this.context; } @Override // kotlinx.coroutines.DispatchedTask public final Continuation getDelegate$kotlinx_coroutines_core() { return this.delegate; } public String nameString() { return "CancellableContinuation"; } public CancellableContinuationImpl(Continuation continuation, int i) { super(i); this.delegate = continuation; this.context = continuation.getContext(); this._decisionAndIndex = 536870911; this._state = Active.INSTANCE; } public final DisposableHandle getParentHandle() { return (DisposableHandle) _parentHandle$FU.get(this); } public final Object getState$kotlinx_coroutines_core() { return _state$FU.get(this); } @Override // kotlinx.coroutines.CancellableContinuation public boolean isActive() { return getState$kotlinx_coroutines_core() instanceof NotCompleted; } @Override // kotlinx.coroutines.CancellableContinuation public boolean isCompleted() { return !(getState$kotlinx_coroutines_core() instanceof NotCompleted); } public final String getStateDebugRepresentation() { Object state$kotlinx_coroutines_core = getState$kotlinx_coroutines_core(); return state$kotlinx_coroutines_core instanceof NotCompleted ? "Active" : state$kotlinx_coroutines_core instanceof CancelledContinuation ? AnalyticsEvents.PARAMETER_DIALOG_OUTCOME_VALUE_CANCELLED : AnalyticsEvents.PARAMETER_DIALOG_OUTCOME_VALUE_COMPLETED; } public void initCancellability() { DisposableHandle installParentHandle = installParentHandle(); if (installParentHandle != null && isCompleted()) { installParentHandle.dispose(); _parentHandle$FU.set(this, NonDisposableHandle.INSTANCE); } } public final boolean isReusable() { if (DispatchedTaskKt.isReusableMode(this.resumeMode)) { Continuation continuation = this.delegate; Intrinsics.checkNotNull(continuation, "null cannot be cast to non-null type kotlinx.coroutines.internal.DispatchedContinuation<*>"); if (((DispatchedContinuation) continuation).isReusable$kotlinx_coroutines_core()) { return true; } } return false; } public final boolean resetStateReusable() { AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = _state$FU; Object obj = atomicReferenceFieldUpdater.get(this); if (!(obj instanceof CompletedContinuation) || ((CompletedContinuation) obj).idempotentResume == null) { _decisionAndIndex$FU.set(this, 536870911); atomicReferenceFieldUpdater.set(this, Active.INSTANCE); return true; } detachChild$kotlinx_coroutines_core(); return false; } @Override // kotlin.coroutines.jvm.internal.CoroutineStackFrame public CoroutineStackFrame getCallerFrame() { Continuation continuation = this.delegate; if (continuation instanceof CoroutineStackFrame) { return (CoroutineStackFrame) continuation; } return null; } @Override // kotlinx.coroutines.DispatchedTask public Object takeState$kotlinx_coroutines_core() { return getState$kotlinx_coroutines_core(); } @Override // kotlinx.coroutines.DispatchedTask public void cancelCompletedResult$kotlinx_coroutines_core(Object obj, Throwable th) { AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = _state$FU; while (true) { Object obj2 = atomicReferenceFieldUpdater.get(this); if (obj2 instanceof NotCompleted) { throw new IllegalStateException("Not completed".toString()); } if (obj2 instanceof CompletedExceptionally) { return; } if (obj2 instanceof CompletedContinuation) { CompletedContinuation completedContinuation = (CompletedContinuation) obj2; if (!(!completedContinuation.getCancelled())) { throw new IllegalStateException("Must be called at most once".toString()); } if (AbstractResolvableFuture$SafeAtomicHelper$$ExternalSyntheticBackportWithForwarding0.m(_state$FU, this, obj2, CompletedContinuation.copy$default(completedContinuation, null, null, null, null, th, 15, null))) { completedContinuation.invokeHandlers(this, th); return; } } else if (AbstractResolvableFuture$SafeAtomicHelper$$ExternalSyntheticBackportWithForwarding0.m(_state$FU, this, obj2, new CompletedContinuation(obj2, null, null, null, th, 14, null))) { return; } } } public final boolean cancelLater(Throwable th) { if (!isReusable()) { return false; } Continuation continuation = this.delegate; Intrinsics.checkNotNull(continuation, "null cannot be cast to non-null type kotlinx.coroutines.internal.DispatchedContinuation<*>"); return ((DispatchedContinuation) continuation).postponeCancellation$kotlinx_coroutines_core(th); } @Override // kotlinx.coroutines.CancellableContinuation public boolean cancel(Throwable th) { Object obj; AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = _state$FU; do { obj = atomicReferenceFieldUpdater.get(this); if (!(obj instanceof NotCompleted)) { return false; } } while (!AbstractResolvableFuture$SafeAtomicHelper$$ExternalSyntheticBackportWithForwarding0.m(_state$FU, this, obj, new CancelledContinuation(this, th, (obj instanceof CancelHandler) || (obj instanceof Segment)))); NotCompleted notCompleted = (NotCompleted) obj; if (notCompleted instanceof CancelHandler) { callCancelHandler((CancelHandler) obj, th); } else if (notCompleted instanceof Segment) { callSegmentOnCancellation((Segment) obj, th); } detachChildIfNonResuable(); dispatchResume(this.resumeMode); return true; } public final void parentCancelled$kotlinx_coroutines_core(Throwable th) { if (cancelLater(th)) { return; } cancel(th); detachChildIfNonResuable(); } public final void callCancelHandler(CancelHandler cancelHandler, Throwable th) { try { cancelHandler.invoke(th); } catch (Throwable th2) { CoroutineExceptionHandlerKt.handleCoroutineException(getContext(), new CompletionHandlerException("Exception in invokeOnCancellation handler for " + this, th2)); } } public final void callSegmentOnCancellation(Segment segment, Throwable th) { int i = _decisionAndIndex$FU.get(this) & 536870911; if (i == 536870911) { throw new IllegalStateException("The index for Segment.onCancellation(..) is broken".toString()); } try { segment.onCancellation(i, th, getContext()); } catch (Throwable th2) { CoroutineExceptionHandlerKt.handleCoroutineException(getContext(), new CompletionHandlerException("Exception in invokeOnCancellation handler for " + this, th2)); } } public final void callOnCancellation(Function1 function1, Throwable th) { try { function1.invoke(th); } catch (Throwable th2) { CoroutineExceptionHandlerKt.handleCoroutineException(getContext(), new CompletionHandlerException("Exception in resume onCancellation handler for " + this, th2)); } } public Throwable getContinuationCancellationCause(Job job) { return job.getCancellationException(); } public final boolean trySuspend() { int i; AtomicIntegerFieldUpdater atomicIntegerFieldUpdater = _decisionAndIndex$FU; do { i = atomicIntegerFieldUpdater.get(this); int i2 = i >> 29; if (i2 != 0) { if (i2 == 2) { return false; } throw new IllegalStateException("Already suspended".toString()); } } while (!_decisionAndIndex$FU.compareAndSet(this, i, DriveFile.MODE_WRITE_ONLY + (536870911 & i))); return true; } public final boolean tryResume() { int i; AtomicIntegerFieldUpdater atomicIntegerFieldUpdater = _decisionAndIndex$FU; do { i = atomicIntegerFieldUpdater.get(this); int i2 = i >> 29; if (i2 != 0) { if (i2 == 1) { return false; } throw new IllegalStateException("Already resumed".toString()); } } while (!_decisionAndIndex$FU.compareAndSet(this, i, 1073741824 + (536870911 & i))); return true; } public final Object getResult() { Job job; Object coroutine_suspended; boolean isReusable = isReusable(); if (trySuspend()) { if (getParentHandle() == null) { installParentHandle(); } if (isReusable) { releaseClaimedReusableContinuation$kotlinx_coroutines_core(); } coroutine_suspended = IntrinsicsKt__IntrinsicsKt.getCOROUTINE_SUSPENDED(); return coroutine_suspended; } if (isReusable) { releaseClaimedReusableContinuation$kotlinx_coroutines_core(); } Object state$kotlinx_coroutines_core = getState$kotlinx_coroutines_core(); if (state$kotlinx_coroutines_core instanceof CompletedExceptionally) { throw ((CompletedExceptionally) state$kotlinx_coroutines_core).cause; } if (DispatchedTaskKt.isCancellableMode(this.resumeMode) && (job = (Job) getContext().get(Job.Key)) != null && !job.isActive()) { CancellationException cancellationException = job.getCancellationException(); cancelCompletedResult$kotlinx_coroutines_core(state$kotlinx_coroutines_core, cancellationException); throw cancellationException; } return getSuccessfulResult$kotlinx_coroutines_core(state$kotlinx_coroutines_core); } public final DisposableHandle installParentHandle() { Job job = (Job) getContext().get(Job.Key); if (job == null) { return null; } DisposableHandle invokeOnCompletion$default = Job.DefaultImpls.invokeOnCompletion$default(job, true, false, new ChildContinuation(this), 2, null); AbstractResolvableFuture$SafeAtomicHelper$$ExternalSyntheticBackportWithForwarding0.m(_parentHandle$FU, this, null, invokeOnCompletion$default); return invokeOnCompletion$default; } public final void releaseClaimedReusableContinuation$kotlinx_coroutines_core() { Throwable tryReleaseClaimedContinuation$kotlinx_coroutines_core; Continuation continuation = this.delegate; DispatchedContinuation dispatchedContinuation = continuation instanceof DispatchedContinuation ? (DispatchedContinuation) continuation : null; if (dispatchedContinuation == null || (tryReleaseClaimedContinuation$kotlinx_coroutines_core = dispatchedContinuation.tryReleaseClaimedContinuation$kotlinx_coroutines_core(this)) == null) { return; } detachChild$kotlinx_coroutines_core(); cancel(tryReleaseClaimedContinuation$kotlinx_coroutines_core); } @Override // kotlin.coroutines.Continuation public void resumeWith(Object obj) { resumeImpl$default(this, CompletionStateKt.toState(obj, this), this.resumeMode, null, 4, null); } @Override // kotlinx.coroutines.CancellableContinuation public void resume(Object obj, Function1 function1) { resumeImpl(obj, this.resumeMode, function1); } @Override // kotlinx.coroutines.Waiter public void invokeOnCancellation(Segment segment, int i) { int i2; AtomicIntegerFieldUpdater atomicIntegerFieldUpdater = _decisionAndIndex$FU; do { i2 = atomicIntegerFieldUpdater.get(this); if ((i2 & 536870911) != 536870911) { throw new IllegalStateException("invokeOnCancellation should be called at most once".toString()); } } while (!atomicIntegerFieldUpdater.compareAndSet(this, i2, ((i2 >> 29) << 29) + i)); invokeOnCancellationImpl(segment); } @Override // kotlinx.coroutines.CancellableContinuation public void invokeOnCancellation(Function1 function1) { invokeOnCancellationImpl(makeCancelHandler(function1)); } public final void invokeOnCancellationImpl(Object obj) { AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = _state$FU; while (true) { Object obj2 = atomicReferenceFieldUpdater.get(this); if (obj2 instanceof Active) { if (AbstractResolvableFuture$SafeAtomicHelper$$ExternalSyntheticBackportWithForwarding0.m(_state$FU, this, obj2, obj)) { return; } } else if ((obj2 instanceof CancelHandler) || (obj2 instanceof Segment)) { multipleHandlersError(obj, obj2); } else { boolean z = obj2 instanceof CompletedExceptionally; if (z) { CompletedExceptionally completedExceptionally = (CompletedExceptionally) obj2; if (!completedExceptionally.makeHandled()) { multipleHandlersError(obj, obj2); } if (obj2 instanceof CancelledContinuation) { if (!z) { completedExceptionally = null; } Throwable th = completedExceptionally != null ? completedExceptionally.cause : null; if (obj instanceof CancelHandler) { callCancelHandler((CancelHandler) obj, th); return; } else { Intrinsics.checkNotNull(obj, "null cannot be cast to non-null type kotlinx.coroutines.internal.Segment<*>"); callSegmentOnCancellation((Segment) obj, th); return; } } return; } if (obj2 instanceof CompletedContinuation) { CompletedContinuation completedContinuation = (CompletedContinuation) obj2; if (completedContinuation.cancelHandler != null) { multipleHandlersError(obj, obj2); } if (obj instanceof Segment) { return; } Intrinsics.checkNotNull(obj, "null cannot be cast to non-null type kotlinx.coroutines.CancelHandler"); CancelHandler cancelHandler = (CancelHandler) obj; if (completedContinuation.getCancelled()) { callCancelHandler(cancelHandler, completedContinuation.cancelCause); return; } else { if (AbstractResolvableFuture$SafeAtomicHelper$$ExternalSyntheticBackportWithForwarding0.m(_state$FU, this, obj2, CompletedContinuation.copy$default(completedContinuation, null, cancelHandler, null, null, null, 29, null))) { return; } } } else { if (obj instanceof Segment) { return; } Intrinsics.checkNotNull(obj, "null cannot be cast to non-null type kotlinx.coroutines.CancelHandler"); if (AbstractResolvableFuture$SafeAtomicHelper$$ExternalSyntheticBackportWithForwarding0.m(_state$FU, this, obj2, new CompletedContinuation(obj2, (CancelHandler) obj, null, null, null, 28, null))) { return; } } } } } public final void multipleHandlersError(Object obj, Object obj2) { throw new IllegalStateException(("It's prohibited to register multiple handlers, tried to register " + obj + ", already has " + obj2).toString()); } public final CancelHandler makeCancelHandler(Function1 function1) { return function1 instanceof CancelHandler ? (CancelHandler) function1 : new InvokeOnCancel(function1); } public final void dispatchResume(int i) { if (tryResume()) { return; } DispatchedTaskKt.dispatch(this, i); } public final Object resumedState(NotCompleted notCompleted, Object obj, int i, Function1 function1, Object obj2) { if (obj instanceof CompletedExceptionally) { return obj; } if (!DispatchedTaskKt.isCancellableMode(i) && obj2 == null) { return obj; } if (function1 == null && !(notCompleted instanceof CancelHandler) && obj2 == null) { return obj; } return new CompletedContinuation(obj, notCompleted instanceof CancelHandler ? (CancelHandler) notCompleted : null, function1, obj2, null, 16, null); } public static /* synthetic */ void resumeImpl$default(CancellableContinuationImpl cancellableContinuationImpl, Object obj, int i, Function1 function1, int i2, Object obj2) { if (obj2 != null) { throw new UnsupportedOperationException("Super calls with default arguments not supported in this target, function: resumeImpl"); } if ((i2 & 4) != 0) { function1 = null; } cancellableContinuationImpl.resumeImpl(obj, i, function1); } public final void resumeImpl(Object obj, int i, Function1 function1) { Object obj2; AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = _state$FU; do { obj2 = atomicReferenceFieldUpdater.get(this); if (obj2 instanceof NotCompleted) { } else { if (obj2 instanceof CancelledContinuation) { CancelledContinuation cancelledContinuation = (CancelledContinuation) obj2; if (cancelledContinuation.makeResumed()) { if (function1 != null) { callOnCancellation(function1, cancelledContinuation.cause); return; } return; } } alreadyResumedError(obj); throw new KotlinNothingValueException(); } } while (!AbstractResolvableFuture$SafeAtomicHelper$$ExternalSyntheticBackportWithForwarding0.m(_state$FU, this, obj2, resumedState((NotCompleted) obj2, obj, i, function1, null))); detachChildIfNonResuable(); dispatchResume(i); } public final Symbol tryResumeImpl(Object obj, Object obj2, Function1 function1) { Object obj3; AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = _state$FU; do { obj3 = atomicReferenceFieldUpdater.get(this); if (obj3 instanceof NotCompleted) { } else { if ((obj3 instanceof CompletedContinuation) && obj2 != null && ((CompletedContinuation) obj3).idempotentResume == obj2) { return CancellableContinuationImplKt.RESUME_TOKEN; } return null; } } while (!AbstractResolvableFuture$SafeAtomicHelper$$ExternalSyntheticBackportWithForwarding0.m(_state$FU, this, obj3, resumedState((NotCompleted) obj3, obj, this.resumeMode, function1, obj2))); detachChildIfNonResuable(); return CancellableContinuationImplKt.RESUME_TOKEN; } public final Void alreadyResumedError(Object obj) { throw new IllegalStateException(("Already resumed, but proposed with update " + obj).toString()); } public final void detachChildIfNonResuable() { if (isReusable()) { return; } detachChild$kotlinx_coroutines_core(); } public final void detachChild$kotlinx_coroutines_core() { DisposableHandle parentHandle = getParentHandle(); if (parentHandle == null) { return; } parentHandle.dispose(); _parentHandle$FU.set(this, NonDisposableHandle.INSTANCE); } @Override // kotlinx.coroutines.CancellableContinuation public Object tryResume(Object obj, Object obj2, Function1 function1) { return tryResumeImpl(obj, obj2, function1); } @Override // kotlinx.coroutines.CancellableContinuation public Object tryResumeWithException(Throwable th) { return tryResumeImpl(new CompletedExceptionally(th, false, 2, null), null, null); } @Override // kotlinx.coroutines.CancellableContinuation public void completeResume(Object obj) { dispatchResume(this.resumeMode); } @Override // kotlinx.coroutines.CancellableContinuation public void resumeUndispatched(CoroutineDispatcher coroutineDispatcher, Object obj) { Continuation continuation = this.delegate; DispatchedContinuation dispatchedContinuation = continuation instanceof DispatchedContinuation ? (DispatchedContinuation) continuation : null; resumeImpl$default(this, obj, (dispatchedContinuation != null ? dispatchedContinuation.dispatcher : null) == coroutineDispatcher ? 4 : this.resumeMode, null, 4, null); } @Override // kotlinx.coroutines.DispatchedTask public Object getSuccessfulResult$kotlinx_coroutines_core(Object obj) { return obj instanceof CompletedContinuation ? ((CompletedContinuation) obj).result : obj; } @Override // kotlinx.coroutines.DispatchedTask public Throwable getExceptionalResult$kotlinx_coroutines_core(Object obj) { Throwable exceptionalResult$kotlinx_coroutines_core = super.getExceptionalResult$kotlinx_coroutines_core(obj); if (exceptionalResult$kotlinx_coroutines_core != null) { return exceptionalResult$kotlinx_coroutines_core; } return null; } public String toString() { return nameString() + '(' + DebugStringsKt.toDebugString(this.delegate) + "){" + getStateDebugRepresentation() + "}@" + DebugStringsKt.getHexAddress(this); } }