package okhttp3.n0.concurrent;

import com.cvte.vman.instance.InvokeCmd;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import kotlin.Metadata;
import kotlin.jvm.internal.DefaultConstructorMarker;
import kotlin.jvm.internal.o;

/* JADX INFO: renamed from: y.n0.f.d, reason: from Kotlin metadata */
/* JADX INFO: loaded from: classes2.dex */
@Metadata(d1 = {"\u0000J\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0004\n\u0002\u0010!\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u000b\n\u0000\n\u0002\u0010\t\n\u0000\n\u0002\u0010\b\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010 \n\u0000\n\u0002\u0010\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\r\u0018\u0000 #2\u00020\u0001:\u0003\"#$B\r\u0012\u0006\u0010\u0002\u001a\u00020\u0003¢\u0006\u0002\u0010\u0004J\f\u0010\u0013\u001a\b\u0012\u0004\u0012\u00020\t0\u0014J\u0018\u0010\u0015\u001a\u00020\u00162\u0006\u0010\u0017\u001a\u00020\u00182\u0006\u0010\u0019\u001a\u00020\rH\u0002J\b\u0010\u001a\u001a\u0004\u0018\u00010\u0018J\u0010\u0010\u001b\u001a\u00020\u00162\u0006\u0010\u0017\u001a\u00020\u0018H\u0002J\u0006\u0010\u001c\u001a\u00020\u0016J\u0015\u0010\u001d\u001a\u00020\u00162\u0006\u0010\u001e\u001a\u00020\tH\u0000¢\u0006\u0002\b\u001fJ\u0006\u0010 \u001a\u00020\tJ\u0010\u0010!\u001a\u00020\u00162\u0006\u0010\u0017\u001a\u00020\u0018H\u0002R\u0011\u0010\u0002\u001a\u00020\u0003¢\u0006\b\n\u0000\u001a\u0004\b\u0005\u0010\u0006R\u0014\u0010\u0007\u001a\b\u0012\u0004\u0012\u00020\t0\bX\u0082\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\n\u001a\u00020\u000bX\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\f\u001a\u00020\rX\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u000e\u001a\u00020\u000fX\u0082\u000e¢\u0006\u0002\n\u0000R\u0014\u0010\u0010\u001a\b\u0012\u0004\u0012\u00020\t0\bX\u0082\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0011\u001a\u00020\u0012X\u0082\u0004¢\u0006\u0002\n\u0000¨\u0006%"}, d2 = {"Lokhttp3/internal/concurrent/TaskRunner;", "", "backend", "Lokhttp3/internal/concurrent/TaskRunner$Backend;", "(Lokhttp3/internal/concurrent/TaskRunner$Backend;)V", "getBackend", "()Lokhttp3/internal/concurrent/TaskRunner$Backend;", "busyQueues", "", "Lokhttp3/internal/concurrent/TaskQueue;", "coordinatorWaiting", "", "coordinatorWakeUpAt", "", "nextQueueName", "", "readyQueues", "runnable", "Ljava/lang/Runnable;", "activeQueues", "", "afterRun", "", "task", "Lokhttp3/internal/concurrent/Task;", "delayNanos", "awaitTaskToRun", "beforeRun", "cancelAll", "kickCoordinator", "taskQueue", "kickCoordinator$okhttp", "newQueue", "runTask", "Backend", "Companion", "RealBackend", "okhttp"}, k = 1, mv = {1, 6, 0}, xi = 48)
public final class TaskRunner {
    public static final b a = new b(null);

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

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

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public int f9733e;

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

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final List<TaskQueue> f9736h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public final List<TaskQueue> f9737i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final Runnable f9738j;

    /* JADX INFO: renamed from: y.n0.f.d$a */
    @Metadata(d1 = {"\u0000(\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\n\u0002\u0010\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0010\t\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\bf\u0018\u00002\u00020\u0001J\u0010\u0010\u0002\u001a\u00020\u00032\u0006\u0010\u0004\u001a\u00020\u0005H&J\u0010\u0010\u0006\u001a\u00020\u00032\u0006\u0010\u0004\u001a\u00020\u0005H&J\u0018\u0010\u0007\u001a\u00020\u00032\u0006\u0010\u0004\u001a\u00020\u00052\u0006\u0010\b\u001a\u00020\tH&J\u0010\u0010\n\u001a\u00020\u00032\u0006\u0010\u000b\u001a\u00020\fH&J\b\u0010\r\u001a\u00020\tH&¨\u0006\u000e"}, d2 = {"Lokhttp3/internal/concurrent/TaskRunner$Backend;", "", "beforeTask", "", "taskRunner", "Lokhttp3/internal/concurrent/TaskRunner;", "coordinatorNotify", "coordinatorWait", "nanos", "", "execute", "runnable", "Ljava/lang/Runnable;", "nanoTime", "okhttp"}, k = 1, mv = {1, 6, 0}, xi = 48)
    public interface a {
        void a(TaskRunner taskRunner);

        void b(TaskRunner taskRunner, long j2);

        long c();

        void execute(Runnable runnable);
    }

    /* JADX INFO: renamed from: y.n0.f.d$b */
    @Metadata(d1 = {"\u0000\u001a\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0003\b\u0086\u0003\u0018\u00002\u00020\u0001B\u0007\b\u0002¢\u0006\u0002\u0010\u0002R\u0010\u0010\u0003\u001a\u00020\u00048\u0006X\u0087\u0004¢\u0006\u0002\n\u0000R\u0011\u0010\u0005\u001a\u00020\u0006¢\u0006\b\n\u0000\u001a\u0004\b\u0007\u0010\b¨\u0006\t"}, d2 = {"Lokhttp3/internal/concurrent/TaskRunner$Companion;", "", "()V", "INSTANCE", "Lokhttp3/internal/concurrent/TaskRunner;", "logger", "Ljava/util/logging/Logger;", "getLogger", "()Ljava/util/logging/Logger;", "okhttp"}, k = 1, mv = {1, 6, 0}, xi = 48)
    public static final class b {
        public b(DefaultConstructorMarker defaultConstructorMarker) {
        }
    }

    /* JADX INFO: renamed from: y.n0.f.d$c */
    @Metadata(d1 = {"\u00006\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0010\t\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0003\u0018\u00002\u00020\u0001B\r\u0012\u0006\u0010\u0002\u001a\u00020\u0003¢\u0006\u0002\u0010\u0004J\u0010\u0010\u0007\u001a\u00020\b2\u0006\u0010\t\u001a\u00020\nH\u0016J\u0010\u0010\u000b\u001a\u00020\b2\u0006\u0010\t\u001a\u00020\nH\u0016J\u0018\u0010\f\u001a\u00020\b2\u0006\u0010\t\u001a\u00020\n2\u0006\u0010\r\u001a\u00020\u000eH\u0016J\u0010\u0010\u000f\u001a\u00020\b2\u0006\u0010\u0010\u001a\u00020\u0011H\u0016J\b\u0010\u0012\u001a\u00020\u000eH\u0016J\u0006\u0010\u0013\u001a\u00020\bR\u000e\u0010\u0005\u001a\u00020\u0006X\u0082\u0004¢\u0006\u0002\n\u0000¨\u0006\u0014"}, d2 = {"Lokhttp3/internal/concurrent/TaskRunner$RealBackend;", "Lokhttp3/internal/concurrent/TaskRunner$Backend;", "threadFactory", "Ljava/util/concurrent/ThreadFactory;", "(Ljava/util/concurrent/ThreadFactory;)V", "executor", "Ljava/util/concurrent/ThreadPoolExecutor;", "beforeTask", "", "taskRunner", "Lokhttp3/internal/concurrent/TaskRunner;", "coordinatorNotify", "coordinatorWait", "nanos", "", "execute", "runnable", "Ljava/lang/Runnable;", "nanoTime", "shutdown", "okhttp"}, k = 1, mv = {1, 6, 0}, xi = 48)
    public static final class c implements a {
        public final ThreadPoolExecutor a;

        public c(ThreadFactory threadFactory) {
            o.f(threadFactory, "threadFactory");
            this.a = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue(), threadFactory);
        }

        @Override // okhttp3.n0.concurrent.TaskRunner.a
        public void a(TaskRunner taskRunner) {
            o.f(taskRunner, "taskRunner");
            taskRunner.notify();
        }

        @Override // okhttp3.n0.concurrent.TaskRunner.a
        public void b(TaskRunner taskRunner, long j2) throws InterruptedException {
            o.f(taskRunner, "taskRunner");
            long j3 = j2 / 1000000;
            long j4 = j2 - (1000000 * j3);
            if (j3 > 0 || j2 > 0) {
                taskRunner.wait(j3, (int) j4);
            }
        }

        @Override // okhttp3.n0.concurrent.TaskRunner.a
        public long c() {
            return System.nanoTime();
        }

        @Override // okhttp3.n0.concurrent.TaskRunner.a
        public void execute(Runnable runnable) {
            o.f(runnable, "runnable");
            this.a.execute(runnable);
        }
    }

    /* JADX INFO: renamed from: y.n0.f.d$d */
    @Metadata(d1 = {"\u0000\u0011\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u0002\n\u0000*\u0001\u0000\b\n\u0018\u00002\u00020\u0001J\b\u0010\u0002\u001a\u00020\u0003H\u0016¨\u0006\u0004"}, d2 = {"okhttp3/internal/concurrent/TaskRunner$runnable$1", "Ljava/lang/Runnable;", "run", "", "okhttp"}, k = 1, mv = {1, 6, 0}, xi = 48)
    public static final class d implements Runnable {
        public d() {
        }

        @Override // java.lang.Runnable
        public void run() {
            Task taskC;
            while (true) {
                TaskRunner taskRunner = TaskRunner.this;
                synchronized (taskRunner) {
                    taskC = taskRunner.c();
                }
                if (taskC == null) {
                    return;
                }
                TaskQueue taskQueue = taskC.f9722c;
                o.c(taskQueue);
                TaskRunner taskRunner2 = TaskRunner.this;
                long jC = -1;
                b bVar = TaskRunner.a;
                boolean zIsLoggable = TaskRunner.f9731c.isLoggable(Level.FINE);
                if (zIsLoggable) {
                    jC = taskQueue.a.f9732d.c();
                    kotlin.reflect.w.internal.y0.n.c2.c.d(taskC, taskQueue, "starting");
                }
                try {
                    TaskRunner.a(taskRunner2, taskC);
                    if (zIsLoggable) {
                        kotlin.reflect.w.internal.y0.n.c2.c.d(taskC, taskQueue, o.l("finished run in ", kotlin.reflect.w.internal.y0.n.c2.c.N(taskQueue.a.f9732d.c() - jC)));
                    }
                } finally {
                }
            }
        }
    }

    static {
        String strL = o.l(okhttp3.n0.c.f9718h, " TaskRunner");
        o.f(strL, "name");
        f9730b = new TaskRunner(new c(new okhttp3.n0.a(strL, true)));
        Logger logger = Logger.getLogger(TaskRunner.class.getName());
        o.e(logger, "getLogger(TaskRunner::class.java.name)");
        f9731c = logger;
    }

    public TaskRunner(a aVar) {
        o.f(aVar, "backend");
        this.f9732d = aVar;
        this.f9733e = InvokeCmd.CMD_PMSCTRL_SET_PMS_WAKEUP_SOURCES;
        this.f9736h = new ArrayList();
        this.f9737i = new ArrayList();
        this.f9738j = new d();
    }

    public static final void a(TaskRunner taskRunner, Task task) {
        Objects.requireNonNull(taskRunner);
        byte[] bArr = okhttp3.n0.c.a;
        Thread threadCurrentThread = Thread.currentThread();
        String name = threadCurrentThread.getName();
        threadCurrentThread.setName(task.a);
        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 j2) {
        byte[] bArr = okhttp3.n0.c.a;
        TaskQueue taskQueue = task.f9722c;
        o.c(taskQueue);
        if (!(taskQueue.f9727d == task)) {
            throw new IllegalStateException("Check failed.".toString());
        }
        boolean z2 = taskQueue.f9729f;
        taskQueue.f9729f = false;
        taskQueue.f9727d = null;
        this.f9736h.remove(taskQueue);
        if (j2 != -1 && !z2 && !taskQueue.f9726c) {
            taskQueue.d(task, j2, true);
        }
        if (!taskQueue.f9728e.isEmpty()) {
            this.f9737i.add(taskQueue);
        }
    }

    public final Task c() {
        boolean z2;
        byte[] bArr = okhttp3.n0.c.a;
        while (!this.f9737i.isEmpty()) {
            long jC = this.f9732d.c();
            long jMin = Long.MAX_VALUE;
            Iterator<TaskQueue> it = this.f9737i.iterator();
            Task task = null;
            while (true) {
                if (!it.hasNext()) {
                    z2 = false;
                    break;
                }
                Task task2 = it.next().f9728e.get(0);
                long jMax = Math.max(0L, task2.f9723d - jC);
                if (jMax > 0) {
                    jMin = Math.min(jMax, jMin);
                } else {
                    if (task != null) {
                        z2 = true;
                        break;
                    }
                    task = task2;
                }
            }
            if (task != null) {
                byte[] bArr2 = okhttp3.n0.c.a;
                task.f9723d = -1L;
                TaskQueue taskQueue = task.f9722c;
                o.c(taskQueue);
                taskQueue.f9728e.remove(task);
                this.f9737i.remove(taskQueue);
                taskQueue.f9727d = task;
                this.f9736h.add(taskQueue);
                if (z2 || (!this.f9734f && (!this.f9737i.isEmpty()))) {
                    this.f9732d.execute(this.f9738j);
                }
                return task;
            }
            if (this.f9734f) {
                if (jMin < this.f9735g - jC) {
                    this.f9732d.a(this);
                }
                return null;
            }
            this.f9734f = true;
            this.f9735g = jC + jMin;
            try {
                try {
                    this.f9732d.b(this, jMin);
                } catch (InterruptedException unused) {
                    d();
                }
            } finally {
                this.f9734f = false;
            }
        }
        return null;
    }

    public final void d() {
        int size = this.f9736h.size() - 1;
        if (size >= 0) {
            while (true) {
                int i2 = size - 1;
                this.f9736h.get(size).b();
                if (i2 < 0) {
                    break;
                } else {
                    size = i2;
                }
            }
        }
        int size2 = this.f9737i.size() - 1;
        if (size2 < 0) {
            return;
        }
        while (true) {
            int i3 = size2 - 1;
            TaskQueue taskQueue = this.f9737i.get(size2);
            taskQueue.b();
            if (taskQueue.f9728e.isEmpty()) {
                this.f9737i.remove(size2);
            }
            if (i3 < 0) {
                return;
            } else {
                size2 = i3;
            }
        }
    }

    public final void e(TaskQueue taskQueue) {
        o.f(taskQueue, "taskQueue");
        byte[] bArr = okhttp3.n0.c.a;
        if (taskQueue.f9727d == null) {
            if (!taskQueue.f9728e.isEmpty()) {
                List<TaskQueue> list = this.f9737i;
                o.f(list, "<this>");
                if (!list.contains(taskQueue)) {
                    list.add(taskQueue);
                }
            } else {
                this.f9737i.remove(taskQueue);
            }
        }
        if (this.f9734f) {
            this.f9732d.a(this);
        } else {
            this.f9732d.execute(this.f9738j);
        }
    }

    public final TaskQueue f() {
        int i2;
        synchronized (this) {
            i2 = this.f9733e;
            this.f9733e = i2 + 1;
        }
        return new TaskQueue(this, o.l("Q", Integer.valueOf(i2)));
    }
}
