package J3;

import androidx.media3.exoplayer.upstream.CmcdData;
import java.util.Locale;
import java.util.Objects;
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: classes3.dex */
public abstract class p extends K3.a implements w {

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final boolean f3532d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final v f3533e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final e4.C f3534f;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public static final Object f3535j;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public volatile Object f3536a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public volatile C0438d f3537b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public volatile o f3538c;

    static {
        boolean z6;
        Throwable th;
        e4.C gVar;
        try {
            z6 = Boolean.parseBoolean(System.getProperty("guava.concurrent.generate_cancellation_cause", "false"));
        } catch (SecurityException unused) {
            z6 = false;
        }
        f3532d = z6;
        v vVar = new v();
        vVar.f3543a = p.class.getName();
        f3533e = vVar;
        Throwable th2 = null;
        try {
            gVar = new n();
            th = null;
        } catch (Error | Exception e6) {
            th = e6;
            try {
                gVar = new C0439e(AtomicReferenceFieldUpdater.newUpdater(o.class, Thread.class, CmcdData.Factory.OBJECT_TYPE_AUDIO_ONLY), AtomicReferenceFieldUpdater.newUpdater(o.class, o.class, "b"), AtomicReferenceFieldUpdater.newUpdater(p.class, o.class, "c"), AtomicReferenceFieldUpdater.newUpdater(p.class, C0438d.class, "b"), AtomicReferenceFieldUpdater.newUpdater(p.class, Object.class, CmcdData.Factory.OBJECT_TYPE_AUDIO_ONLY));
            } catch (Error | Exception e7) {
                th2 = e7;
                gVar = new g();
            }
        }
        f3534f = gVar;
        if (th2 != null) {
            v vVar2 = f3533e;
            Logger loggerA = vVar2.a();
            Level level = Level.SEVERE;
            loggerA.log(level, "UnsafeAtomicHelper is broken!", th);
            vVar2.a().log(level, "SafeAtomicHelper is broken!", th2);
        }
        f3535j = new Object();
    }

    public static void d(p pVar, boolean z6) {
        C0438d c0438d = null;
        while (true) {
            pVar.getClass();
            for (o oVarC = f3534f.C(pVar); oVarC != null; oVarC = oVarC.f3531b) {
                Thread thread = oVarC.f3530a;
                if (thread != null) {
                    oVarC.f3530a = null;
                    LockSupport.unpark(thread);
                }
            }
            if (z6) {
                z6 = false;
            }
            pVar.b();
            C0438d c0438d2 = c0438d;
            C0438d c0438dB = f3534f.B(pVar);
            C0438d c0438d3 = c0438d2;
            while (c0438dB != null) {
                C0438d c0438d4 = c0438dB.f3517c;
                c0438dB.f3517c = c0438d3;
                c0438d3 = c0438dB;
                c0438dB = c0438d4;
            }
            while (c0438d3 != null) {
                c0438d = c0438d3.f3517c;
                Runnable runnable = c0438d3.f3515a;
                Objects.requireNonNull(runnable);
                if (runnable instanceof RunnableC0440f) {
                    RunnableC0440f runnableC0440f = (RunnableC0440f) runnable;
                    pVar = runnableC0440f.f3522a;
                    if (pVar.f3536a == runnableC0440f) {
                        if (f3534f.o(pVar, runnableC0440f, g(runnableC0440f.f3523b))) {
                            break;
                        }
                    } else {
                        continue;
                    }
                } else {
                    Executor executor = c0438d3.f3516b;
                    Objects.requireNonNull(executor);
                    e(runnable, executor);
                }
                c0438d3 = c0438d;
            }
            return;
        }
    }

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

    public static Object f(Object obj) throws ExecutionException {
        if (obj instanceof C0435a) {
            RuntimeException runtimeException = ((C0435a) obj).f3510b;
            CancellationException cancellationException = new CancellationException("Task was cancelled.");
            cancellationException.initCause(runtimeException);
            throw cancellationException;
        }
        if (obj instanceof C0437c) {
            throw new ExecutionException(((C0437c) obj).f3513a);
        }
        if (obj == f3535j) {
            return null;
        }
        return obj;
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:21:0x0043  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static Object g(w wVar) {
        if (wVar instanceof h) {
            Object c0435a = ((p) wVar).f3536a;
            if (c0435a instanceof C0435a) {
                C0435a c0435a2 = (C0435a) c0435a;
                if (c0435a2.f3509a) {
                    c0435a = c0435a2.f3510b != null ? new C0435a(false, c0435a2.f3510b) : C0435a.f3508d;
                }
            }
            Objects.requireNonNull(c0435a);
            return c0435a;
        }
        if (wVar instanceof K3.a) {
            p pVar = (p) ((K3.a) wVar);
            pVar.getClass();
            if (pVar instanceof h) {
                Object obj = pVar.f3536a;
                Throwable th = obj instanceof C0437c ? ((C0437c) obj).f3513a : null;
                if (th != null) {
                    return new C0437c(th);
                }
            }
        }
        boolean zIsCancelled = wVar.isCancelled();
        if ((!f3532d) && zIsCancelled) {
            C0435a c0435a3 = C0435a.f3508d;
            Objects.requireNonNull(c0435a3);
            return c0435a3;
        }
        try {
            try {
                try {
                    Object objH = h(wVar);
                    if (!zIsCancelled) {
                        return objH == null ? f3535j : objH;
                    }
                    return new C0435a(false, new IllegalArgumentException("get() did not throw CancellationException, despite reporting isCancelled() == true: " + wVar));
                } catch (Error | Exception e6) {
                    e = e6;
                }
            } catch (Error e7) {
                e = e7;
            }
            return new C0437c(e);
        } catch (CancellationException e8) {
            if (zIsCancelled) {
                return new C0435a(false, e8);
            }
            return new C0437c(new IllegalArgumentException("get() threw CancellationException, despite reporting isCancelled() == false: " + wVar, e8));
        } catch (ExecutionException e9) {
            if (!zIsCancelled) {
                return new C0437c(e9.getCause());
            }
            return new C0435a(false, new IllegalArgumentException("get() did not throw CancellationException, despite reporting isCancelled() == true: " + wVar, e9));
        }
    }

    public static Object h(w wVar) {
        Object obj;
        boolean z6 = false;
        while (true) {
            try {
                obj = wVar.get();
                break;
            } catch (InterruptedException unused) {
                z6 = true;
            } catch (Throwable th) {
                if (z6) {
                    Thread.currentThread().interrupt();
                }
                throw th;
            }
        }
        if (z6) {
            Thread.currentThread().interrupt();
        }
        return obj;
    }

    public final void a(StringBuilder sb) {
        try {
            Object objH = h(this);
            sb.append("SUCCESS, result=[");
            c(sb, objH);
            sb.append("]");
        } catch (CancellationException unused) {
            sb.append("CANCELLED");
        } catch (ExecutionException e6) {
            sb.append("FAILURE, cause=[");
            sb.append(e6.getCause());
            sb.append("]");
        } catch (Exception e7) {
            sb.append("UNKNOWN, cause=[");
            sb.append(e7.getClass());
            sb.append(" thrown from get()]");
        }
    }

    @Override // J3.w
    public void addListener(Runnable runnable, Executor executor) {
        C0438d c0438d;
        C0438d c0438d2;
        if (!isDone() && (c0438d = this.f3537b) != (c0438d2 = C0438d.f3514d)) {
            C0438d c0438d3 = new C0438d(runnable, executor);
            do {
                c0438d3.f3517c = c0438d;
                if (f3534f.n(this, c0438d, c0438d3)) {
                    return;
                } else {
                    c0438d = this.f3537b;
                }
            } while (c0438d != c0438d2);
        }
        e(runnable, executor);
    }

    public final void c(StringBuilder sb, Object obj) {
        if (obj == null) {
            sb.append("null");
        } else {
            if (obj == this) {
                sb.append("this future");
                return;
            }
            sb.append(obj.getClass().getName());
            sb.append("@");
            sb.append(Integer.toHexString(System.identityHashCode(obj)));
        }
    }

    @Override // java.util.concurrent.Future
    public boolean cancel(boolean z6) {
        C0435a c0435a;
        Object obj = this.f3536a;
        if (!(obj == null) && !(obj instanceof RunnableC0440f)) {
            return false;
        }
        if (f3532d) {
            c0435a = new C0435a(z6, new CancellationException("Future.cancel() was called."));
        } else {
            c0435a = z6 ? C0435a.f3507c : C0435a.f3508d;
            Objects.requireNonNull(c0435a);
        }
        p pVar = this;
        boolean z7 = false;
        while (true) {
            if (f3534f.o(pVar, obj, c0435a)) {
                d(pVar, z6);
                if (!(obj instanceof RunnableC0440f)) {
                    break;
                }
                w wVar = ((RunnableC0440f) obj).f3523b;
                if (!(wVar instanceof h)) {
                    wVar.cancel(z6);
                    break;
                }
                pVar = (p) wVar;
                obj = pVar.f3536a;
                if (!(obj == null) && !(obj instanceof RunnableC0440f)) {
                    break;
                }
                z7 = true;
            } else {
                obj = pVar.f3536a;
                if (!(obj instanceof RunnableC0440f)) {
                    return z7;
                }
            }
        }
        return true;
    }

    @Override // java.util.concurrent.Future
    public Object get(long j4, TimeUnit timeUnit) throws InterruptedException, TimeoutException {
        boolean z6;
        long j6;
        long nanos = timeUnit.toNanos(j4);
        if (Thread.interrupted()) {
            throw new InterruptedException();
        }
        Object obj = this.f3536a;
        if ((obj != null) && (!(obj instanceof RunnableC0440f))) {
            return f(obj);
        }
        long j7 = 0;
        long jNanoTime = nanos > 0 ? System.nanoTime() + nanos : 0L;
        if (nanos >= 1000) {
            o oVar = this.f3538c;
            o oVar2 = o.f3529c;
            if (oVar != oVar2) {
                o oVar3 = new o();
                z6 = true;
                while (true) {
                    e4.C c6 = f3534f;
                    c6.Z(oVar3, oVar);
                    if (c6.p(this, oVar, oVar3)) {
                        j6 = j7;
                        do {
                            LockSupport.parkNanos(this, Math.min(nanos, 2147483647999999999L));
                            if (Thread.interrupted()) {
                                j(oVar3);
                                throw new InterruptedException();
                            }
                            Object obj2 = this.f3536a;
                            if ((obj2 != null) && (!(obj2 instanceof RunnableC0440f))) {
                                return f(obj2);
                            }
                            nanos = jNanoTime - System.nanoTime();
                        } while (nanos >= 1000);
                        j(oVar3);
                    } else {
                        long j8 = j7;
                        oVar = this.f3538c;
                        if (oVar == oVar2) {
                            break;
                        }
                        j7 = j8;
                    }
                }
            }
            Object obj3 = this.f3536a;
            Objects.requireNonNull(obj3);
            return f(obj3);
        }
        z6 = true;
        j6 = 0;
        while (nanos > j6) {
            Object obj4 = this.f3536a;
            if ((obj4 != null ? z6 : false) && (!(obj4 instanceof RunnableC0440f))) {
                return f(obj4);
            }
            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 < j6) {
            String strN2 = Z0.o.n(strN, " (plus ");
            long j9 = -nanos;
            long jConvert = timeUnit.convert(j9, TimeUnit.NANOSECONDS);
            long nanos2 = j9 - timeUnit.toNanos(jConvert);
            boolean z7 = (jConvert == j6 || nanos2 > 1000) ? z6 : false;
            if (jConvert > j6) {
                String strN3 = strN2 + jConvert + " " + lowerCase;
                if (z7) {
                    strN3 = Z0.o.n(strN3, ",");
                }
                strN2 = Z0.o.n(strN3, " ");
            }
            if (z7) {
                strN2 = strN2 + nanos2 + " nanoseconds ";
            }
            strN = Z0.o.n(strN2, "delay)");
        }
        if (isDone()) {
            throw new TimeoutException(Z0.o.n(strN, " but future completed as timeout expired"));
        }
        throw new TimeoutException(Z0.o.B(strN, " for ", string));
    }

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

    public boolean isCancelled() {
        return this.f3536a instanceof C0435a;
    }

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

    public final void j(o oVar) {
        oVar.f3530a = null;
        while (true) {
            o oVar2 = this.f3538c;
            if (oVar2 == o.f3529c) {
                return;
            }
            o oVar3 = null;
            while (oVar2 != null) {
                o oVar4 = oVar2.f3531b;
                if (oVar2.f3530a != null) {
                    oVar3 = oVar2;
                } else if (oVar3 != null) {
                    oVar3.f3531b = oVar4;
                    if (oVar3.f3530a == null) {
                        break;
                    }
                } else if (!f3534f.p(this, oVar2, oVar4)) {
                    break;
                }
                oVar2 = oVar4;
            }
            return;
        }
    }

    public boolean k(Throwable th) {
        th.getClass();
        if (!f3534f.o(this, null, new C0437c(th))) {
            return false;
        }
        d(this, false);
        return true;
    }

    public final String toString() {
        String strI;
        StringBuilder sb = new StringBuilder();
        if (getClass().getName().startsWith("com.google.common.util.concurrent.")) {
            sb.append(getClass().getSimpleName());
        } else {
            sb.append(getClass().getName());
        }
        sb.append('@');
        sb.append(Integer.toHexString(System.identityHashCode(this)));
        sb.append("[status=");
        if (isCancelled()) {
            sb.append("CANCELLED");
        } else if (isDone()) {
            a(sb);
        } else {
            int length = sb.length();
            sb.append("PENDING");
            Object obj = this.f3536a;
            if (obj instanceof RunnableC0440f) {
                sb.append(", setFuture=[");
                w wVar = ((RunnableC0440f) obj).f3523b;
                try {
                    if (wVar == this) {
                        sb.append("this future");
                    } else {
                        sb.append(wVar);
                    }
                } catch (Exception e6) {
                    e = e6;
                    sb.append("Exception thrown from implementation: ");
                    sb.append(e.getClass());
                } catch (StackOverflowError e7) {
                    e = e7;
                    sb.append("Exception thrown from implementation: ");
                    sb.append(e.getClass());
                }
                sb.append("]");
            } else {
                try {
                    strI = i();
                    if (e4.C.e0(strI)) {
                        strI = null;
                    }
                } catch (Exception | StackOverflowError e8) {
                    strI = "Exception thrown from implementation: " + e8.getClass();
                }
                if (strI != null) {
                    sb.append(", info=[");
                    sb.append(strI);
                    sb.append("]");
                }
            }
            if (isDone()) {
                sb.delete(length, sb.length());
                a(sb);
            }
        }
        sb.append("]");
        return sb.toString();
    }

    public void b() {
    }

    @Override // java.util.concurrent.Future
    public Object get() throws InterruptedException {
        Object obj;
        if (!Thread.interrupted()) {
            Object obj2 = this.f3536a;
            if ((obj2 != null) & (!(obj2 instanceof RunnableC0440f))) {
                return f(obj2);
            }
            o oVar = this.f3538c;
            o oVar2 = o.f3529c;
            if (oVar != oVar2) {
                o oVar3 = new o();
                do {
                    e4.C c6 = f3534f;
                    c6.Z(oVar3, oVar);
                    if (c6.p(this, oVar, oVar3)) {
                        do {
                            LockSupport.park(this);
                            if (!Thread.interrupted()) {
                                obj = this.f3536a;
                            } else {
                                j(oVar3);
                                throw new InterruptedException();
                            }
                        } while (!((obj != null) & (!(obj instanceof RunnableC0440f))));
                        return f(obj);
                    }
                    oVar = this.f3538c;
                } while (oVar != oVar2);
            }
            Object obj3 = this.f3536a;
            Objects.requireNonNull(obj3);
            return f(obj3);
        }
        throw new InterruptedException();
    }
}
