package okhttp3.internal.concurrent;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import kotlin.jvm.internal.o;
import o5.a;
import okhttp3.internal.Util;
import okhttp3.internal.concurrent.TaskRunner;

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public static final Companion f22273h = new Companion(0);

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public static final TaskRunner f22274i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public static final Logger f22275j;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final RealBackend f22276a;

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public long f22279d;

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

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

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

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final TaskRunner$runnable$1 f22282g = new Runnable() { // from class: okhttp3.internal.concurrent.TaskRunner$runnable$1
        @Override // java.lang.Runnable
        public final void run() {
            Task taskC;
            long jNanoTime;
            while (true) {
                TaskRunner taskRunner = this.f22284a;
                synchronized (taskRunner) {
                    taskC = taskRunner.c();
                }
                if (taskC == null) {
                    return;
                }
                TaskQueue taskQueue = taskC.f22263c;
                o.e(taskQueue);
                TaskRunner taskRunner2 = this.f22284a;
                TaskRunner.f22273h.getClass();
                boolean zIsLoggable = TaskRunner.f22275j.isLoggable(Level.FINE);
                if (zIsLoggable) {
                    TaskRunner.RealBackend realBackend = taskQueue.f22265a.f22276a;
                    jNanoTime = System.nanoTime();
                    TaskLoggerKt.a(taskC, taskQueue, "starting");
                } else {
                    jNanoTime = -1;
                }
                try {
                    TaskRunner.a(taskRunner2, taskC);
                    if (zIsLoggable) {
                        TaskRunner.RealBackend realBackend2 = taskQueue.f22265a.f22276a;
                        TaskLoggerKt.a(taskC, taskQueue, "finished run in ".concat(TaskLoggerKt.b(System.nanoTime() - jNanoTime)));
                    }
                } finally {
                }
            }
        }
    };

    public interface Backend {
    }

    public static final class Companion {
        public /* synthetic */ Companion(int i6) {
            this();
        }

        private Companion() {
        }
    }

    public static final class RealBackend implements Backend {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        public final ThreadPoolExecutor f22283a;

        public RealBackend(a aVar) {
            this.f22283a = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue(), aVar);
        }

        public final void a(TaskRunner$runnable$1 runnable) {
            o.h(runnable, "runnable");
            this.f22283a.execute(runnable);
        }
    }

    static {
        String name = Util.f22185g + " TaskRunner";
        o.h(name, "name");
        f22274i = new TaskRunner(new RealBackend(new a(name, true)));
        Logger logger = Logger.getLogger(TaskRunner.class.getName());
        o.g(logger, "getLogger(TaskRunner::class.java.name)");
        f22275j = logger;
    }

    /* JADX WARN: Type inference failed for: r1v4, types: [okhttp3.internal.concurrent.TaskRunner$runnable$1] */
    public TaskRunner(RealBackend realBackend) {
        this.f22276a = realBackend;
    }

    public static final void a(TaskRunner taskRunner, Task task) {
        taskRunner.getClass();
        byte[] bArr = Util.f22179a;
        Thread threadCurrentThread = Thread.currentThread();
        String name = threadCurrentThread.getName();
        threadCurrentThread.setName(task.f22261a);
        try {
            long jA = task.a();
            synchronized (taskRunner) {
                taskRunner.b(task, jA);
            }
            threadCurrentThread.setName(name);
        } catch (Throwable th) {
            synchronized (taskRunner) {
                taskRunner.b(task, -1L);
                threadCurrentThread.setName(name);
                throw th;
            }
        }
    }

    public final void b(Task task, long j4) {
        byte[] bArr = Util.f22179a;
        TaskQueue taskQueue = task.f22263c;
        o.e(taskQueue);
        if (taskQueue.f22268d != task) {
            throw new IllegalStateException("Check failed.");
        }
        boolean z6 = taskQueue.f22270f;
        taskQueue.f22270f = false;
        taskQueue.f22268d = null;
        this.f22280e.remove(taskQueue);
        if (j4 != -1 && !z6 && !taskQueue.f22267c) {
            taskQueue.e(task, j4, true);
        }
        if (taskQueue.f22269e.isEmpty()) {
            return;
        }
        this.f22281f.add(taskQueue);
    }

    public final Task c() {
        long j4;
        Task task;
        boolean z6;
        byte[] bArr = Util.f22179a;
        while (true) {
            ArrayList arrayList = this.f22281f;
            if (arrayList.isEmpty()) {
                return null;
            }
            RealBackend realBackend = this.f22276a;
            long jNanoTime = System.nanoTime();
            Iterator it = arrayList.iterator();
            long jMin = Long.MAX_VALUE;
            Task task2 = null;
            while (true) {
                if (!it.hasNext()) {
                    j4 = jNanoTime;
                    task = null;
                    z6 = false;
                    break;
                }
                Task task3 = (Task) ((TaskQueue) it.next()).f22269e.get(0);
                j4 = jNanoTime;
                task = null;
                long jMax = Math.max(0L, task3.f22264d - j4);
                if (jMax > 0) {
                    jMin = Math.min(jMax, jMin);
                } else {
                    if (task2 != null) {
                        z6 = true;
                        break;
                    }
                    task2 = task3;
                }
                jNanoTime = j4;
            }
            ArrayList arrayList2 = this.f22280e;
            if (task2 != null) {
                byte[] bArr2 = Util.f22179a;
                task2.f22264d = -1L;
                TaskQueue taskQueue = task2.f22263c;
                o.e(taskQueue);
                taskQueue.f22269e.remove(task2);
                arrayList.remove(taskQueue);
                taskQueue.f22268d = task2;
                arrayList2.add(taskQueue);
                if (z6 || (!this.f22278c && !arrayList.isEmpty())) {
                    realBackend.a(this.f22282g);
                }
                return task2;
            }
            if (this.f22278c) {
                if (jMin >= this.f22279d - j4) {
                    return task;
                }
                notify();
                return task;
            }
            this.f22278c = true;
            this.f22279d = j4 + jMin;
            try {
                try {
                    long j6 = jMin / 1000000;
                    long j7 = jMin - (1000000 * j6);
                    if (j6 > 0 || jMin > 0) {
                        wait(j6, (int) j7);
                    }
                } catch (InterruptedException unused) {
                    for (int size = arrayList2.size() - 1; -1 < size; size--) {
                        ((TaskQueue) arrayList2.get(size)).b();
                    }
                    for (int size2 = arrayList.size() - 1; -1 < size2; size2--) {
                        TaskQueue taskQueue2 = (TaskQueue) arrayList.get(size2);
                        taskQueue2.b();
                        if (taskQueue2.f22269e.isEmpty()) {
                            arrayList.remove(size2);
                        }
                    }
                }
            } finally {
                this.f22278c = false;
            }
        }
    }

    public final void d(TaskQueue taskQueue) {
        o.h(taskQueue, "taskQueue");
        byte[] bArr = Util.f22179a;
        if (taskQueue.f22268d == null) {
            boolean zIsEmpty = taskQueue.f22269e.isEmpty();
            ArrayList arrayList = this.f22281f;
            if (zIsEmpty) {
                arrayList.remove(taskQueue);
            } else {
                o.h(arrayList, "<this>");
                if (!arrayList.contains(taskQueue)) {
                    arrayList.add(taskQueue);
                }
            }
        }
        boolean z6 = this.f22278c;
        RealBackend realBackend = this.f22276a;
        if (z6) {
            notify();
        } else {
            realBackend.a(this.f22282g);
        }
    }

    public final TaskQueue e() {
        int i6;
        synchronized (this) {
            i6 = this.f22277b;
            this.f22277b = i6 + 1;
        }
        return new TaskQueue(this, Z0.o.l("Q", i6));
    }
}
