package androidx.room.coroutines;

import androidx.media3.extractor.b1;
import java.util.concurrent.locks.ReentrantLock;
import kotlin.collections.builders.ListBuilder;
import kotlin.collections.n;
import kotlin.coroutines.Continuation;
import kotlin.coroutines.intrinsics.CoroutineSingletons;
import kotlin.jvm.internal.Ref$ObjectRef;
import kotlinx.coroutines.TimeoutCancellationException;
import kotlinx.coroutines.y;

/* JADX INFO: loaded from: classes.dex */
public final class h {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final int f4027a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final sd.a f4028b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final ReentrantLock f4029c = new ReentrantLock();

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public int f4030d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public boolean f4031e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final g[] f4032f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final kotlinx.coroutines.sync.e f4033g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final b1 f4034h;

    public h(int i10, e eVar) {
        this.f4027a = i10;
        this.f4028b = eVar;
        this.f4032f = new g[i10];
        int i11 = kotlinx.coroutines.sync.f.f29554a;
        this.f4033g = new kotlinx.coroutines.sync.e(i10, 0);
        this.f4034h = new b1(i10);
    }

    /* JADX WARN: Code restructure failed: missing block: B:26:0x0066, code lost:
    
        r0.d(r5, r8.f29553b);
     */
    /* JADX WARN: Removed duplicated region for block: B:7:0x0013  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object a(Continuation continuation) {
        Pool$acquire$1 pool$acquire$1;
        int andDecrement;
        int i10;
        h hVar;
        if (continuation instanceof Pool$acquire$1) {
            pool$acquire$1 = (Pool$acquire$1) continuation;
            int i11 = pool$acquire$1.label;
            if ((i11 & Integer.MIN_VALUE) != 0) {
                pool$acquire$1.label = i11 - Integer.MIN_VALUE;
            } else {
                pool$acquire$1 = new Pool$acquire$1(this, continuation);
            }
        }
        Object obj = pool$acquire$1.result;
        CoroutineSingletons coroutineSingletons = CoroutineSingletons.f29226a;
        int i12 = pool$acquire$1.label;
        if (i12 == 0) {
            kotlin.a.d(obj);
            kotlinx.coroutines.sync.e eVar = this.f4033g;
            pool$acquire$1.L$0 = this;
            pool$acquire$1.label = 1;
            eVar.getClass();
            do {
                andDecrement = kotlinx.coroutines.sync.e.f29551g.getAndDecrement(eVar);
                i10 = eVar.f29552a;
            } while (andDecrement > i10);
            Object obj2 = kd.e.f29109a;
            if (andDecrement <= 0) {
                kotlinx.coroutines.h hVarC0 = hb.a.c0(nb.g.F(pool$acquire$1));
                try {
                    if (!eVar.a(hVarC0)) {
                        while (true) {
                            int andDecrement2 = kotlinx.coroutines.sync.e.f29551g.getAndDecrement(eVar);
                            if (andDecrement2 <= i10) {
                                if (andDecrement2 > 0) {
                                    break;
                                }
                                if (eVar.a(hVarC0)) {
                                    break;
                                }
                            }
                        }
                    }
                    Object objS = hVarC0.s();
                    if (objS != coroutineSingletons) {
                        objS = obj2;
                    }
                    if (objS == coroutineSingletons) {
                        obj2 = objS;
                    }
                } catch (Throwable th) {
                    hVarC0.A();
                    throw th;
                }
            }
            if (obj2 == coroutineSingletons) {
                return coroutineSingletons;
            }
            hVar = this;
        } else {
            if (i12 != 1) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            hVar = (h) pool$acquire$1.L$0;
            kotlin.a.d(obj);
        }
        try {
            ReentrantLock reentrantLock = hVar.f4029c;
            b1 b1Var = hVar.f4034h;
            reentrantLock.lock();
            try {
                if (hVar.f4031e) {
                    ua.b.I(21, "Connection pool is closed");
                    throw null;
                }
                if (b1Var.f3328c == b1Var.f3329d && hVar.f4030d < hVar.f4027a) {
                    g gVar = new g((k3.a) hVar.f4028b.invoke());
                    int i13 = hVar.f4030d;
                    hVar.f4030d = i13 + 1;
                    hVar.f4032f[i13] = gVar;
                    b1Var.a(gVar);
                }
                int i14 = b1Var.f3328c;
                if (i14 == b1Var.f3329d) {
                    throw new ArrayIndexOutOfBoundsException();
                }
                Object obj3 = b1Var.f3327b;
                Object obj4 = ((Object[]) obj3)[i14];
                ((Object[]) obj3)[i14] = null;
                b1Var.f3328c = (i14 + 1) & b1Var.f3330e;
                return (g) obj4;
            } finally {
                reentrantLock.unlock();
            }
        } catch (Throwable th2) {
            hVar.f4033g.b();
            throw th2;
        }
    }

    /* JADX WARN: Can't wrap try/catch for region: R(10:14|49|47|28|(0)(0)|37|19|45|20|(0)(0)) */
    /* JADX WARN: Code restructure failed: missing block: B:25:0x0067, code lost:
    
        r2 = move-exception;
     */
    /* JADX WARN: Code restructure failed: missing block: B:27:0x006c, code lost:
    
        r12 = r12;
        r11 = r11;
        r2 = r1;
        r1 = r0;
        r0 = r2;
     */
    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:22:0x005e A[RETURN] */
    /* JADX WARN: Removed duplicated region for block: B:23:0x005f  */
    /* JADX WARN: Removed duplicated region for block: B:30:0x0076 A[Catch: all -> 0x007a, TryCatch #2 {all -> 0x007a, blocks: (B:28:0x0072, B:30:0x0076, B:34:0x007e, B:38:0x0086), top: B:47:0x0072 }] */
    /* JADX WARN: Removed duplicated region for block: B:33:0x007c  */
    /* JADX WARN: Removed duplicated region for block: B:7:0x0013  */
    /* JADX WARN: Unsupported multi-entry loop pattern (BACK_EDGE: B:23:0x005f -> B:24:0x0061). Please report as a decompilation issue!!! */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object b(long j10, d dVar, Continuation continuation) {
        Pool$acquireWithTimeout$1 pool$acquireWithTimeout$1;
        h hVar;
        sd.a aVar;
        Ref$ObjectRef ref$ObjectRef;
        sd.a aVar2;
        CoroutineSingletons coroutineSingletons;
        Pool$acquireWithTimeout$1 pool$acquireWithTimeout$12;
        Throwable th;
        Pool$acquireWithTimeout$2 pool$acquireWithTimeout$2;
        sd.a aVar3;
        if (continuation instanceof Pool$acquireWithTimeout$1) {
            pool$acquireWithTimeout$1 = (Pool$acquireWithTimeout$1) continuation;
            int i10 = pool$acquireWithTimeout$1.label;
            if ((i10 & Integer.MIN_VALUE) != 0) {
                pool$acquireWithTimeout$1.label = i10 - Integer.MIN_VALUE;
            } else {
                pool$acquireWithTimeout$1 = new Pool$acquireWithTimeout$1(this, continuation);
            }
        }
        Object obj = pool$acquireWithTimeout$1.result;
        CoroutineSingletons coroutineSingletons2 = CoroutineSingletons.f29226a;
        int i11 = pool$acquireWithTimeout$1.label;
        if (i11 == 0) {
            kotlin.a.d(obj);
            hVar = this;
            aVar = dVar;
            Ref$ObjectRef ref$ObjectRef2 = new Ref$ObjectRef();
            pool$acquireWithTimeout$2 = new Pool$acquireWithTimeout$2(ref$ObjectRef2, hVar, null);
            pool$acquireWithTimeout$1.L$0 = hVar;
            pool$acquireWithTimeout$1.L$1 = aVar;
            pool$acquireWithTimeout$1.L$2 = ref$ObjectRef2;
            pool$acquireWithTimeout$1.J$0 = j10;
            pool$acquireWithTimeout$1.label = 1;
            if (y.j0(j10, pool$acquireWithTimeout$2, pool$acquireWithTimeout$1) != coroutineSingletons2) {
            }
        } else {
            if (i11 != 1) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            j10 = pool$acquireWithTimeout$1.J$0;
            Ref$ObjectRef ref$ObjectRef3 = (Ref$ObjectRef) pool$acquireWithTimeout$1.L$2;
            sd.a aVar4 = (sd.a) pool$acquireWithTimeout$1.L$1;
            hVar = (h) pool$acquireWithTimeout$1.L$0;
            try {
                kotlin.a.d(obj);
                aVar3 = aVar4;
            } catch (Throwable th2) {
                ref$ObjectRef = ref$ObjectRef3;
                aVar2 = aVar4;
                coroutineSingletons = coroutineSingletons2;
                pool$acquireWithTimeout$12 = pool$acquireWithTimeout$1;
                th = th2;
                if (th instanceof TimeoutCancellationException) {
                }
                pool$acquireWithTimeout$1 = pool$acquireWithTimeout$12;
                coroutineSingletons2 = coroutineSingletons;
                aVar = aVar2;
                Ref$ObjectRef ref$ObjectRef22 = new Ref$ObjectRef();
                pool$acquireWithTimeout$2 = new Pool$acquireWithTimeout$2(ref$ObjectRef22, hVar, null);
                pool$acquireWithTimeout$1.L$0 = hVar;
                pool$acquireWithTimeout$1.L$1 = aVar;
                pool$acquireWithTimeout$1.L$2 = ref$ObjectRef22;
                pool$acquireWithTimeout$1.J$0 = j10;
                pool$acquireWithTimeout$1.label = 1;
                if (y.j0(j10, pool$acquireWithTimeout$2, pool$acquireWithTimeout$1) != coroutineSingletons2) {
                }
            }
            ref$ObjectRef = ref$ObjectRef3;
            aVar2 = aVar3;
            coroutineSingletons = coroutineSingletons2;
            pool$acquireWithTimeout$12 = pool$acquireWithTimeout$1;
            th = null;
            try {
                if (th instanceof TimeoutCancellationException) {
                    aVar2.invoke();
                } else {
                    if (th != null) {
                        throw th;
                    }
                    T t10 = ref$ObjectRef.element;
                    if (t10 != 0) {
                        return t10;
                    }
                }
                pool$acquireWithTimeout$1 = pool$acquireWithTimeout$12;
                coroutineSingletons2 = coroutineSingletons;
                aVar = aVar2;
                Ref$ObjectRef ref$ObjectRef222 = new Ref$ObjectRef();
                pool$acquireWithTimeout$2 = new Pool$acquireWithTimeout$2(ref$ObjectRef222, hVar, null);
                pool$acquireWithTimeout$1.L$0 = hVar;
                pool$acquireWithTimeout$1.L$1 = aVar;
                pool$acquireWithTimeout$1.L$2 = ref$ObjectRef222;
                pool$acquireWithTimeout$1.J$0 = j10;
                pool$acquireWithTimeout$1.label = 1;
                if (y.j0(j10, pool$acquireWithTimeout$2, pool$acquireWithTimeout$1) != coroutineSingletons2) {
                    return coroutineSingletons2;
                }
                aVar3 = aVar;
                ref$ObjectRef3 = ref$ObjectRef222;
                ref$ObjectRef = ref$ObjectRef3;
                aVar2 = aVar3;
                coroutineSingletons = coroutineSingletons2;
                pool$acquireWithTimeout$12 = pool$acquireWithTimeout$1;
                th = null;
                if (th instanceof TimeoutCancellationException) {
                }
                pool$acquireWithTimeout$1 = pool$acquireWithTimeout$12;
                coroutineSingletons2 = coroutineSingletons;
                aVar = aVar2;
                Ref$ObjectRef ref$ObjectRef2222 = new Ref$ObjectRef();
                pool$acquireWithTimeout$2 = new Pool$acquireWithTimeout$2(ref$ObjectRef2222, hVar, null);
                pool$acquireWithTimeout$1.L$0 = hVar;
                pool$acquireWithTimeout$1.L$1 = aVar;
                pool$acquireWithTimeout$1.L$2 = ref$ObjectRef2222;
                pool$acquireWithTimeout$1.J$0 = j10;
                pool$acquireWithTimeout$1.label = 1;
                if (y.j0(j10, pool$acquireWithTimeout$2, pool$acquireWithTimeout$1) != coroutineSingletons2) {
                }
            } catch (Throwable th3) {
                g gVar = (g) ref$ObjectRef.element;
                if (gVar != null) {
                    hVar.e(gVar);
                }
                throw th3;
            }
        }
    }

    public final void c() {
        ReentrantLock reentrantLock = this.f4029c;
        reentrantLock.lock();
        try {
            this.f4031e = true;
            for (g gVar : this.f4032f) {
                if (gVar != null) {
                    gVar.close();
                }
            }
        } finally {
            reentrantLock.unlock();
        }
    }

    public final void d(StringBuilder sb) {
        b1 b1Var = this.f4034h;
        ReentrantLock reentrantLock = this.f4029c;
        reentrantLock.lock();
        try {
            ListBuilder listBuilder = new ListBuilder();
            int i10 = (b1Var.f3329d - b1Var.f3328c) & b1Var.f3330e;
            for (int i11 = 0; i11 < i10; i11++) {
                if (i11 >= 0) {
                    int i12 = b1Var.f3329d;
                    int i13 = b1Var.f3328c;
                    int i14 = b1Var.f3330e;
                    if (i11 < ((i12 - i13) & i14)) {
                        Object obj = ((Object[]) b1Var.f3327b)[(i13 + i11) & i14];
                        nb.g.f(obj);
                        listBuilder.add(obj);
                    }
                }
                throw new ArrayIndexOutOfBoundsException();
            }
            listBuilder.m();
            sb.append('\t' + toString() + " (");
            sb.append("capacity=" + this.f4027a + ", ");
            StringBuilder sb2 = new StringBuilder();
            sb2.append("permits=");
            kotlinx.coroutines.sync.e eVar = this.f4033g;
            eVar.getClass();
            sb2.append(Math.max(kotlinx.coroutines.sync.e.f29551g.get(eVar), 0));
            sb2.append(", ");
            sb.append(sb2.toString());
            sb.append("queue=(size=" + listBuilder.getLength() + ")[" + n.j1(listBuilder, null, null, null, null, 63) + "], ");
            sb.append(")");
            sb.append('\n');
            g[] gVarArr = this.f4032f;
            int length = gVarArr.length;
            int i15 = 0;
            for (int i16 = 0; i16 < length; i16++) {
                g gVar = gVarArr[i16];
                i15++;
                StringBuilder sb3 = new StringBuilder();
                sb3.append("\t\t[");
                sb3.append(i15);
                sb3.append("] - ");
                sb3.append(gVar != null ? gVar.f4023a.toString() : null);
                sb.append(sb3.toString());
                sb.append('\n');
                if (gVar != null) {
                    gVar.k(sb);
                }
            }
            reentrantLock.unlock();
        } catch (Throwable th) {
            reentrantLock.unlock();
            throw th;
        }
    }

    public final void e(g gVar) {
        nb.g.i(gVar, "connection");
        ReentrantLock reentrantLock = this.f4029c;
        reentrantLock.lock();
        try {
            this.f4034h.a(gVar);
            reentrantLock.unlock();
            this.f4033g.b();
        } catch (Throwable th) {
            reentrantLock.unlock();
            throw th;
        }
    }
}
