package com.google.android.gms.internal.ads;

import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.LockSupport;

/* JADX INFO: loaded from: classes2.dex */
abstract class zzgzv extends AtomicReference implements Runnable {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final ig f19815a;

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

    static {
        int i10 = 8;
        int i11 = 0;
        f19815a = new ig(i10, i11);
        f19816b = new ig(i10, i11);
    }

    public abstract Object a();

    public final void b(Thread thread) {
        Runnable runnable = (Runnable) get();
        zzgzt zzgztVar = null;
        boolean z10 = false;
        int i10 = 0;
        while (true) {
            boolean z11 = runnable instanceof zzgzt;
            ig igVar = f19816b;
            if (!z11) {
                if (runnable != igVar) {
                    break;
                }
            } else {
                zzgztVar = (zzgzt) runnable;
            }
            i10++;
            if (i10 <= 1000) {
                Thread.yield();
            } else if (runnable == igVar || compareAndSet(runnable, igVar)) {
                z10 = Thread.interrupted() || z10;
                LockSupport.park(zzgztVar);
            }
            runnable = (Runnable) get();
        }
        if (z10) {
            thread.interrupt();
        }
    }

    public abstract String c();

    public abstract boolean d();

    public abstract void e(Object obj);

    public abstract void f(Throwable th);

    public final void g() {
        ig igVar = f19816b;
        ig igVar2 = f19815a;
        Runnable runnable = (Runnable) get();
        if (runnable instanceof Thread) {
            zzgzt zzgztVar = new zzgzt(this);
            zzgztVar.a(Thread.currentThread());
            if (compareAndSet(runnable, zzgztVar)) {
                try {
                    Thread thread = (Thread) runnable;
                    thread.interrupt();
                    if (((Runnable) getAndSet(igVar2)) == igVar) {
                        LockSupport.unpark(thread);
                    }
                } catch (Throwable th) {
                    if (((Runnable) getAndSet(igVar2)) == igVar) {
                        LockSupport.unpark((Thread) runnable);
                    }
                    throw th;
                }
            }
        }
    }

    @Override // java.lang.Runnable
    public final void run() {
        Thread threadCurrentThread = Thread.currentThread();
        Object objA = null;
        if (compareAndSet(null, threadCurrentThread)) {
            boolean z10 = !d();
            ig igVar = f19815a;
            if (z10) {
                try {
                    objA = a();
                } catch (Throwable th) {
                    try {
                        if (th instanceof InterruptedException) {
                            Thread.currentThread().interrupt();
                        }
                        if (!compareAndSet(threadCurrentThread, igVar)) {
                            b(threadCurrentThread);
                        }
                        f(th);
                        return;
                    } catch (Throwable th2) {
                        if (!compareAndSet(threadCurrentThread, igVar)) {
                            b(threadCurrentThread);
                        }
                        e(null);
                        throw th2;
                    }
                }
            }
            if (!compareAndSet(threadCurrentThread, igVar)) {
                b(threadCurrentThread);
            }
            if (z10) {
                e(objA);
            }
        }
    }

    @Override // java.util.concurrent.atomic.AtomicReference
    public final String toString() {
        String strP;
        Runnable runnable = (Runnable) get();
        if (runnable == f19815a) {
            strP = "running=[DONE]";
        } else if (runnable instanceof zzgzt) {
            strP = "running=[INTERRUPTED]";
        } else if (runnable instanceof Thread) {
            String name = ((Thread) runnable).getName();
            strP = defpackage.e.p(new StringBuilder(String.valueOf(name).length() + 21), "running=[RUNNING ON ", name, "]");
        } else {
            strP = "running=[NOT STARTED YET]";
        }
        String strC = c();
        return defpackage.e.p(new StringBuilder(strP.length() + 2 + String.valueOf(strC).length()), strP, ", ", strC);
    }
}
