Rabbit-R1/switch port/java/sources/okhttp3/internal/concurrent/TaskQueue.java
2024-05-21 17:08:36 -04:00

296 lines
14 KiB
Java

package okhttp3.internal.concurrent;
import io.sentry.MonitorConfig;
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 kotlin.Metadata;
import kotlin.Unit;
import kotlin.collections.CollectionsKt;
import kotlin.jvm.functions.Function0;
import kotlin.jvm.internal.Intrinsics;
import okhttp3.internal.Util;
/* compiled from: TaskQueue.kt */
@Metadata(bv = {1, 0, 3}, d1 = {"\u0000V\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u000e\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0005\n\u0002\u0010\u000b\n\u0002\b\u0005\n\u0002\u0010!\n\u0002\b\u0005\n\u0002\u0010 \n\u0002\b\u0007\n\u0002\u0010\u0002\n\u0002\b\u0004\n\u0002\u0010\t\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\b\u0018\u00002\u00020\u0001:\u00013B\u0017\b\u0000\u0012\u0006\u0010\u0002\u001a\u00020\u0003\u0012\u0006\u0010\u0004\u001a\u00020\u0005¢\u0006\u0002\u0010\u0006J\u0006\u0010!\u001a\u00020\"J\r\u0010#\u001a\u00020\u000eH\u0000¢\u0006\u0002\b$J8\u0010%\u001a\u00020\"2\u0006\u0010\u0004\u001a\u00020\u00052\b\b\u0002\u0010&\u001a\u00020'2\b\b\u0002\u0010(\u001a\u00020\u000e2\u000e\b\u0004\u0010)\u001a\b\u0012\u0004\u0012\u00020\"0*H\u0086\\u0001\u0000J\u0006\u0010+\u001a\u00020,J.\u0010-\u001a\u00020\"2\u0006\u0010\u0004\u001a\u00020\u00052\b\b\u0002\u0010&\u001a\u00020'2\u000e\b\u0004\u0010)\u001a\b\u0012\u0004\u0012\u00020'0*H\u0086\\u0001\u0000J\u0018\u0010-\u001a\u00020\"2\u0006\u0010.\u001a\u00020\b2\b\b\u0002\u0010&\u001a\u00020'J%\u0010/\u001a\u00020\u000e2\u0006\u0010.\u001a\u00020\b2\u0006\u0010&\u001a\u00020'2\u0006\u00100\u001a\u00020\u000eH\u0000¢\u0006\u0002\b1J\u0006\u0010\u001c\u001a\u00020\"J\b\u00102\u001a\u00020\u0005H\u0016R\u001c\u0010\u0007\u001a\u0004\u0018\u00010\bX\u0080\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b\t\u0010\n\"\u0004\b\u000b\u0010\fR\u001a\u0010\r\u001a\u00020\u000eX\u0080\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b\u000f\u0010\u0010\"\u0004\b\u0011\u0010\u0012R\u001a\u0010\u0013\u001a\b\u0012\u0004\u0012\u00020\b0\u0014X\u0080\u0004¢\u0006\b\n\u0000\u001a\u0004\b\u0015\u0010\u0016R\u0014\u0010\u0004\u001a\u00020\u0005X\u0080\u0004¢\u0006\b\n\u0000\u001a\u0004\b\u0017\u0010\u0018R\u0017\u0010\u0019\u001a\b\u0012\u0004\u0012\u00020\b0\u001a8F¢\u0006\u0006\u001a\u0004\b\u001b\u0010\u0016R\u001a\u0010\u001c\u001a\u00020\u000eX\u0080\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b\u001d\u0010\u0010\"\u0004\b\u001e\u0010\u0012R\u0014\u0010\u0002\u001a\u00020\u0003X\u0080\u0004¢\u0006\b\n\u0000\u001a\u0004\b\u001f\u0010 \u0082\u0002\u0007\n\u0005\b\u009920\u0001¨\u00064"}, d2 = {"Lokhttp3/internal/concurrent/TaskQueue;", "", "taskRunner", "Lokhttp3/internal/concurrent/TaskRunner;", "name", "", "(Lokhttp3/internal/concurrent/TaskRunner;Ljava/lang/String;)V", "activeTask", "Lokhttp3/internal/concurrent/Task;", "getActiveTask$okhttp", "()Lokhttp3/internal/concurrent/Task;", "setActiveTask$okhttp", "(Lokhttp3/internal/concurrent/Task;)V", "cancelActiveTask", "", "getCancelActiveTask$okhttp", "()Z", "setCancelActiveTask$okhttp", "(Z)V", "futureTasks", "", "getFutureTasks$okhttp", "()Ljava/util/List;", "getName$okhttp", "()Ljava/lang/String;", "scheduledTasks", "", "getScheduledTasks", "shutdown", "getShutdown$okhttp", "setShutdown$okhttp", "getTaskRunner$okhttp", "()Lokhttp3/internal/concurrent/TaskRunner;", "cancelAll", "", "cancelAllAndDecide", "cancelAllAndDecide$okhttp", "execute", "delayNanos", "", "cancelable", "block", "Lkotlin/Function0;", "idleLatch", "Ljava/util/concurrent/CountDownLatch;", MonitorConfig.JsonKeys.SCHEDULE, "task", "scheduleAndDecide", "recurrence", "scheduleAndDecide$okhttp", "toString", "AwaitIdleTask", "okhttp"}, k = 1, mv = {1, 4, 0})
/* loaded from: classes3.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;
/* renamed from: getActiveTask$okhttp, reason: from getter */
public final Task getActiveTask() {
return this.activeTask;
}
/* renamed from: getCancelActiveTask$okhttp, reason: from getter */
public final boolean getCancelActiveTask() {
return this.cancelActiveTask;
}
public final List<Task> getFutureTasks$okhttp() {
return this.futureTasks;
}
/* renamed from: getName$okhttp, reason: from getter */
public final String getName() {
return this.name;
}
/* renamed from: getShutdown$okhttp, reason: from getter */
public final boolean getShutdown() {
return this.shutdown;
}
/* renamed from: getTaskRunner$okhttp, reason: from getter */
public final TaskRunner getTaskRunner() {
return this.taskRunner;
}
public final void setActiveTask$okhttp(Task task) {
this.activeTask = task;
}
public final void setCancelActiveTask$okhttp(boolean z) {
this.cancelActiveTask = z;
}
public final void setShutdown$okhttp(boolean z) {
this.shutdown = z;
}
public String toString() {
return this.name;
}
public TaskQueue(TaskRunner taskRunner, String name) {
Intrinsics.checkNotNullParameter(taskRunner, "taskRunner");
Intrinsics.checkNotNullParameter(name, "name");
this.taskRunner = taskRunner;
this.name = name;
this.futureTasks = new ArrayList();
}
public final List<Task> getScheduledTasks() {
List<Task> list;
synchronized (this.taskRunner) {
list = CollectionsKt.toList(this.futureTasks);
}
return list;
}
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 delayNanos) {
Intrinsics.checkNotNullParameter(task, "task");
synchronized (this.taskRunner) {
if (this.shutdown) {
if (task.getCancelable()) {
if (TaskRunner.INSTANCE.getLogger().isLoggable(Level.FINE)) {
TaskLoggerKt.access$log(task, this, "schedule canceled (queue is shutdown)");
}
return;
} else {
if (TaskRunner.INSTANCE.getLogger().isLoggable(Level.FINE)) {
TaskLoggerKt.access$log(task, this, "schedule failed (queue is shutdown)");
}
throw new RejectedExecutionException();
}
}
if (scheduleAndDecide$okhttp(task, delayNanos, false)) {
this.taskRunner.kickCoordinator$okhttp(this);
}
Unit unit = Unit.INSTANCE;
}
}
public static /* synthetic */ void schedule$default(TaskQueue taskQueue, String name, long j, Function0 block, int i, Object obj) {
if ((i & 2) != 0) {
j = 0;
}
Intrinsics.checkNotNullParameter(name, "name");
Intrinsics.checkNotNullParameter(block, "block");
taskQueue.schedule(new TaskQueue$schedule$2(block, name, name), j);
}
public final void schedule(String name, long delayNanos, Function0<Long> block) {
Intrinsics.checkNotNullParameter(name, "name");
Intrinsics.checkNotNullParameter(block, "block");
schedule(new TaskQueue$schedule$2(block, name, name), delayNanos);
}
public static /* synthetic */ void execute$default(TaskQueue taskQueue, String name, long j, boolean z, Function0 block, int i, Object obj) {
if ((i & 2) != 0) {
j = 0;
}
if ((i & 4) != 0) {
z = true;
}
boolean z2 = z;
Intrinsics.checkNotNullParameter(name, "name");
Intrinsics.checkNotNullParameter(block, "block");
taskQueue.schedule(new TaskQueue$execute$1(block, name, z2, name, z2), j);
}
public final void execute(String name, long delayNanos, boolean cancelable, Function0<Unit> block) {
Intrinsics.checkNotNullParameter(name, "name");
Intrinsics.checkNotNullParameter(block, "block");
schedule(new TaskQueue$execute$1(block, name, cancelable, name, cancelable), delayNanos);
}
public final CountDownLatch idleLatch() {
synchronized (this.taskRunner) {
if (this.activeTask == null && this.futureTasks.isEmpty()) {
return new CountDownLatch(0);
}
Task task = this.activeTask;
if (task instanceof AwaitIdleTask) {
return ((AwaitIdleTask) task).getLatch();
}
for (Task task2 : this.futureTasks) {
if (task2 instanceof AwaitIdleTask) {
return ((AwaitIdleTask) task2).getLatch();
}
}
AwaitIdleTask awaitIdleTask = new AwaitIdleTask();
if (scheduleAndDecide$okhttp(awaitIdleTask, 0L, false)) {
this.taskRunner.kickCoordinator$okhttp(this);
}
return awaitIdleTask.getLatch();
}
}
/* compiled from: TaskQueue.kt */
@Metadata(bv = {1, 0, 3}, d1 = {"\u0000\u001a\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0010\t\n\u0000\b\u0002\u0018\u00002\u00020\u0001B\u0005¢\u0006\u0002\u0010\u0002J\b\u0010\u0007\u001a\u00020\bH\u0016R\u0011\u0010\u0003\u001a\u00020\u0004¢\u0006\b\n\u0000\u001a\u0004\b\u0005\u0010\u0006¨\u0006\t"}, d2 = {"Lokhttp3/internal/concurrent/TaskQueue$AwaitIdleTask;", "Lokhttp3/internal/concurrent/Task;", "()V", "latch", "Ljava/util/concurrent/CountDownLatch;", "getLatch", "()Ljava/util/concurrent/CountDownLatch;", "runOnce", "", "okhttp"}, k = 1, mv = {1, 4, 0})
/* loaded from: classes3.dex */
private static final class AwaitIdleTask extends Task {
private final CountDownLatch latch;
public final CountDownLatch getLatch() {
return this.latch;
}
public AwaitIdleTask() {
super(Util.okHttpName + " awaitIdle", false);
this.latch = new CountDownLatch(1);
}
@Override // okhttp3.internal.concurrent.Task
public long runOnce() {
this.latch.countDown();
return -1L;
}
}
public final boolean scheduleAndDecide$okhttp(Task task, long delayNanos, boolean recurrence) {
String str;
Intrinsics.checkNotNullParameter(task, "task");
task.initQueue$okhttp(this);
long nanoTime = this.taskRunner.getBackend().nanoTime();
long j = nanoTime + delayNanos;
int indexOf = this.futureTasks.indexOf(task);
if (indexOf != -1) {
if (task.getNextExecuteNanoTime() > j) {
this.futureTasks.remove(indexOf);
} else {
if (TaskRunner.INSTANCE.getLogger().isLoggable(Level.FINE)) {
TaskLoggerKt.access$log(task, this, "already scheduled");
}
return false;
}
}
task.setNextExecuteNanoTime$okhttp(j);
if (TaskRunner.INSTANCE.getLogger().isLoggable(Level.FINE)) {
if (recurrence) {
str = "run again after " + TaskLoggerKt.formatDuration(j - nanoTime);
} else {
str = "scheduled after " + TaskLoggerKt.formatDuration(j - nanoTime);
}
TaskLoggerKt.access$log(task, this, str);
}
Iterator<Task> it = this.futureTasks.iterator();
int i = 0;
while (true) {
if (!it.hasNext()) {
i = -1;
break;
}
if (it.next().getNextExecuteNanoTime() - nanoTime > delayNanos) {
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) {
Intrinsics.checkNotNull(task);
if (task.getCancelable()) {
this.cancelActiveTask = true;
}
}
boolean z = false;
for (int size = this.futureTasks.size() - 1; size >= 0; size--) {
if (this.futureTasks.get(size).getCancelable()) {
Task task2 = this.futureTasks.get(size);
if (TaskRunner.INSTANCE.getLogger().isLoggable(Level.FINE)) {
TaskLoggerKt.access$log(task2, this, "canceled");
}
this.futureTasks.remove(size);
z = true;
}
}
return z;
}
public final void cancelAll() {
if (!Util.assertionsEnabled || !Thread.holdsLock(this)) {
synchronized (this.taskRunner) {
if (cancelAllAndDecide$okhttp()) {
this.taskRunner.kickCoordinator$okhttp(this);
}
Unit unit = Unit.INSTANCE;
}
return;
}
StringBuilder sb = new StringBuilder("Thread ");
Thread currentThread = Thread.currentThread();
Intrinsics.checkNotNullExpressionValue(currentThread, "Thread.currentThread()");
throw new AssertionError(sb.append(currentThread.getName()).append(" MUST NOT hold lock on ").append(this).toString());
}
public final void shutdown() {
if (!Util.assertionsEnabled || !Thread.holdsLock(this)) {
synchronized (this.taskRunner) {
this.shutdown = true;
if (cancelAllAndDecide$okhttp()) {
this.taskRunner.kickCoordinator$okhttp(this);
}
Unit unit = Unit.INSTANCE;
}
return;
}
StringBuilder sb = new StringBuilder("Thread ");
Thread currentThread = Thread.currentThread();
Intrinsics.checkNotNullExpressionValue(currentThread, "Thread.currentThread()");
throw new AssertionError(sb.append(currentThread.getName()).append(" MUST NOT hold lock on ").append(this).toString());
}
}