package androidx.compose.runtime.internal;

import D.q;
import androidx.compose.runtime.CancellationHandle;
import androidx.compose.runtime.OneShotCancellationHandle;
import androidx.compose.runtime.internal.AwaiterQueue.Awaiter;
import kotlin.jvm.internal.A;
import n.J;
import w4.C3137A;

/* JADX INFO: loaded from: classes.dex */
@StabilityInferred(parameters = 0)
public final class AwaiterQueue<A extends Awaiter> {
    public static final int $stable = 8;
    private Throwable failureCause;
    private final Object lock = new Object();
    private final AtomicInt pendingAwaitersCountUnlocked = AtomicAwaitersCount.m200constructorimpl();
    private J awaiters = new J();
    private J spareList = new J();

    @StabilityInferred(parameters = 1)
    public static abstract class Awaiter {
        public static final int $stable = 0;

        public abstract void cancel();

        public abstract void resumeWithException(Throwable th);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final C3137A addAwaiter$lambda$1(Awaiter awaiter, AwaiterQueue awaiterQueue, A a6) {
        int i6;
        awaiter.cancel();
        AtomicInt atomicInt = awaiterQueue.pendingAwaitersCountUnlocked;
        int i7 = a6.f20113a;
        do {
            i6 = atomicInt.get();
        } while (!atomicInt.compareAndSet(i6, ((i6 >>> 27) & 15) == i7 ? i6 - 1 : i6));
        return C3137A.f25453a;
    }

    public final CancellationHandle addAwaiter(A a6, J4.a aVar) {
        int i6;
        int i7;
        A a7 = new A();
        a7.f20113a = -1;
        synchronized (this.lock) {
            Throwable th = this.failureCause;
            if (th != null) {
                a6.resumeWithException(th);
                return CancellationHandle.Companion.getEmpty$runtime();
            }
            AtomicInt atomicInt = this.pendingAwaitersCountUnlocked;
            do {
                i6 = atomicInt.get();
                i7 = i6 + 1;
            } while (!atomicInt.compareAndSet(i6, i7));
            boolean z6 = true;
            if ((134217727 & i7) != 1) {
                z6 = false;
            }
            a7.f20113a = (i7 >>> 27) & 15;
            this.awaiters.g(a6);
            if (z6 && aVar != null) {
                try {
                    aVar.invoke();
                } catch (Throwable th2) {
                    fail(th2);
                }
            }
            return new OneShotCancellationHandle(new q(a6, this, a7, 7));
        }
    }

    public final void fail(Throwable th) {
        int i6;
        synchronized (this.lock) {
            try {
                if (this.failureCause != null) {
                    return;
                }
                this.failureCause = th;
                J j4 = this.awaiters;
                Object[] objArr = j4.f21376a;
                int i7 = j4.f21377b;
                for (int i8 = 0; i8 < i7; i8++) {
                    ((Awaiter) objArr[i8]).resumeWithException(th);
                }
                this.awaiters.j();
                AtomicInt atomicInt = this.pendingAwaitersCountUnlocked;
                do {
                    i6 = atomicInt.get();
                } while (!atomicInt.compareAndSet(i6, AtomicAwaitersCount.m211packimpl(atomicInt, ((i6 >>> 27) & 15) + 1, 0)));
            } catch (Throwable th2) {
                throw th2;
            }
        }
    }

    public final void flushAndDispatchAwaiters(J4.c cVar) {
        int i6;
        int i7;
        synchronized (this.lock) {
            try {
                J j4 = this.awaiters;
                this.awaiters = this.spareList;
                this.spareList = j4;
                AtomicInt atomicInt = this.pendingAwaitersCountUnlocked;
                do {
                    i6 = atomicInt.get();
                } while (!atomicInt.compareAndSet(i6, AtomicAwaitersCount.m211packimpl(atomicInt, ((i6 >>> 27) & 15) + 1, 0)));
                int i8 = j4.f21377b;
                for (i7 = 0; i7 < i8; i7++) {
                    cVar.invoke(j4.b(i7));
                }
                j4.j();
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final boolean getHasAwaiters() {
        return (this.pendingAwaitersCountUnlocked.get() & 134217727) > 0;
    }
}
