package okhttp3;

import java.io.InterruptedIOException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import kotlin.jvm.internal.o;
import o5.a;
import okhttp3.internal.Util;
import okhttp3.internal.connection.RealCall;

/* JADX INFO: loaded from: classes3.dex */
public final class Dispatcher {

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public ThreadPoolExecutor f22004c;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public int f22002a = 64;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public int f22003b = 5;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final ArrayDeque f22005d = new ArrayDeque();

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final ArrayDeque f22006e = new ArrayDeque();

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final ArrayDeque f22007f = new ArrayDeque();

    public final void a(ArrayDeque arrayDeque, Object obj) {
        synchronized (this) {
            if (!arrayDeque.remove(obj)) {
                throw new AssertionError("Call wasn't in-flight!");
            }
        }
        c();
    }

    public final void b(RealCall.AsyncCall asyncCall) {
        asyncCall.f22333b.decrementAndGet();
        a(this.f22006e, asyncCall);
    }

    /* JADX WARN: Removed duplicated region for block: B:20:0x005c  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void c() {
        int size;
        int i6;
        ThreadPoolExecutor threadPoolExecutor;
        byte[] bArr = Util.f22179a;
        ArrayList arrayList = new ArrayList();
        synchronized (this) {
            try {
                Iterator it = this.f22005d.iterator();
                o.g(it, "readyAsyncCalls.iterator()");
                while (it.hasNext()) {
                    RealCall.AsyncCall asyncCall = (RealCall.AsyncCall) it.next();
                    if (this.f22006e.size() >= this.f22002a) {
                        break;
                    }
                    if (asyncCall.f22333b.get() < this.f22003b) {
                        it.remove();
                        asyncCall.f22333b.incrementAndGet();
                        arrayList.add(asyncCall);
                        this.f22006e.add(asyncCall);
                    }
                }
                synchronized (this) {
                    this.f22006e.size();
                    this.f22007f.size();
                }
                size = arrayList.size();
                boolean z6 = false;
                for (i6 = 0; i6 < size; i6++) {
                    RealCall.AsyncCall asyncCall2 = (RealCall.AsyncCall) arrayList.get(i6);
                    synchronized (this) {
                        try {
                            if (this.f22004c == null) {
                                TimeUnit timeUnit = TimeUnit.SECONDS;
                                SynchronousQueue synchronousQueue = new SynchronousQueue();
                                String name = Util.f22185g + " Dispatcher";
                                o.h(name, "name");
                                this.f22004c = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, timeUnit, synchronousQueue, new a(name, z6));
                            }
                            threadPoolExecutor = this.f22004c;
                            o.e(threadPoolExecutor);
                        } finally {
                        }
                    }
                    asyncCall2.getClass();
                    RealCall realCall = RealCall.this;
                    Dispatcher dispatcher = realCall.f22317a.f22072a;
                    byte[] bArr2 = Util.f22179a;
                    try {
                        try {
                            threadPoolExecutor.execute(asyncCall2);
                        } catch (RejectedExecutionException e6) {
                            InterruptedIOException interruptedIOException = new InterruptedIOException("executor rejected");
                            interruptedIOException.initCause(e6);
                            realCall.i(interruptedIOException);
                            asyncCall2.f22332a.onFailure(realCall, interruptedIOException);
                            realCall.f22317a.f22072a.b(asyncCall2);
                        }
                    } catch (Throwable th) {
                        realCall.f22317a.f22072a.b(asyncCall2);
                        throw th;
                    }
                }
            } catch (Throwable th2) {
                throw th2;
            }
        }
        size = arrayList.size();
        boolean z62 = false;
        while (i6 < size) {
        }
    }
}
