package androidx.compose.runtime;

import V4.C0772m;
import V4.InterfaceC0770l;
import a.AbstractC0872a;
import androidx.compose.runtime.internal.StabilityInferred;
import java.util.ArrayList;
import java.util.List;
import w4.C3137A;

/* JADX INFO: loaded from: classes.dex */
@StabilityInferred(parameters = 0)
public final class Latch {
    public static final int $stable = 8;
    private final Object lock = new Object();
    private List<A4.d> awaiters = new ArrayList();
    private List<A4.d> spareList = new ArrayList();
    private boolean _isOpen = true;

    public final Object await(A4.d dVar) {
        if (isOpen()) {
            return C3137A.f25453a;
        }
        final C0772m c0772m = new C0772m(1, AbstractC0872a.V(dVar));
        c0772m.s();
        synchronized (this.lock) {
            this.awaiters.add(c0772m);
        }
        c0772m.u(new J4.c() { // from class: androidx.compose.runtime.Latch$await$2$2
            @Override // J4.c
            public /* bridge */ /* synthetic */ Object invoke(Object obj) {
                invoke((Throwable) obj);
                return C3137A.f25453a;
            }

            public final void invoke(Throwable th) {
                Object obj = this.this$0.lock;
                Latch latch = this.this$0;
                InterfaceC0770l interfaceC0770l = c0772m;
                synchronized (obj) {
                    latch.awaiters.remove(interfaceC0770l);
                }
            }
        });
        Object objR = c0772m.r();
        return objR == B4.a.f492a ? objR : C3137A.f25453a;
    }

    public final void closeLatch() {
        synchronized (this.lock) {
            this._isOpen = false;
        }
    }

    public final boolean isOpen() {
        boolean z6;
        synchronized (this.lock) {
            z6 = this._isOpen;
        }
        return z6;
    }

    public final void openLatch() {
        synchronized (this.lock) {
            try {
                if (isOpen()) {
                    return;
                }
                List<A4.d> list = this.awaiters;
                this.awaiters = this.spareList;
                this.spareList = list;
                this._isOpen = true;
                int size = list.size();
                for (int i6 = 0; i6 < size; i6++) {
                    list.get(i6).resumeWith(C3137A.f25453a);
                }
                list.clear();
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final <R> R withClosed(J4.a aVar) {
        closeLatch();
        try {
            return (R) aVar.invoke();
        } finally {
            openLatch();
        }
    }
}
