package defpackage;

import androidx.credentials.provider.CredentialEntry;
import androidx.media3.exoplayer.upstream.CmcdData;
import java.util.Locale;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import java.util.concurrent.locks.LockSupport;
import java.util.logging.Level;
import java.util.logging.Logger;

/* JADX INFO: loaded from: classes.dex */
@m38
public abstract class h1<V> implements zb5<V> {
    public static final boolean d = Boolean.parseBoolean(System.getProperty("guava.concurrent.generate_cancellation_cause", CredentialEntry.FALSE_STRING));
    public static final Logger e = Logger.getLogger(h1.class.getName());
    public static final b f;
    public static final Object i;
    public volatile Object a;
    public volatile e b;
    public volatile i c;

    public static abstract class b {
        public abstract boolean a(h1 h1Var, e eVar, e eVar2);

        public abstract boolean b(h1 h1Var, Object obj, Object obj2);

        public abstract boolean c(h1 h1Var, i iVar, i iVar2);

        public abstract void d(i iVar, i iVar2);

        public abstract void e(i iVar, Thread thread);
    }

    public static final class c {
        public static final c c;
        public static final c d;
        public final boolean a;
        public final Throwable b;

        static {
            if (h1.d) {
                d = null;
                c = null;
            } else {
                d = new c(null, false);
                c = new c(null, true);
            }
        }

        public c(Throwable th, boolean z) {
            this.a = z;
            this.b = th;
        }
    }

    public static final class d {
        public final Throwable a;

        public class a extends Throwable {
            @Override // java.lang.Throwable
            public final synchronized Throwable fillInStackTrace() {
                return this;
            }
        }

        static {
            new d(new a("Failure occurred while trying to finish a future."));
        }

        public d(Throwable th) {
            boolean z = h1.d;
            th.getClass();
            this.a = th;
        }
    }

    public static final class e {
        public static final e d = new e(null, null);
        public final Runnable a;
        public final Executor b;
        public e c;

        public e(Runnable runnable, Executor executor) {
            this.a = runnable;
            this.b = executor;
        }
    }

    public static final class f extends b {
        public final AtomicReferenceFieldUpdater a;
        public final AtomicReferenceFieldUpdater b;
        public final AtomicReferenceFieldUpdater c;
        public final AtomicReferenceFieldUpdater d;
        public final AtomicReferenceFieldUpdater e;

        public f(AtomicReferenceFieldUpdater atomicReferenceFieldUpdater, AtomicReferenceFieldUpdater atomicReferenceFieldUpdater2, AtomicReferenceFieldUpdater atomicReferenceFieldUpdater3, AtomicReferenceFieldUpdater atomicReferenceFieldUpdater4, AtomicReferenceFieldUpdater atomicReferenceFieldUpdater5) {
            this.a = atomicReferenceFieldUpdater;
            this.b = atomicReferenceFieldUpdater2;
            this.c = atomicReferenceFieldUpdater3;
            this.d = atomicReferenceFieldUpdater4;
            this.e = atomicReferenceFieldUpdater5;
        }

        @Override // h1.b
        public final boolean a(h1 h1Var, e eVar, e eVar2) {
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater;
            do {
                atomicReferenceFieldUpdater = this.d;
                if (atomicReferenceFieldUpdater.compareAndSet(h1Var, eVar, eVar2)) {
                    return true;
                }
            } while (atomicReferenceFieldUpdater.get(h1Var) == eVar);
            return false;
        }

        @Override // h1.b
        public final boolean b(h1 h1Var, Object obj, Object obj2) {
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater;
            do {
                atomicReferenceFieldUpdater = this.e;
                if (atomicReferenceFieldUpdater.compareAndSet(h1Var, obj, obj2)) {
                    return true;
                }
            } while (atomicReferenceFieldUpdater.get(h1Var) == obj);
            return false;
        }

        @Override // h1.b
        public final boolean c(h1 h1Var, i iVar, i iVar2) {
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater;
            do {
                atomicReferenceFieldUpdater = this.c;
                if (atomicReferenceFieldUpdater.compareAndSet(h1Var, iVar, iVar2)) {
                    return true;
                }
            } while (atomicReferenceFieldUpdater.get(h1Var) == iVar);
            return false;
        }

        @Override // h1.b
        public final void d(i iVar, i iVar2) {
            this.b.lazySet(iVar, iVar2);
        }

        @Override // h1.b
        public final void e(i iVar, Thread thread) {
            this.a.lazySet(iVar, thread);
        }
    }

    public static final class g<V> implements Runnable {
        @Override // java.lang.Runnable
        public final void run() {
            throw null;
        }
    }

    public static final class h extends b {
        @Override // h1.b
        public final boolean a(h1 h1Var, e eVar, e eVar2) {
            synchronized (h1Var) {
                try {
                    if (h1Var.b != eVar) {
                        return false;
                    }
                    h1Var.b = eVar2;
                    return true;
                } catch (Throwable th) {
                    throw th;
                }
            }
        }

        @Override // h1.b
        public final boolean b(h1 h1Var, Object obj, Object obj2) {
            synchronized (h1Var) {
                try {
                    if (h1Var.a != obj) {
                        return false;
                    }
                    h1Var.a = obj2;
                    return true;
                } catch (Throwable th) {
                    throw th;
                }
            }
        }

        @Override // h1.b
        public final boolean c(h1 h1Var, i iVar, i iVar2) {
            synchronized (h1Var) {
                try {
                    if (h1Var.c != iVar) {
                        return false;
                    }
                    h1Var.c = iVar2;
                    return true;
                } catch (Throwable th) {
                    throw th;
                }
            }
        }

        @Override // h1.b
        public final void d(i iVar, i iVar2) {
            iVar.b = iVar2;
        }

        @Override // h1.b
        public final void e(i iVar, Thread thread) {
            iVar.a = thread;
        }
    }

    public static final class i {
        public static final i c = new i();
        public volatile Thread a;
        public volatile i b;

        public i() {
            h1.f.e(this, Thread.currentThread());
        }
    }

    static {
        b hVar;
        try {
            hVar = new f(AtomicReferenceFieldUpdater.newUpdater(i.class, Thread.class, CmcdData.OBJECT_TYPE_AUDIO_ONLY), AtomicReferenceFieldUpdater.newUpdater(i.class, i.class, "b"), AtomicReferenceFieldUpdater.newUpdater(h1.class, i.class, "c"), AtomicReferenceFieldUpdater.newUpdater(h1.class, e.class, "b"), AtomicReferenceFieldUpdater.newUpdater(h1.class, Object.class, CmcdData.OBJECT_TYPE_AUDIO_ONLY));
            th = null;
        } catch (Throwable th) {
            th = th;
            hVar = new h();
        }
        f = hVar;
        if (th != null) {
            e.log(Level.SEVERE, "SafeAtomicHelper is broken!", th);
        }
        i = new Object();
    }

    public static void c(h1 h1Var) {
        i iVar;
        e eVar;
        do {
            iVar = h1Var.c;
        } while (!f.c(h1Var, iVar, i.c));
        while (iVar != null) {
            Thread thread = iVar.a;
            if (thread != null) {
                iVar.a = null;
                LockSupport.unpark(thread);
            }
            iVar = iVar.b;
        }
        h1Var.b();
        do {
            eVar = h1Var.b;
        } while (!f.a(h1Var, eVar, e.d));
        e eVar2 = null;
        while (eVar != null) {
            e eVar3 = eVar.c;
            eVar.c = eVar2;
            eVar2 = eVar;
            eVar = eVar3;
        }
        while (eVar2 != null) {
            e eVar4 = eVar2.c;
            Runnable runnable = eVar2.a;
            if (runnable instanceof g) {
                throw null;
            }
            d(runnable, eVar2.b);
            eVar2 = eVar4;
        }
    }

    public static void d(Runnable runnable, Executor executor) {
        try {
            executor.execute(runnable);
        } catch (RuntimeException e2) {
            e.log(Level.SEVERE, "RuntimeException while executing runnable " + runnable + " with executor " + executor, (Throwable) e2);
        }
    }

    public static Object e(Object obj) throws ExecutionException {
        if (obj instanceof c) {
            Throwable th = ((c) obj).b;
            CancellationException cancellationException = new CancellationException("Task was cancelled.");
            cancellationException.initCause(th);
            throw cancellationException;
        }
        if (obj instanceof d) {
            throw new ExecutionException(((d) obj).a);
        }
        if (obj == i) {
            return null;
        }
        return obj;
    }

    public static Object f(h1 h1Var) {
        V v;
        boolean z = false;
        while (true) {
            try {
                v = h1Var.get();
                break;
            } catch (InterruptedException unused) {
                z = true;
            } catch (Throwable th) {
                if (z) {
                    Thread.currentThread().interrupt();
                }
                throw th;
            }
        }
        if (z) {
            Thread.currentThread().interrupt();
        }
        return v;
    }

    public final void a(StringBuilder sb) {
        try {
            Object objF = f(this);
            sb.append("SUCCESS, result=[");
            sb.append(objF == this ? "this future" : String.valueOf(objF));
            sb.append("]");
        } catch (CancellationException unused) {
            sb.append("CANCELLED");
        } catch (RuntimeException e2) {
            sb.append("UNKNOWN, cause=[");
            sb.append(e2.getClass());
            sb.append(" thrown from get()]");
        } catch (ExecutionException e3) {
            sb.append("FAILURE, cause=[");
            sb.append(e3.getCause());
            sb.append("]");
        }
    }

    @Override // defpackage.zb5
    public final void addListener(Runnable runnable, Executor executor) {
        runnable.getClass();
        executor.getClass();
        e eVar = this.b;
        e eVar2 = e.d;
        if (eVar != eVar2) {
            e eVar3 = new e(runnable, executor);
            do {
                eVar3.c = eVar;
                if (f.a(this, eVar, eVar3)) {
                    return;
                } else {
                    eVar = this.b;
                }
            } while (eVar != eVar2);
        }
        d(runnable, executor);
    }

    public void b() {
    }

    @Override // java.util.concurrent.Future
    public final boolean cancel(boolean z) {
        Object obj = this.a;
        if ((obj == null) | (obj instanceof g)) {
            c cVar = d ? new c(new CancellationException("Future.cancel() was called."), z) : z ? c.c : c.d;
            while (!f.b(this, obj, cVar)) {
                obj = this.a;
                if (!(obj instanceof g)) {
                }
            }
            c(this);
            if (obj instanceof g) {
                throw null;
            }
            return true;
        }
        return false;
    }

    /* JADX WARN: Multi-variable type inference failed */
    public String g() {
        if (this.a instanceof g) {
            return vw2.ab("setFuture=[", "null", "]");
        }
        if (!(this instanceof ScheduledFuture)) {
            return null;
        }
        return "remaining delay=[" + ((ScheduledFuture) this).getDelay(TimeUnit.MILLISECONDS) + " ms]";
    }

    @Override // java.util.concurrent.Future
    public final Object get(long j, TimeUnit timeUnit) throws InterruptedException, TimeoutException {
        boolean z;
        i iVar = i.c;
        long nanos = timeUnit.toNanos(j);
        if (Thread.interrupted()) {
            throw new InterruptedException();
        }
        Object obj = this.a;
        if ((obj != null) && (!(obj instanceof g))) {
            return e(obj);
        }
        long jNanoTime = nanos > 0 ? System.nanoTime() + nanos : 0L;
        if (nanos >= 1000) {
            i iVar2 = this.c;
            if (iVar2 != iVar) {
                i iVar3 = new i();
                z = true;
                do {
                    b bVar = f;
                    bVar.d(iVar3, iVar2);
                    if (bVar.c(this, iVar2, iVar3)) {
                        do {
                            LockSupport.parkNanos(this, nanos);
                            if (Thread.interrupted()) {
                                h(iVar3);
                                throw new InterruptedException();
                            }
                            Object obj2 = this.a;
                            if ((obj2 != null) && (!(obj2 instanceof g))) {
                                return e(obj2);
                            }
                            nanos = jNanoTime - System.nanoTime();
                        } while (nanos >= 1000);
                        h(iVar3);
                    } else {
                        iVar2 = this.c;
                    }
                } while (iVar2 != iVar);
            }
            return e(this.a);
        }
        z = true;
        while (nanos > 0) {
            Object obj3 = this.a;
            if ((obj3 != null ? z : false) && (!(obj3 instanceof g))) {
                return e(obj3);
            }
            if (Thread.interrupted()) {
                throw new InterruptedException();
            }
            nanos = jNanoTime - System.nanoTime();
        }
        String string = toString();
        String string2 = timeUnit.toString();
        Locale locale = Locale.ROOT;
        String lowerCase = string2.toLowerCase(locale);
        StringBuilder sbP = vw2.p(j, "Waited ", " ");
        sbP.append(timeUnit.toString().toLowerCase(locale));
        String string3 = sbP.toString();
        if (nanos + 1000 < 0) {
            String strK = vw2.k(string3, " (plus ");
            long j2 = -nanos;
            long jConvert = timeUnit.convert(j2, TimeUnit.NANOSECONDS);
            long nanos2 = j2 - timeUnit.toNanos(jConvert);
            boolean z2 = (jConvert == 0 || nanos2 > 1000) ? z : false;
            if (jConvert > 0) {
                String strK2 = strK + jConvert + " " + lowerCase;
                if (z2) {
                    strK2 = vw2.k(strK2, ",");
                }
                strK = vw2.k(strK2, " ");
            }
            if (z2) {
                strK = strK + nanos2 + " nanoseconds ";
            }
            string3 = vw2.k(strK, "delay)");
        }
        if (isDone()) {
            throw new TimeoutException(vw2.k(string3, " but future completed as timeout expired"));
        }
        throw new TimeoutException(vw2.l(string3, " for ", string));
    }

    public final void h(i iVar) {
        iVar.a = null;
        while (true) {
            i iVar2 = this.c;
            if (iVar2 == i.c) {
                return;
            }
            i iVar3 = null;
            while (iVar2 != null) {
                i iVar4 = iVar2.b;
                if (iVar2.a != null) {
                    iVar3 = iVar2;
                } else if (iVar3 != null) {
                    iVar3.b = iVar4;
                    if (iVar3.a == null) {
                        break;
                    }
                } else if (!f.c(this, iVar2, iVar4)) {
                    break;
                }
                iVar2 = iVar4;
            }
            return;
        }
    }

    public boolean i(Object obj) {
        if (obj == null) {
            obj = i;
        }
        if (!f.b(this, null, obj)) {
            return false;
        }
        c(this);
        return true;
    }

    @Override // java.util.concurrent.Future
    public final boolean isCancelled() {
        return this.a instanceof c;
    }

    @Override // java.util.concurrent.Future
    public final boolean isDone() {
        return (!(r0 instanceof g)) & (this.a != null);
    }

    public boolean j(Throwable th) {
        th.getClass();
        if (!f.b(this, null, new d(th))) {
            return false;
        }
        c(this);
        return true;
    }

    public final String toString() {
        String strG;
        StringBuilder sb = new StringBuilder();
        sb.append(super.toString());
        sb.append("[status=");
        if (this.a instanceof c) {
            sb.append("CANCELLED");
        } else if (isDone()) {
            a(sb);
        } else {
            try {
                strG = g();
            } catch (RuntimeException e2) {
                strG = "Exception thrown from implementation: " + e2.getClass();
            }
            if (strG != null && !strG.isEmpty()) {
                sb.append("PENDING, info=[");
                sb.append(strG);
                sb.append("]");
            } else if (isDone()) {
                a(sb);
            } else {
                sb.append("PENDING");
            }
        }
        sb.append("]");
        return sb.toString();
    }

    @Override // java.util.concurrent.Future
    public final Object get() throws InterruptedException {
        Object obj;
        i iVar = i.c;
        if (!Thread.interrupted()) {
            Object obj2 = this.a;
            if ((obj2 != null) & (!(obj2 instanceof g))) {
                return e(obj2);
            }
            i iVar2 = this.c;
            if (iVar2 != iVar) {
                i iVar3 = new i();
                do {
                    b bVar = f;
                    bVar.d(iVar3, iVar2);
                    if (bVar.c(this, iVar2, iVar3)) {
                        do {
                            LockSupport.park(this);
                            if (!Thread.interrupted()) {
                                obj = this.a;
                            } else {
                                h(iVar3);
                                throw new InterruptedException();
                            }
                        } while (!((obj != null) & (!(obj instanceof g))));
                        return e(obj);
                    }
                    iVar2 = this.c;
                } while (iVar2 != iVar);
            }
            return e(this.a);
        }
        throw new InterruptedException();
    }
}
