package o;

import com.google.android.gms.common.internal.Preconditions;
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;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* loaded from: classes.dex */
public final class Fr0 extends AbstractC1429hs0 {
    public static final AtomicLong k = new AtomicLong(Long.MIN_VALUE);
    public Dr0 c;
    public Dr0 d;
    public final PriorityBlockingQueue e;
    public final LinkedBlockingQueue f;
    public final Ar0 g;
    public final Ar0 h;
    public final Object i;
    public final Semaphore j;

    public Fr0(Kr0 kr0) {
        super(kr0);
        this.i = new Object();
        this.j = new Semaphore(2);
        this.e = new PriorityBlockingQueue();
        this.f = new LinkedBlockingQueue();
        this.g = new Ar0(this, "Thread death: Uncaught exception on worker thread");
        this.h = new Ar0(this, "Thread death: Uncaught exception on network thread");
    }

    public final Cr0 A(Callable callable) {
        u();
        Preconditions.checkNotNull(callable);
        Cr0 cr0 = new Cr0(this, callable, true);
        if (Thread.currentThread() == this.c) {
            cr0.run();
            return cr0;
        }
        F(cr0);
        return cr0;
    }

    public final void B(Runnable runnable) {
        u();
        Preconditions.checkNotNull(runnable);
        F(new Cr0(this, runnable, false, "Task exception on worker thread"));
    }

    public final Object C(AtomicReference atomicReference, long j, String str, Runnable runnable) {
        synchronized (atomicReference) {
            Fr0 fr0 = ((Kr0) this.a).g;
            Kr0.l(fr0);
            fr0.B(runnable);
            try {
                atomicReference.wait(j);
            } catch (InterruptedException unused) {
                Nq0 nq0 = ((Kr0) this.a).f;
                Kr0.l(nq0);
                C1984nl c1984nl = nq0.i;
                StringBuilder sb = new StringBuilder(str.length() + 24);
                sb.append("Interrupted waiting for ");
                sb.append(str);
                c1984nl.b(sb.toString());
                return null;
            }
        }
        Object obj = atomicReference.get();
        if (obj == null) {
            Nq0 nq02 = ((Kr0) this.a).f;
            Kr0.l(nq02);
            nq02.i.b("Timed out waiting for ".concat(str));
        }
        return obj;
    }

    public final void D(Runnable runnable) {
        u();
        Preconditions.checkNotNull(runnable);
        F(new Cr0(this, runnable, true, "Task exception on worker thread"));
    }

    public final void E(Runnable runnable) {
        u();
        Preconditions.checkNotNull(runnable);
        Cr0 cr0 = new Cr0(this, runnable, false, "Task exception on network thread");
        synchronized (this.i) {
            try {
                LinkedBlockingQueue linkedBlockingQueue = this.f;
                linkedBlockingQueue.add(cr0);
                Dr0 dr0 = this.d;
                if (dr0 == null) {
                    Dr0 dr02 = new Dr0(this, "Measurement Network", linkedBlockingQueue);
                    this.d = dr02;
                    dr02.setUncaughtExceptionHandler(this.h);
                    this.d.start();
                } else {
                    Object obj = dr0.l;
                    synchronized (obj) {
                        obj.notifyAll();
                    }
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void F(Cr0 cr0) {
        synchronized (this.i) {
            try {
                PriorityBlockingQueue priorityBlockingQueue = this.e;
                priorityBlockingQueue.add(cr0);
                Dr0 dr0 = this.c;
                if (dr0 == null) {
                    Dr0 dr02 = new Dr0(this, "Measurement Worker", priorityBlockingQueue);
                    this.c = dr02;
                    dr02.setUncaughtExceptionHandler(this.g);
                    this.c.start();
                } else {
                    Object obj = dr0.l;
                    synchronized (obj) {
                        obj.notifyAll();
                    }
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    @Override // o.Eu0
    public final void r() {
        if (Thread.currentThread() != this.c) {
            throw new IllegalStateException("Call expected from worker thread");
        }
    }

    @Override // o.AbstractC1429hs0
    public final boolean t() {
        return false;
    }

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

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

    public final boolean y() {
        return Thread.currentThread() == this.c;
    }

    public final Cr0 z(Callable callable) {
        u();
        Preconditions.checkNotNull(callable);
        Cr0 cr0 = new Cr0(this, callable, false);
        if (Thread.currentThread() != this.c) {
            F(cr0);
            return cr0;
        }
        if (!this.e.isEmpty()) {
            Nq0 nq0 = ((Kr0) this.a).f;
            Kr0.l(nq0);
            nq0.i.b("Callable skipped the worker queue.");
        }
        cr0.run();
        return cr0;
    }
}
