package androidx.concurrent.futures;

import J3.w;
import Z0.o;
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.Future;
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 */
public abstract class i implements w {
    static final a ATOMIC_HELPER;
    private static final Object NULL;
    private static final long SPIN_THRESHOLD_NANOS = 1000;
    volatile d listeners;
    volatile Object value;
    volatile h waiters;
    static final boolean GENERATE_CANCELLATION_CAUSES = Boolean.parseBoolean(System.getProperty("guava.concurrent.generate_cancellation_cause", "false"));
    private static final Logger log = Logger.getLogger(i.class.getName());

    static {
        a gVar;
        try {
            gVar = new e(AtomicReferenceFieldUpdater.newUpdater(h.class, Thread.class, CmcdData.Factory.OBJECT_TYPE_AUDIO_ONLY), AtomicReferenceFieldUpdater.newUpdater(h.class, h.class, "b"), AtomicReferenceFieldUpdater.newUpdater(i.class, h.class, "waiters"), AtomicReferenceFieldUpdater.newUpdater(i.class, d.class, "listeners"), AtomicReferenceFieldUpdater.newUpdater(i.class, Object.class, "value"));
            th = null;
        } catch (Throwable th) {
            th = th;
            gVar = new g();
        }
        ATOMIC_HELPER = gVar;
        if (th != null) {
            log.log(Level.SEVERE, "SafeAtomicHelper is broken!", th);
        }
        NULL = new Object();
    }

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

    public static Object c(Object obj) throws ExecutionException {
        if (obj instanceof b) {
            CancellationException cancellationException = ((b) obj).f11162b;
            CancellationException cancellationException2 = new CancellationException("Task was cancelled.");
            cancellationException2.initCause(cancellationException);
            throw cancellationException2;
        }
        if (obj instanceof c) {
            throw new ExecutionException(((c) obj).f11164a);
        }
        if (obj == NULL) {
            return null;
        }
        return obj;
    }

    public static <T> T checkNotNull(T t6) {
        t6.getClass();
        return t6;
    }

    public static void complete(i iVar) {
        d dVar;
        d dVar2;
        d dVar3 = null;
        while (true) {
            h hVar = iVar.waiters;
            if (ATOMIC_HELPER.c(iVar, hVar, h.f11176c)) {
                while (hVar != null) {
                    Thread thread = hVar.f11177a;
                    if (thread != null) {
                        hVar.f11177a = null;
                        LockSupport.unpark(thread);
                    }
                    hVar = hVar.f11178b;
                }
                iVar.afterDone();
                do {
                    dVar = iVar.listeners;
                } while (!ATOMIC_HELPER.a(iVar, dVar, d.f11165d));
                while (true) {
                    dVar2 = dVar3;
                    dVar3 = dVar;
                    if (dVar3 == null) {
                        break;
                    }
                    dVar = dVar3.f11168c;
                    dVar3.f11168c = dVar2;
                }
                while (dVar2 != null) {
                    dVar3 = dVar2.f11168c;
                    Runnable runnable = dVar2.f11166a;
                    if (runnable instanceof f) {
                        f fVar = (f) runnable;
                        iVar = fVar.f11174a;
                        if (iVar.value == fVar) {
                            if (ATOMIC_HELPER.b(iVar, fVar, getFutureValue(fVar.f11175b))) {
                                break;
                            }
                        } else {
                            continue;
                        }
                    } else {
                        b(runnable, dVar2.f11167b);
                    }
                    dVar2 = dVar3;
                }
                return;
            }
        }
    }

    public static Object getFutureValue(w wVar) {
        if (wVar instanceof i) {
            Object obj = ((i) wVar).value;
            if (!(obj instanceof b)) {
                return obj;
            }
            b bVar = (b) obj;
            return bVar.f11161a ? bVar.f11162b != null ? new b(false, bVar.f11162b) : b.f11160d : obj;
        }
        boolean zIsCancelled = wVar.isCancelled();
        if ((!GENERATE_CANCELLATION_CAUSES) && zIsCancelled) {
            return b.f11160d;
        }
        try {
            Object uninterruptibly = getUninterruptibly(wVar);
            return uninterruptibly == null ? NULL : uninterruptibly;
        } catch (CancellationException e6) {
            if (zIsCancelled) {
                return new b(false, e6);
            }
            return new c(new IllegalArgumentException("get() threw CancellationException, despite reporting isCancelled() == false: " + wVar, e6));
        } catch (ExecutionException e7) {
            return new c(e7.getCause());
        } catch (Throwable th) {
            return new c(th);
        }
    }

    public static <V> V getUninterruptibly(Future<V> future) {
        V v3;
        boolean z6 = false;
        while (true) {
            try {
                v3 = future.get();
                break;
            } catch (InterruptedException unused) {
                z6 = true;
            } catch (Throwable th) {
                if (z6) {
                    Thread.currentThread().interrupt();
                }
                throw th;
            }
        }
        if (z6) {
            Thread.currentThread().interrupt();
        }
        return v3;
    }

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

    @Override // J3.w
    public final void addListener(Runnable runnable, Executor executor) {
        checkNotNull(runnable);
        checkNotNull(executor);
        d dVar = this.listeners;
        d dVar2 = d.f11165d;
        if (dVar != dVar2) {
            d dVar3 = new d(runnable, executor);
            do {
                dVar3.f11168c = dVar;
                if (ATOMIC_HELPER.a(this, dVar, dVar3)) {
                    return;
                } else {
                    dVar = this.listeners;
                }
            } while (dVar != dVar2);
        }
        b(runnable, executor);
    }

    public void afterDone() {
    }

    @Override // java.util.concurrent.Future
    public final boolean cancel(boolean z6) {
        Object obj = this.value;
        if (!(obj == null) && !(obj instanceof f)) {
            return false;
        }
        b bVar = GENERATE_CANCELLATION_CAUSES ? new b(z6, new CancellationException("Future.cancel() was called.")) : z6 ? b.f11159c : b.f11160d;
        i iVar = this;
        boolean z7 = false;
        while (true) {
            if (ATOMIC_HELPER.b(iVar, obj, bVar)) {
                if (z6) {
                    iVar.interruptTask();
                }
                complete(iVar);
                if (!(obj instanceof f)) {
                    break;
                }
                w wVar = ((f) obj).f11175b;
                if (!(wVar instanceof i)) {
                    wVar.cancel(z6);
                    break;
                }
                iVar = (i) wVar;
                obj = iVar.value;
                if (!(obj == null) && !(obj instanceof f)) {
                    break;
                }
                z7 = true;
            } else {
                obj = iVar.value;
                if (!(obj instanceof f)) {
                    return z7;
                }
            }
        }
        return true;
    }

    public final void d(h hVar) {
        hVar.f11177a = null;
        while (true) {
            h hVar2 = this.waiters;
            if (hVar2 == h.f11176c) {
                return;
            }
            h hVar3 = null;
            while (hVar2 != null) {
                h hVar4 = hVar2.f11178b;
                if (hVar2.f11177a != null) {
                    hVar3 = hVar2;
                } else if (hVar3 != null) {
                    hVar3.f11178b = hVar4;
                    if (hVar3.f11177a == null) {
                        break;
                    }
                } else if (!ATOMIC_HELPER.c(this, hVar2, hVar4)) {
                    break;
                }
                hVar2 = hVar4;
            }
            return;
        }
    }

    @Override // java.util.concurrent.Future
    public final Object get(long j4, TimeUnit timeUnit) throws InterruptedException, TimeoutException {
        boolean z6;
        long nanos = timeUnit.toNanos(j4);
        if (Thread.interrupted()) {
            throw new InterruptedException();
        }
        Object obj = this.value;
        if ((obj != null) && (!(obj instanceof f))) {
            return c(obj);
        }
        long jNanoTime = nanos > 0 ? System.nanoTime() + nanos : 0L;
        if (nanos >= 1000) {
            h hVar = this.waiters;
            h hVar2 = h.f11176c;
            if (hVar != hVar2) {
                h hVar3 = new h();
                z6 = true;
                do {
                    a aVar = ATOMIC_HELPER;
                    aVar.d(hVar3, hVar);
                    if (aVar.c(this, hVar, hVar3)) {
                        do {
                            LockSupport.parkNanos(this, nanos);
                            if (Thread.interrupted()) {
                                d(hVar3);
                                throw new InterruptedException();
                            }
                            Object obj2 = this.value;
                            if ((obj2 != null) && (!(obj2 instanceof f))) {
                                return c(obj2);
                            }
                            nanos = jNanoTime - System.nanoTime();
                        } while (nanos >= 1000);
                        d(hVar3);
                    } else {
                        hVar = this.waiters;
                    }
                } while (hVar != hVar2);
            }
            return c(this.value);
        }
        z6 = true;
        while (nanos > 0) {
            Object obj3 = this.value;
            if ((obj3 != null ? z6 : false) && (!(obj3 instanceof f))) {
                return c(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);
        String strN = "Waited " + j4 + " " + timeUnit.toString().toLowerCase(locale);
        if (nanos + 1000 < 0) {
            String strN2 = o.n(strN, " (plus ");
            long j6 = -nanos;
            long jConvert = timeUnit.convert(j6, TimeUnit.NANOSECONDS);
            long nanos2 = j6 - timeUnit.toNanos(jConvert);
            boolean z7 = (jConvert == 0 || nanos2 > 1000) ? z6 : false;
            if (jConvert > 0) {
                String strN3 = strN2 + jConvert + " " + lowerCase;
                if (z7) {
                    strN3 = o.n(strN3, ",");
                }
                strN2 = o.n(strN3, " ");
            }
            if (z7) {
                strN2 = strN2 + nanos2 + " nanoseconds ";
            }
            strN = o.n(strN2, "delay)");
        }
        if (isDone()) {
            throw new TimeoutException(o.n(strN, " but future completed as timeout expired"));
        }
        throw new TimeoutException(o.B(strN, " for ", string));
    }

    public void interruptTask() {
    }

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

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

    public final void maybePropagateCancellationTo(Future<?> future) {
        if ((future != null) && isCancelled()) {
            future.cancel(wasInterrupted());
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    public String pendingToString() {
        Object obj = this.value;
        if (obj instanceof f) {
            StringBuilder sb = new StringBuilder("setFuture=[");
            w wVar = ((f) obj).f11175b;
            return com.google.android.recaptcha.internal.a.n(sb, wVar == this ? "this future" : String.valueOf(wVar), "]");
        }
        if (!(this instanceof ScheduledFuture)) {
            return null;
        }
        return "remaining delay=[" + ((ScheduledFuture) this).getDelay(TimeUnit.MILLISECONDS) + " ms]";
    }

    public boolean set(Object obj) {
        if (obj == null) {
            obj = NULL;
        }
        if (!ATOMIC_HELPER.b(this, null, obj)) {
            return false;
        }
        complete(this);
        return true;
    }

    public boolean setException(Throwable th) {
        if (!ATOMIC_HELPER.b(this, null, new c((Throwable) checkNotNull(th)))) {
            return false;
        }
        complete(this);
        return true;
    }

    /* JADX WARN: Removed duplicated region for block: B:23:0x0048  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public boolean setFuture(w wVar) {
        c cVar;
        checkNotNull(wVar);
        Object obj = this.value;
        if (obj != null) {
            if (obj instanceof b) {
                wVar.cancel(((b) obj).f11161a);
            }
        } else if (wVar.isDone()) {
            if (ATOMIC_HELPER.b(this, null, getFutureValue(wVar))) {
                complete(this);
                return true;
            }
        } else {
            f fVar = new f(this, wVar);
            if (ATOMIC_HELPER.b(this, null, fVar)) {
                try {
                    wVar.addListener(fVar, j.f11179a);
                    return true;
                } catch (Throwable th) {
                    try {
                        cVar = new c(th);
                    } catch (Throwable unused) {
                        cVar = c.f11163b;
                    }
                    ATOMIC_HELPER.b(this, fVar, cVar);
                    return true;
                }
            }
            obj = this.value;
            if (obj instanceof b) {
            }
        }
        return false;
    }

    public String toString() {
        String strPendingToString;
        StringBuilder sb = new StringBuilder();
        sb.append(super.toString());
        sb.append("[status=");
        if (isCancelled()) {
            sb.append("CANCELLED");
        } else if (isDone()) {
            a(sb);
        } else {
            try {
                strPendingToString = pendingToString();
            } catch (RuntimeException e6) {
                strPendingToString = "Exception thrown from implementation: " + e6.getClass();
            }
            if (strPendingToString != null && !strPendingToString.isEmpty()) {
                sb.append("PENDING, info=[");
                sb.append(strPendingToString);
                sb.append("]");
            } else if (isDone()) {
                a(sb);
            } else {
                sb.append("PENDING");
            }
        }
        sb.append("]");
        return sb.toString();
    }

    public final boolean wasInterrupted() {
        Object obj = this.value;
        return (obj instanceof b) && ((b) obj).f11161a;
    }

    @Override // java.util.concurrent.Future
    public final Object get() throws InterruptedException {
        Object obj;
        if (!Thread.interrupted()) {
            Object obj2 = this.value;
            if ((obj2 != null) & (!(obj2 instanceof f))) {
                return c(obj2);
            }
            h hVar = this.waiters;
            h hVar2 = h.f11176c;
            if (hVar != hVar2) {
                h hVar3 = new h();
                do {
                    a aVar = ATOMIC_HELPER;
                    aVar.d(hVar3, hVar);
                    if (aVar.c(this, hVar, hVar3)) {
                        do {
                            LockSupport.park(this);
                            if (!Thread.interrupted()) {
                                obj = this.value;
                            } else {
                                d(hVar3);
                                throw new InterruptedException();
                            }
                        } while (!((obj != null) & (!(obj instanceof f))));
                        return c(obj);
                    }
                    hVar = this.waiters;
                } while (hVar != hVar2);
            }
            return c(this.value);
        }
        throw new InterruptedException();
    }
}
