package okhttp3; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Deque; import java.util.Iterator; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.SynchronousQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import o.C14866gag; import o.C14881gav; import o.C14957gcv; import okhttp3.internal.Util; import okhttp3.internal.connection.RealCall; /* loaded from: classes.dex */ public final class Dispatcher { private ExecutorService executorServiceOrNull; private Runnable idleCallback; private int maxRequests; private int maxRequestsPerHost; private final ArrayDeque readyAsyncCalls; private final ArrayDeque runningAsyncCalls; private final ArrayDeque runningSyncCalls; public Dispatcher() { this.maxRequests = 64; this.maxRequestsPerHost = 5; this.readyAsyncCalls = new ArrayDeque<>(); this.runningAsyncCalls = new ArrayDeque<>(); this.runningSyncCalls = new ArrayDeque<>(); } public final int getMaxRequests() { int i; synchronized (this) { i = this.maxRequests; } return i; } public final void setMaxRequests(int i) { if (i <= 0) { throw new IllegalArgumentException(C14957gcv.c("max < 1: ", Integer.valueOf(i)).toString()); } synchronized (this) { this.maxRequests = i; C14866gag c14866gag = C14866gag.c; } promoteAndExecute(); } public final int getMaxRequestsPerHost() { int i; synchronized (this) { i = this.maxRequestsPerHost; } return i; } public final void setMaxRequestsPerHost(int i) { if (i <= 0) { throw new IllegalArgumentException(C14957gcv.c("max < 1: ", Integer.valueOf(i)).toString()); } synchronized (this) { this.maxRequestsPerHost = i; C14866gag c14866gag = C14866gag.c; } promoteAndExecute(); } public final Runnable getIdleCallback() { Runnable runnable; synchronized (this) { runnable = this.idleCallback; } return runnable; } public final void setIdleCallback(Runnable runnable) { synchronized (this) { this.idleCallback = runnable; } } public final ExecutorService executorService() { ExecutorService executorService; synchronized (this) { if (this.executorServiceOrNull == null) { this.executorServiceOrNull = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue(), Util.threadFactory(C14957gcv.c(Util.okHttpName, (Object) " Dispatcher"), false)); } executorService = this.executorServiceOrNull; C14957gcv.e(executorService); } return executorService; } /* JADX WARN: 'this' call moved to the top of the method (can break code semantics) */ public Dispatcher(ExecutorService executorService) { this(); C14957gcv.e(executorService, ""); this.executorServiceOrNull = executorService; } public final void enqueue$okhttp(RealCall.AsyncCall asyncCall) { RealCall.AsyncCall findExistingCallWithHost; C14957gcv.e(asyncCall, ""); synchronized (this) { this.readyAsyncCalls.add(asyncCall); if (!asyncCall.getCall().getForWebSocket() && (findExistingCallWithHost = findExistingCallWithHost(asyncCall.getHost())) != null) { asyncCall.reuseCallsPerHostFrom(findExistingCallWithHost); } C14866gag c14866gag = C14866gag.c; } promoteAndExecute(); } private final RealCall.AsyncCall findExistingCallWithHost(String str) { Iterator it = this.runningAsyncCalls.iterator(); while (it.hasNext()) { RealCall.AsyncCall next = it.next(); if (C14957gcv.b((Object) next.getHost(), (Object) str)) { return next; } } Iterator it2 = this.readyAsyncCalls.iterator(); while (it2.hasNext()) { RealCall.AsyncCall next2 = it2.next(); if (C14957gcv.b((Object) next2.getHost(), (Object) str)) { return next2; } } return null; } public final void cancelAll() { synchronized (this) { Iterator it = this.readyAsyncCalls.iterator(); while (it.hasNext()) { it.next().getCall().cancel(); } Iterator it2 = this.runningAsyncCalls.iterator(); while (it2.hasNext()) { it2.next().getCall().cancel(); } Iterator it3 = this.runningSyncCalls.iterator(); while (it3.hasNext()) { it3.next().cancel(); } } } public final void executed$okhttp(RealCall realCall) { synchronized (this) { C14957gcv.e(realCall, ""); this.runningSyncCalls.add(realCall); } } public final void finished$okhttp(RealCall.AsyncCall asyncCall) { C14957gcv.e(asyncCall, ""); asyncCall.getCallsPerHost().decrementAndGet(); finished(this.runningAsyncCalls, asyncCall); } public final void finished$okhttp(RealCall realCall) { C14957gcv.e(realCall, ""); finished(this.runningSyncCalls, realCall); } private final void finished(Deque deque, T t) { Runnable idleCallback; synchronized (this) { if (!deque.remove(t)) { throw new AssertionError("Call wasn't in-flight!"); } idleCallback = getIdleCallback(); C14866gag c14866gag = C14866gag.c; } if (promoteAndExecute() || idleCallback == null) { return; } idleCallback.run(); } public final List queuedCalls() { List unmodifiableList; synchronized (this) { ArrayDeque arrayDeque = this.readyAsyncCalls; C14957gcv.e(arrayDeque, ""); ArrayList arrayList = new ArrayList(arrayDeque instanceof Collection ? arrayDeque.size() : 10); Iterator it = arrayDeque.iterator(); while (it.hasNext()) { arrayList.add(((RealCall.AsyncCall) it.next()).getCall()); } unmodifiableList = Collections.unmodifiableList(arrayList); C14957gcv.c(unmodifiableList, ""); } return unmodifiableList; } public final List runningCalls() { List unmodifiableList; synchronized (this) { ArrayDeque arrayDeque = this.runningSyncCalls; ArrayDeque arrayDeque2 = this.runningAsyncCalls; C14957gcv.e(arrayDeque2, ""); ArrayList arrayList = new ArrayList(arrayDeque2 instanceof Collection ? arrayDeque2.size() : 10); Iterator it = arrayDeque2.iterator(); while (it.hasNext()) { arrayList.add(((RealCall.AsyncCall) it.next()).getCall()); } unmodifiableList = Collections.unmodifiableList(C14881gav.e(arrayDeque, arrayList)); C14957gcv.c(unmodifiableList, ""); } return unmodifiableList; } public final int queuedCallsCount() { int size; synchronized (this) { size = this.readyAsyncCalls.size(); } return size; } public final int runningCallsCount() { int size; int size2; synchronized (this) { size = this.runningAsyncCalls.size(); size2 = this.runningSyncCalls.size(); } return size + size2; } /* renamed from: -deprecated_executorService, reason: not valid java name */ public final ExecutorService m396deprecated_executorService() { return executorService(); } private final boolean promoteAndExecute() { int i; boolean z; if (!Util.assertionsEnabled || !Thread.holdsLock(this)) { ArrayList arrayList = new ArrayList(); synchronized (this) { Iterator it = this.readyAsyncCalls.iterator(); C14957gcv.c(it, ""); while (it.hasNext()) { RealCall.AsyncCall next = it.next(); if (this.runningAsyncCalls.size() >= getMaxRequests()) { break; } if (next.getCallsPerHost().get() < getMaxRequestsPerHost()) { it.remove(); next.getCallsPerHost().incrementAndGet(); C14957gcv.c(next, ""); arrayList.add(next); this.runningAsyncCalls.add(next); } } z = runningCallsCount() > 0; C14866gag c14866gag = C14866gag.c; } int size = arrayList.size(); for (i = 0; i < size; i++) { ((RealCall.AsyncCall) arrayList.get(i)).executeOn(executorService()); } return z; } StringBuilder sb = new StringBuilder("Thread "); sb.append((Object) Thread.currentThread().getName()); sb.append(" MUST NOT hold lock on "); sb.append(this); throw new AssertionError(sb.toString()); } }