package yq;

import com.google.android.gms.common.api.Api;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.BlockingQueue;
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.jvm.internal.k;
import kotlin.jvm.internal.t;
import rm.n0;
import vq.m;
import vq.p;

/* loaded from: classes3.dex */
public final class d {

    /* renamed from: i, reason: collision with root package name */
    public static final b f52927i = new b(null);

    /* renamed from: j, reason: collision with root package name */
    private static final Logger f52928j;

    /* renamed from: k, reason: collision with root package name */
    public static final d f52929k;

    /* renamed from: a, reason: collision with root package name */
    private final a f52930a;

    /* renamed from: b, reason: collision with root package name */
    private final Logger f52931b;

    /* renamed from: c, reason: collision with root package name */
    private int f52932c;

    /* renamed from: d, reason: collision with root package name */
    private boolean f52933d;

    /* renamed from: e, reason: collision with root package name */
    private long f52934e;

    /* renamed from: f, reason: collision with root package name */
    private final List f52935f;

    /* renamed from: g, reason: collision with root package name */
    private final List f52936g;

    /* renamed from: h, reason: collision with root package name */
    private final Runnable f52937h;

    /* loaded from: classes3.dex */
    public interface a {
        BlockingQueue a(BlockingQueue blockingQueue);

        void b(d dVar);

        void c(d dVar, Runnable runnable);

        void d(d dVar, long j10);

        long nanoTime();
    }

    /* loaded from: classes3.dex */
    public static final class b {
        private b() {
        }

        public /* synthetic */ b(k kVar) {
            this();
        }
    }

    /* loaded from: classes3.dex */
    public static final class c implements a {

        /* renamed from: a, reason: collision with root package name */
        private final ThreadPoolExecutor f52938a;

        public c(ThreadFactory threadFactory) {
            t.g(threadFactory, "threadFactory");
            this.f52938a = new ThreadPoolExecutor(0, Api.BaseClientBuilder.API_PRIORITY_OTHER, 60L, TimeUnit.SECONDS, new SynchronousQueue(), threadFactory);
        }

        @Override // yq.d.a
        public BlockingQueue a(BlockingQueue queue) {
            t.g(queue, "queue");
            return queue;
        }

        @Override // yq.d.a
        public void b(d taskRunner) {
            t.g(taskRunner, "taskRunner");
            taskRunner.notify();
        }

        @Override // yq.d.a
        public void c(d taskRunner, Runnable runnable) {
            t.g(taskRunner, "taskRunner");
            t.g(runnable, "runnable");
            this.f52938a.execute(runnable);
        }

        @Override // yq.d.a
        public void d(d taskRunner, long j10) {
            t.g(taskRunner, "taskRunner");
            long j11 = j10 / 1000000;
            long j12 = j10 - (1000000 * j11);
            if (j11 > 0 || j10 > 0) {
                taskRunner.wait(j11, (int) j12);
            }
        }

        @Override // yq.d.a
        public long nanoTime() {
            return System.nanoTime();
        }
    }

    /* renamed from: yq.d$d, reason: collision with other inner class name */
    /* loaded from: classes3.dex */
    public static final class RunnableC1048d implements Runnable {
        RunnableC1048d() {
        }

        @Override // java.lang.Runnable
        public void run() {
            yq.a c10;
            long j10;
            while (true) {
                d dVar = d.this;
                synchronized (dVar) {
                    c10 = dVar.c();
                }
                if (c10 == null) {
                    return;
                }
                Logger g10 = d.this.g();
                yq.c d10 = c10.d();
                t.d(d10);
                d dVar2 = d.this;
                boolean isLoggable = g10.isLoggable(Level.FINE);
                if (isLoggable) {
                    j10 = d10.j().f().nanoTime();
                    yq.b.c(g10, c10, d10, "starting");
                } else {
                    j10 = -1;
                }
                try {
                    try {
                        dVar2.j(c10);
                        n0 n0Var = n0.f34904a;
                        if (isLoggable) {
                            yq.b.c(g10, c10, d10, "finished run in " + yq.b.b(d10.j().f().nanoTime() - j10));
                        }
                    } catch (Throwable th2) {
                        synchronized (dVar2) {
                            dVar2.f().c(dVar2, this);
                            n0 n0Var2 = n0.f34904a;
                            throw th2;
                        }
                    }
                } catch (Throwable th3) {
                    if (isLoggable) {
                        yq.b.c(g10, c10, d10, "failed a run in " + yq.b.b(d10.j().f().nanoTime() - j10));
                    }
                    throw th3;
                }
            }
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    static {
        Logger logger = Logger.getLogger(d.class.getName());
        t.f(logger, "getLogger(TaskRunner::class.java.name)");
        f52928j = logger;
        f52929k = new d(new c(p.o(p.f40483f + " TaskRunner", true)), 0 == true ? 1 : 0, 2, 0 == true ? 1 : 0);
    }

    public d(a backend, Logger logger) {
        t.g(backend, "backend");
        t.g(logger, "logger");
        this.f52930a = backend;
        this.f52931b = logger;
        this.f52932c = 10000;
        this.f52935f = new ArrayList();
        this.f52936g = new ArrayList();
        this.f52937h = new RunnableC1048d();
    }

    private final void b(yq.a aVar, long j10) {
        if (p.f40482e && !Thread.holdsLock(this)) {
            throw new AssertionError("Thread " + Thread.currentThread().getName() + " MUST hold lock on " + this);
        }
        yq.c d10 = aVar.d();
        t.d(d10);
        if (!(d10.e() == aVar)) {
            throw new IllegalStateException("Check failed.".toString());
        }
        boolean f10 = d10.f();
        d10.p(false);
        d10.o(null);
        this.f52935f.remove(d10);
        if (j10 != -1 && !f10 && !d10.i()) {
            d10.n(aVar, j10, true);
        }
        if (!d10.g().isEmpty()) {
            this.f52936g.add(d10);
        }
    }

    private final void d(yq.a aVar) {
        if (p.f40482e && !Thread.holdsLock(this)) {
            throw new AssertionError("Thread " + Thread.currentThread().getName() + " MUST hold lock on " + this);
        }
        aVar.g(-1L);
        yq.c d10 = aVar.d();
        t.d(d10);
        d10.g().remove(aVar);
        this.f52936g.remove(d10);
        d10.o(aVar);
        this.f52935f.add(d10);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final void j(yq.a aVar) {
        Thread currentThread = Thread.currentThread();
        String name = currentThread.getName();
        currentThread.setName(aVar.b());
        try {
            long f10 = aVar.f();
            synchronized (this) {
                b(aVar, f10);
                n0 n0Var = n0.f34904a;
            }
            currentThread.setName(name);
        } catch (Throwable th2) {
            synchronized (this) {
                b(aVar, -1L);
                n0 n0Var2 = n0.f34904a;
                currentThread.setName(name);
                throw th2;
            }
        }
    }

    public final yq.a c() {
        boolean z10;
        if (p.f40482e && !Thread.holdsLock(this)) {
            throw new AssertionError("Thread " + Thread.currentThread().getName() + " MUST hold lock on " + this);
        }
        while (!this.f52936g.isEmpty()) {
            long nanoTime = this.f52930a.nanoTime();
            Iterator it = this.f52936g.iterator();
            long j10 = Long.MAX_VALUE;
            yq.a aVar = null;
            while (true) {
                if (!it.hasNext()) {
                    z10 = false;
                    break;
                }
                yq.a aVar2 = (yq.a) ((yq.c) it.next()).g().get(0);
                long max = Math.max(0L, aVar2.c() - nanoTime);
                if (max > 0) {
                    j10 = Math.min(max, j10);
                } else {
                    if (aVar != null) {
                        z10 = true;
                        break;
                    }
                    aVar = aVar2;
                }
            }
            if (aVar != null) {
                d(aVar);
                if (z10 || (!this.f52933d && (!this.f52936g.isEmpty()))) {
                    this.f52930a.c(this, this.f52937h);
                }
                return aVar;
            }
            if (this.f52933d) {
                if (j10 < this.f52934e - nanoTime) {
                    this.f52930a.b(this);
                }
                return null;
            }
            this.f52933d = true;
            this.f52934e = nanoTime + j10;
            try {
                try {
                    this.f52930a.d(this, j10);
                } catch (InterruptedException unused) {
                    e();
                }
            } finally {
                this.f52933d = false;
            }
        }
        return null;
    }

    public final void e() {
        if (p.f40482e && !Thread.holdsLock(this)) {
            throw new AssertionError("Thread " + Thread.currentThread().getName() + " MUST hold lock on " + this);
        }
        int size = this.f52935f.size();
        while (true) {
            size--;
            if (-1 >= size) {
                break;
            } else {
                ((yq.c) this.f52935f.get(size)).b();
            }
        }
        for (int size2 = this.f52936g.size() - 1; -1 < size2; size2--) {
            yq.c cVar = (yq.c) this.f52936g.get(size2);
            cVar.b();
            if (cVar.g().isEmpty()) {
                this.f52936g.remove(size2);
            }
        }
    }

    public final a f() {
        return this.f52930a;
    }

    public final Logger g() {
        return this.f52931b;
    }

    public final void h(yq.c taskQueue) {
        t.g(taskQueue, "taskQueue");
        if (p.f40482e && !Thread.holdsLock(this)) {
            throw new AssertionError("Thread " + Thread.currentThread().getName() + " MUST hold lock on " + this);
        }
        if (taskQueue.e() == null) {
            if (!taskQueue.g().isEmpty()) {
                m.a(this.f52936g, taskQueue);
            } else {
                this.f52936g.remove(taskQueue);
            }
        }
        if (this.f52933d) {
            this.f52930a.b(this);
        } else {
            this.f52930a.c(this, this.f52937h);
        }
    }

    public final yq.c i() {
        int i10;
        synchronized (this) {
            i10 = this.f52932c;
            this.f52932c = i10 + 1;
        }
        StringBuilder sb2 = new StringBuilder();
        sb2.append('Q');
        sb2.append(i10);
        return new yq.c(this, sb2.toString());
    }

    public /* synthetic */ d(a aVar, Logger logger, int i10, k kVar) {
        this(aVar, (i10 & 2) != 0 ? f52928j : logger);
    }
}
