package d3;

import java.util.concurrent.Callable;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;

/* JADX INFO: renamed from: d3.q0, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C0815q0 extends A0 {

    /* JADX INFO: renamed from: A, reason: collision with root package name */
    public static final AtomicLong f12104A = new AtomicLong(Long.MIN_VALUE);

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public C0812p0 f12105s;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public C0812p0 f12106t;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public final PriorityBlockingQueue f12107u;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    public final LinkedBlockingQueue f12108v;

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public final C0806n0 f12109w;

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    public final C0806n0 f12110x;

    /* JADX INFO: renamed from: y, reason: collision with root package name */
    public final Object f12111y;

    /* JADX INFO: renamed from: z, reason: collision with root package name */
    public final Semaphore f12112z;

    public C0815q0(C0817r0 c0817r0) {
        super(c0817r0);
        this.f12111y = new Object();
        this.f12112z = new Semaphore(2);
        this.f12107u = new PriorityBlockingQueue();
        this.f12108v = new LinkedBlockingQueue();
        this.f12109w = new C0806n0(this, "Thread death: Uncaught exception on worker thread");
        this.f12110x = new C0806n0(this, "Thread death: Uncaught exception on network thread");
    }

    @Override // O3.e
    public final void E() {
        if (Thread.currentThread() != this.f12105s) {
            throw new IllegalStateException("Call expected from worker thread");
        }
    }

    @Override // d3.A0
    public final boolean F() {
        return false;
    }

    public final void I() {
        if (Thread.currentThread() != this.f12106t) {
            throw new IllegalStateException("Call expected from network thread");
        }
    }

    public final Object J(AtomicReference atomicReference, long j8, String str, Runnable runnable) {
        synchronized (atomicReference) {
            C0815q0 c0815q0 = ((C0817r0) this.f5146q).f12151z;
            C0817r0.k(c0815q0);
            c0815q0.O(runnable);
            try {
                atomicReference.wait(j8);
            } catch (InterruptedException unused) {
                Z z8 = ((C0817r0) this.f5146q).f12150y;
                C0817r0.k(z8);
                z8.f11878y.b("Interrupted waiting for ".concat(str));
                return null;
            }
        }
        Object obj = atomicReference.get();
        if (obj == null) {
            Z z9 = ((C0817r0) this.f5146q).f12150y;
            C0817r0.k(z9);
            z9.f11878y.b("Timed out waiting for ".concat(str));
        }
        return obj;
    }

    public final C0809o0 K(Callable callable) {
        G();
        C0809o0 c0809o0 = new C0809o0(this, callable, false);
        if (Thread.currentThread() != this.f12105s) {
            R(c0809o0);
            return c0809o0;
        }
        if (!this.f12107u.isEmpty()) {
            Z z8 = ((C0817r0) this.f5146q).f12150y;
            C0817r0.k(z8);
            z8.f11878y.b("Callable skipped the worker queue.");
        }
        c0809o0.run();
        return c0809o0;
    }

    public final C0809o0 L(Callable callable) {
        G();
        C0809o0 c0809o0 = new C0809o0(this, callable, true);
        if (Thread.currentThread() == this.f12105s) {
            c0809o0.run();
            return c0809o0;
        }
        R(c0809o0);
        return c0809o0;
    }

    public final void M() {
        if (Thread.currentThread() == this.f12105s) {
            throw new IllegalStateException("Call not expected from worker thread");
        }
    }

    public final void N(Runnable runnable) {
        G();
        C0809o0 c0809o0 = new C0809o0(this, runnable, false, "Task exception on network thread");
        synchronized (this.f12111y) {
            try {
                LinkedBlockingQueue linkedBlockingQueue = this.f12108v;
                linkedBlockingQueue.add(c0809o0);
                C0812p0 c0812p0 = this.f12106t;
                if (c0812p0 == null) {
                    C0812p0 c0812p02 = new C0812p0(this, "Measurement Network", linkedBlockingQueue);
                    this.f12106t = c0812p02;
                    c0812p02.setUncaughtExceptionHandler(this.f12110x);
                    this.f12106t.start();
                } else {
                    c0812p0.a();
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void O(Runnable runnable) {
        G();
        P2.w.f(runnable);
        R(new C0809o0(this, runnable, false, "Task exception on worker thread"));
    }

    public final void P(Runnable runnable) {
        G();
        R(new C0809o0(this, runnable, true, "Task exception on worker thread"));
    }

    public final boolean Q() {
        return Thread.currentThread() == this.f12105s;
    }

    public final void R(C0809o0 c0809o0) {
        synchronized (this.f12111y) {
            try {
                PriorityBlockingQueue priorityBlockingQueue = this.f12107u;
                priorityBlockingQueue.add(c0809o0);
                C0812p0 c0812p0 = this.f12105s;
                if (c0812p0 == null) {
                    C0812p0 c0812p02 = new C0812p0(this, "Measurement Worker", priorityBlockingQueue);
                    this.f12105s = c0812p02;
                    c0812p02.setUncaughtExceptionHandler(this.f12109w);
                    this.f12105s.start();
                } else {
                    c0812p0.a();
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }
}
