what-the-bank/sources/okhttp3/internal/concurrent/TaskQueue.java

295 lines
9.7 KiB
Java
Raw Normal View History

2024-07-27 18:17:47 +07:00
package okhttp3.internal.concurrent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.RejectedExecutionException;
import java.util.logging.Level;
import o.C14866gag;
import o.C14881gav;
import o.C14957gcv;
import o.InterfaceC14890gbN;
import okhttp3.internal.Util;
/* loaded from: classes.dex */
public final class TaskQueue {
private Task activeTask;
private boolean cancelActiveTask;
private final List<Task> futureTasks;
private final String name;
private boolean shutdown;
private final TaskRunner taskRunner;
public TaskQueue(TaskRunner taskRunner, String str) {
C14957gcv.e(taskRunner, "");
C14957gcv.e(str, "");
this.taskRunner = taskRunner;
this.name = str;
this.futureTasks = new ArrayList();
}
public final List<Task> getScheduledTasks() {
List<Task> d;
synchronized (this.taskRunner) {
d = C14881gav.d((Iterable) getFutureTasks$okhttp());
}
return d;
}
public static /* synthetic */ void schedule$default(TaskQueue taskQueue, Task task, long j, int i, Object obj) {
if ((i & 2) != 0) {
j = 0;
}
taskQueue.schedule(task, j);
}
public final void schedule(Task task, long j) {
C14957gcv.e(task, "");
synchronized (this.taskRunner) {
if (getShutdown$okhttp()) {
if (task.getCancelable()) {
if (TaskRunner.Companion.getLogger().isLoggable(Level.FINE)) {
TaskLoggerKt.access$log(task, this, "schedule canceled (queue is shutdown)");
}
return;
} else {
if (TaskRunner.Companion.getLogger().isLoggable(Level.FINE)) {
TaskLoggerKt.access$log(task, this, "schedule failed (queue is shutdown)");
}
throw new RejectedExecutionException();
}
}
if (scheduleAndDecide$okhttp(task, j, false)) {
getTaskRunner$okhttp().kickCoordinator$okhttp(this);
}
C14866gag c14866gag = C14866gag.c;
}
}
public static /* synthetic */ void schedule$default(TaskQueue taskQueue, String str, long j, InterfaceC14890gbN interfaceC14890gbN, int i, Object obj) {
if ((i & 2) != 0) {
j = 0;
}
C14957gcv.e(str, "");
C14957gcv.e(interfaceC14890gbN, "");
taskQueue.schedule(new TaskQueue$schedule$2(str, interfaceC14890gbN), j);
}
public final void schedule(String str, long j, InterfaceC14890gbN<Long> interfaceC14890gbN) {
C14957gcv.e(str, "");
C14957gcv.e(interfaceC14890gbN, "");
schedule(new TaskQueue$schedule$2(str, interfaceC14890gbN), j);
}
public static /* synthetic */ void execute$default(TaskQueue taskQueue, String str, long j, boolean z, InterfaceC14890gbN interfaceC14890gbN, int i, Object obj) {
if ((i & 2) != 0) {
j = 0;
}
if ((i & 4) != 0) {
z = true;
}
C14957gcv.e(str, "");
C14957gcv.e(interfaceC14890gbN, "");
taskQueue.schedule(new TaskQueue$execute$1(str, z, interfaceC14890gbN), j);
}
public final void execute(String str, long j, boolean z, InterfaceC14890gbN<C14866gag> interfaceC14890gbN) {
C14957gcv.e(str, "");
C14957gcv.e(interfaceC14890gbN, "");
schedule(new TaskQueue$execute$1(str, z, interfaceC14890gbN), j);
}
public final CountDownLatch idleLatch() {
synchronized (this.taskRunner) {
if (getActiveTask$okhttp() == null && getFutureTasks$okhttp().isEmpty()) {
return new CountDownLatch(0);
}
Task activeTask$okhttp = getActiveTask$okhttp();
if (activeTask$okhttp instanceof AwaitIdleTask) {
return ((AwaitIdleTask) activeTask$okhttp).getLatch();
}
for (Task task : getFutureTasks$okhttp()) {
if (task instanceof AwaitIdleTask) {
return ((AwaitIdleTask) task).getLatch();
}
}
AwaitIdleTask awaitIdleTask = new AwaitIdleTask();
if (scheduleAndDecide$okhttp(awaitIdleTask, 0L, false)) {
getTaskRunner$okhttp().kickCoordinator$okhttp(this);
}
return awaitIdleTask.getLatch();
}
}
/* loaded from: classes6.dex */
static final class AwaitIdleTask extends Task {
private final CountDownLatch latch;
public AwaitIdleTask() {
super(C14957gcv.c(Util.okHttpName, (Object) " awaitIdle"), false);
this.latch = new CountDownLatch(1);
}
@Override // okhttp3.internal.concurrent.Task
public final long runOnce() {
this.latch.countDown();
return -1L;
}
public final CountDownLatch getLatch() {
return this.latch;
}
}
public final boolean scheduleAndDecide$okhttp(Task task, long j, boolean z) {
String c;
C14957gcv.e(task, "");
task.initQueue$okhttp(this);
long nanoTime = this.taskRunner.getBackend().nanoTime();
long j2 = nanoTime + j;
int indexOf = this.futureTasks.indexOf(task);
if (indexOf != -1) {
if (task.getNextExecuteNanoTime$okhttp() > j2) {
this.futureTasks.remove(indexOf);
} else {
if (TaskRunner.Companion.getLogger().isLoggable(Level.FINE)) {
TaskLoggerKt.access$log(task, this, "already scheduled");
}
return false;
}
}
task.setNextExecuteNanoTime$okhttp(j2);
if (TaskRunner.Companion.getLogger().isLoggable(Level.FINE)) {
if (z) {
c = C14957gcv.c("run again after ", (Object) TaskLoggerKt.formatDuration(j2 - nanoTime));
} else {
c = C14957gcv.c("scheduled after ", (Object) TaskLoggerKt.formatDuration(j2 - nanoTime));
}
TaskLoggerKt.access$log(task, this, c);
}
Iterator<Task> it = this.futureTasks.iterator();
int i = 0;
while (true) {
if (!it.hasNext()) {
i = -1;
break;
}
if (it.next().getNextExecuteNanoTime$okhttp() - nanoTime > j) {
break;
}
i++;
}
if (i == -1) {
i = this.futureTasks.size();
}
this.futureTasks.add(i, task);
return i == 0;
}
public final boolean cancelAllAndDecide$okhttp() {
Task task = this.activeTask;
if (task != null) {
C14957gcv.e(task);
if (task.getCancelable()) {
this.cancelActiveTask = true;
}
}
int size = this.futureTasks.size() - 1;
boolean z = false;
if (size >= 0) {
while (true) {
int i = size - 1;
if (this.futureTasks.get(size).getCancelable()) {
Task task2 = this.futureTasks.get(size);
if (TaskRunner.Companion.getLogger().isLoggable(Level.FINE)) {
TaskLoggerKt.access$log(task2, this, "canceled");
}
this.futureTasks.remove(size);
z = true;
}
if (i < 0) {
break;
}
size = i;
}
}
return z;
}
public final void cancelAll() {
if (!Util.assertionsEnabled || !Thread.holdsLock(this)) {
synchronized (this.taskRunner) {
if (cancelAllAndDecide$okhttp()) {
getTaskRunner$okhttp().kickCoordinator$okhttp(this);
}
C14866gag c14866gag = C14866gag.c;
}
return;
}
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());
}
public final void shutdown() {
if (!Util.assertionsEnabled || !Thread.holdsLock(this)) {
synchronized (this.taskRunner) {
setShutdown$okhttp(true);
if (cancelAllAndDecide$okhttp()) {
getTaskRunner$okhttp().kickCoordinator$okhttp(this);
}
C14866gag c14866gag = C14866gag.c;
}
return;
}
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());
}
public final String toString() {
return this.name;
}
public final void setShutdown$okhttp(boolean z) {
this.shutdown = z;
}
public final void setCancelActiveTask$okhttp(boolean z) {
this.cancelActiveTask = z;
}
public final void setActiveTask$okhttp(Task task) {
this.activeTask = task;
}
public final TaskRunner getTaskRunner$okhttp() {
return this.taskRunner;
}
public final boolean getShutdown$okhttp() {
return this.shutdown;
}
public final String getName$okhttp() {
return this.name;
}
public final List<Task> getFutureTasks$okhttp() {
return this.futureTasks;
}
public final boolean getCancelActiveTask$okhttp() {
return this.cancelActiveTask;
}
public final Task getActiveTask$okhttp() {
return this.activeTask;
}
}