package uq;

import com.google.android.gms.common.api.Api;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.Iterator;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import rm.n0;
import zq.h;

/* loaded from: classes3.dex */
public final class p {

    /* renamed from: c, reason: collision with root package name */
    private Runnable f37574c;

    /* renamed from: d, reason: collision with root package name */
    private ExecutorService f37575d;

    /* renamed from: a, reason: collision with root package name */
    private int f37572a = 64;

    /* renamed from: b, reason: collision with root package name */
    private int f37573b = 5;

    /* renamed from: e, reason: collision with root package name */
    private final ArrayDeque f37576e = new ArrayDeque();

    /* renamed from: f, reason: collision with root package name */
    private final ArrayDeque f37577f = new ArrayDeque();

    /* renamed from: g, reason: collision with root package name */
    private final ArrayDeque f37578g = new ArrayDeque();

    private final h.a d(String str) {
        Iterator it = this.f37577f.iterator();
        while (it.hasNext()) {
            h.a aVar = (h.a) it.next();
            if (kotlin.jvm.internal.t.b(aVar.f(), str)) {
                return aVar;
            }
        }
        Iterator it2 = this.f37576e.iterator();
        while (it2.hasNext()) {
            h.a aVar2 = (h.a) it2.next();
            if (kotlin.jvm.internal.t.b(aVar2.f(), str)) {
                return aVar2;
            }
        }
        return null;
    }

    private final void e(Deque deque, Object obj) {
        Runnable runnable;
        synchronized (this) {
            if (!deque.remove(obj)) {
                throw new AssertionError("Call wasn't in-flight!");
            }
            runnable = this.f37574c;
            n0 n0Var = n0.f34904a;
        }
        if (h() || runnable == null) {
            return;
        }
        runnable.run();
    }

    private final boolean h() {
        int i10;
        boolean z10;
        if (vq.p.f40482e && Thread.holdsLock(this)) {
            throw new AssertionError("Thread " + Thread.currentThread().getName() + " MUST NOT hold lock on " + this);
        }
        ArrayList arrayList = new ArrayList();
        synchronized (this) {
            Iterator it = this.f37576e.iterator();
            kotlin.jvm.internal.t.f(it, "readyAsyncCalls.iterator()");
            while (it.hasNext()) {
                h.a asyncCall = (h.a) it.next();
                if (this.f37577f.size() >= this.f37572a) {
                    break;
                }
                if (asyncCall.e().get() < this.f37573b) {
                    it.remove();
                    asyncCall.e().incrementAndGet();
                    kotlin.jvm.internal.t.f(asyncCall, "asyncCall");
                    arrayList.add(asyncCall);
                    this.f37577f.add(asyncCall);
                }
            }
            i10 = 0;
            z10 = i() > 0;
            n0 n0Var = n0.f34904a;
        }
        if (c().isShutdown()) {
            int size = arrayList.size();
            while (i10 < size) {
                h.a aVar = (h.a) arrayList.get(i10);
                aVar.e().decrementAndGet();
                synchronized (this) {
                    this.f37577f.remove(aVar);
                }
                h.a.c(aVar, null, 1, null);
                i10++;
            }
            Runnable runnable = this.f37574c;
            if (runnable != null) {
                runnable.run();
            }
        } else {
            int size2 = arrayList.size();
            while (i10 < size2) {
                ((h.a) arrayList.get(i10)).a(c());
                i10++;
            }
        }
        return z10;
    }

    public final void a(h.a call) {
        h.a d10;
        kotlin.jvm.internal.t.g(call, "call");
        synchronized (this) {
            this.f37576e.add(call);
            if (!call.d().p() && (d10 = d(call.f())) != null) {
                call.g(d10);
            }
            n0 n0Var = n0.f34904a;
        }
        h();
    }

    public final synchronized void b(zq.h call) {
        kotlin.jvm.internal.t.g(call, "call");
        this.f37578g.add(call);
    }

    public final synchronized ExecutorService c() {
        ExecutorService executorService;
        if (this.f37575d == null) {
            this.f37575d = new ThreadPoolExecutor(0, Api.BaseClientBuilder.API_PRIORITY_OTHER, 60L, TimeUnit.SECONDS, new SynchronousQueue(), vq.p.o(vq.p.f40483f + " Dispatcher", false));
        }
        executorService = this.f37575d;
        kotlin.jvm.internal.t.d(executorService);
        return executorService;
    }

    public final void f(h.a call) {
        kotlin.jvm.internal.t.g(call, "call");
        call.e().decrementAndGet();
        e(this.f37577f, call);
    }

    public final void g(zq.h call) {
        kotlin.jvm.internal.t.g(call, "call");
        e(this.f37578g, call);
    }

    public final synchronized int i() {
        return this.f37577f.size() + this.f37578g.size();
    }
}
