package com.google.common.util.concurrent;

import androidx.media3.exoplayer.upstream.CmcdData;
import com.google.android.gms.internal.ads.cv1;
import com.google.android.gms.measurement.internal.u1;
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.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: classes2.dex */
public abstract class l extends ca.a implements w {

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

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final c0 f23796f;

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

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

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

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

    static {
        boolean z10;
        c0 fVar;
        try {
            z10 = Boolean.parseBoolean(System.getProperty("guava.concurrent.generate_cancellation_cause", "false"));
        } catch (SecurityException unused) {
            z10 = false;
        }
        f23794d = z10;
        f23795e = new cv1(1, l.class);
        Throwable th = null;
        try {
            fVar = new j();
            e = null;
        } catch (Error | Exception e10) {
            e = e10;
            try {
                fVar = new d(AtomicReferenceFieldUpdater.newUpdater(k.class, Thread.class, CmcdData.OBJECT_TYPE_AUDIO_ONLY), AtomicReferenceFieldUpdater.newUpdater(k.class, k.class, "b"), AtomicReferenceFieldUpdater.newUpdater(l.class, k.class, "c"), AtomicReferenceFieldUpdater.newUpdater(l.class, c.class, "b"), AtomicReferenceFieldUpdater.newUpdater(l.class, Object.class, CmcdData.OBJECT_TYPE_AUDIO_ONLY));
            } catch (Error | Exception e11) {
                th = e11;
                fVar = new f();
            }
        }
        f23796f = fVar;
        if (th != null) {
            cv1 cv1Var = f23795e;
            Logger loggerA = cv1Var.a();
            Level level = Level.SEVERE;
            loggerA.log(level, "UnsafeAtomicHelper is broken!", e);
            cv1Var.a().log(level, "SafeAtomicHelper is broken!", th);
        }
        f23797g = new Object();
    }

    public static void e(l lVar, boolean z10) {
        c cVar = null;
        while (true) {
            lVar.getClass();
            for (k kVarI = f23796f.i(lVar); kVarI != null; kVarI = kVarI.f23793b) {
                Thread thread = kVarI.f23792a;
                if (thread != null) {
                    kVarI.f23792a = null;
                    LockSupport.unpark(thread);
                }
            }
            if (z10) {
                lVar.k();
                z10 = false;
            }
            lVar.c();
            c cVar2 = cVar;
            c cVarH = f23796f.h(lVar);
            c cVar3 = cVar2;
            while (cVarH != null) {
                c cVar4 = cVarH.f23776c;
                cVarH.f23776c = cVar3;
                cVar3 = cVarH;
                cVarH = cVar4;
            }
            while (cVar3 != null) {
                cVar = cVar3.f23776c;
                Runnable runnable = cVar3.f23774a;
                Objects.requireNonNull(runnable);
                if (runnable instanceof e) {
                    e eVar = (e) runnable;
                    lVar = eVar.f23782a;
                    if (lVar.f23798a == eVar) {
                        if (f23796f.c(lVar, eVar, i(eVar.f23783b))) {
                            break;
                        }
                    } else {
                        continue;
                    }
                } else {
                    Executor executor = cVar3.f23775b;
                    Objects.requireNonNull(executor);
                    f(runnable, executor);
                }
                cVar3 = cVar;
            }
            return;
        }
    }

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

    public static Object h(Object obj) throws ExecutionException {
        if (obj instanceof a) {
            Throwable th = ((a) obj).f23768b;
            CancellationException cancellationException = new CancellationException("Task was cancelled.");
            cancellationException.initCause(th);
            throw cancellationException;
        }
        if (obj instanceof b) {
            throw new ExecutionException(((b) obj).f23771a);
        }
        if (obj == f23797g) {
            return null;
        }
        return obj;
    }

    /* JADX WARN: Multi-variable type inference failed */
    public static Object i(w wVar) {
        Throwable thA;
        if (wVar instanceof g) {
            Object aVar = ((l) wVar).f23798a;
            if (aVar instanceof a) {
                a aVar2 = (a) aVar;
                if (aVar2.f23767a) {
                    aVar = aVar2.f23768b != null ? new a(false, aVar2.f23768b) : a.f23766d;
                }
            }
            Objects.requireNonNull(aVar);
            return aVar;
        }
        if ((wVar instanceof ca.a) && (thA = ((ca.a) wVar).a()) != null) {
            return new b(thA);
        }
        boolean zIsCancelled = wVar.isCancelled();
        if ((!f23794d) && zIsCancelled) {
            a aVar3 = a.f23766d;
            Objects.requireNonNull(aVar3);
            return aVar3;
        }
        try {
            Object objJ = j(wVar);
            if (!zIsCancelled) {
                return objJ == null ? f23797g : objJ;
            }
            return new a(false, new IllegalArgumentException("get() did not throw CancellationException, despite reporting isCancelled() == true: " + wVar));
        } catch (Error e10) {
            e = e10;
            return new b(e);
        } catch (CancellationException e11) {
            if (zIsCancelled) {
                return new a(false, e11);
            }
            return new b(new IllegalArgumentException("get() threw CancellationException, despite reporting isCancelled() == false: " + wVar, e11));
        } catch (ExecutionException e12) {
            if (!zIsCancelled) {
                return new b(e12.getCause());
            }
            return new a(false, new IllegalArgumentException("get() did not throw CancellationException, despite reporting isCancelled() == true: " + wVar, e12));
        } catch (Exception e13) {
            e = e13;
            return new b(e);
        }
    }

    public static Object j(Future future) {
        Object obj;
        boolean z10 = false;
        while (true) {
            try {
                obj = future.get();
                break;
            } catch (InterruptedException unused) {
                z10 = true;
            } catch (Throwable th) {
                if (z10) {
                    Thread.currentThread().interrupt();
                }
                throw th;
            }
        }
        if (z10) {
            Thread.currentThread().interrupt();
        }
        return obj;
    }

    @Override // ca.a
    public final Throwable a() {
        if (!(this instanceof g)) {
            return null;
        }
        Object obj = this.f23798a;
        if (obj instanceof b) {
            return ((b) obj).f23771a;
        }
        return null;
    }

    public final void b(StringBuilder sb) {
        try {
            Object objJ = j(this);
            sb.append("SUCCESS, result=[");
            d(sb, objJ);
            sb.append("]");
        } catch (CancellationException unused) {
            sb.append("CANCELLED");
        } catch (ExecutionException e10) {
            sb.append("FAILURE, cause=[");
            sb.append(e10.getCause());
            sb.append("]");
        } catch (Exception e11) {
            sb.append("UNKNOWN, cause=[");
            sb.append(e11.getClass());
            sb.append(" thrown from get()]");
        }
    }

    public void c() {
    }

    public boolean cancel(boolean z10) {
        a aVar;
        Object obj = this.f23798a;
        if (!(obj == null) && !(obj instanceof e)) {
            return false;
        }
        if (f23794d) {
            aVar = new a(z10, new CancellationException("Future.cancel() was called."));
        } else {
            aVar = z10 ? a.f23765c : a.f23766d;
            Objects.requireNonNull(aVar);
        }
        l lVar = this;
        boolean z11 = false;
        while (true) {
            if (f23796f.c(lVar, obj, aVar)) {
                e(lVar, z10);
                if (!(obj instanceof e)) {
                    return true;
                }
                w wVar = ((e) obj).f23783b;
                if (!(wVar instanceof g)) {
                    wVar.cancel(z10);
                    return true;
                }
                lVar = (l) wVar;
                obj = lVar.f23798a;
                if (!(obj == null) && !(obj instanceof e)) {
                    return true;
                }
                z11 = true;
            } else {
                obj = lVar.f23798a;
                if (!(obj instanceof e)) {
                    return z11;
                }
            }
        }
    }

    public final void d(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)));
        }
    }

    public void g(Runnable runnable, Executor executor) {
        c cVar;
        c cVar2;
        if (runnable == null) {
            throw new NullPointerException("Runnable was null.");
        }
        if (executor == null) {
            throw new NullPointerException("Executor was null.");
        }
        if (!isDone() && (cVar = this.f23799b) != (cVar2 = c.f23773d)) {
            c cVar3 = new c(runnable, executor);
            do {
                cVar3.f23776c = cVar;
                if (f23796f.b(this, cVar, cVar3)) {
                    return;
                } else {
                    cVar = this.f23799b;
                }
            } while (cVar != cVar2);
        }
        f(runnable, executor);
    }

    /* JADX WARN: Removed duplicated region for block: B:46:0x009c  */
    /* JADX WARN: Removed duplicated region for block: B:59:0x00c1  */
    /* JADX WARN: Unsupported multi-entry loop pattern (BACK_EDGE: B:56:0x00b4 -> B:36:0x007d). Please report as a decompilation issue!!! */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public Object get(long j10, TimeUnit timeUnit) throws InterruptedException, TimeoutException {
        long j11;
        long nanos = timeUnit.toNanos(j10);
        if (Thread.interrupted()) {
            throw new InterruptedException();
        }
        Object obj = this.f23798a;
        if ((obj != null) && (!(obj instanceof e))) {
            return h(obj);
        }
        long jNanoTime = nanos > 0 ? System.nanoTime() + nanos : 0L;
        if (nanos >= 1000) {
            k kVar = this.f23800c;
            k kVar2 = k.f23791c;
            if (kVar != kVar2) {
                k kVar3 = new k();
                do {
                    c0 c0Var = f23796f;
                    c0Var.l(kVar3, kVar);
                    if (c0Var.d(this, kVar, kVar3)) {
                        do {
                            LockSupport.parkNanos(this, Math.min(nanos, 2147483647999999999L));
                            if (Thread.interrupted()) {
                                m(kVar3);
                                throw new InterruptedException();
                            }
                            Object obj2 = this.f23798a;
                            if ((obj2 != null) && (!(obj2 instanceof e))) {
                                return h(obj2);
                            }
                            nanos = jNanoTime - System.nanoTime();
                        } while (nanos >= 1000);
                        m(kVar3);
                        j11 = 0;
                        if (nanos <= j11) {
                            String string = toString();
                            String string2 = timeUnit.toString();
                            Locale locale = Locale.ROOT;
                            String lowerCase = string2.toLowerCase(locale);
                            StringBuilder sbS = defpackage.e.s("Waited ", j10, " ");
                            sbS.append(timeUnit.toString().toLowerCase(locale));
                            String string3 = sbS.toString();
                            if (nanos + 1000 < 0) {
                                String strJ = u1.j(string3, " (plus ");
                                long j12 = -nanos;
                                long jConvert = timeUnit.convert(j12, TimeUnit.NANOSECONDS);
                                long nanos2 = j12 - timeUnit.toNanos(jConvert);
                                boolean z10 = jConvert == 0 || nanos2 > 1000;
                                if (jConvert > 0) {
                                    String strJ2 = strJ + jConvert + " " + lowerCase;
                                    if (z10) {
                                        strJ2 = u1.j(strJ2, ",");
                                    }
                                    strJ = u1.j(strJ2, " ");
                                }
                                if (z10) {
                                    strJ = strJ + nanos2 + " nanoseconds ";
                                }
                                string3 = u1.j(strJ, "delay)");
                            }
                            if (isDone()) {
                                throw new TimeoutException(u1.j(string3, " but future completed as timeout expired"));
                            }
                            throw new TimeoutException(defpackage.e.j(string3, " for ", string));
                        }
                        Object obj3 = this.f23798a;
                        if ((obj3 != null) && (!(obj3 instanceof e))) {
                            return h(obj3);
                        }
                        if (Thread.interrupted()) {
                            throw new InterruptedException();
                        }
                        nanos = jNanoTime - System.nanoTime();
                        j11 = 0;
                        if (nanos <= j11) {
                        }
                    } else {
                        kVar = this.f23800c;
                    }
                } while (kVar != kVar2);
            }
            Object obj4 = this.f23798a;
            Objects.requireNonNull(obj4);
            return h(obj4);
        }
        j11 = 0;
        if (nanos <= j11) {
        }
    }

    public boolean isCancelled() {
        return this.f23798a instanceof a;
    }

    public boolean isDone() {
        return (!(r0 instanceof e)) & (this.f23798a != null);
    }

    public void k() {
    }

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

    public final void m(k kVar) {
        kVar.f23792a = null;
        while (true) {
            k kVar2 = this.f23800c;
            if (kVar2 == k.f23791c) {
                return;
            }
            k kVar3 = null;
            while (kVar2 != null) {
                k kVar4 = kVar2.f23793b;
                if (kVar2.f23792a != null) {
                    kVar3 = kVar2;
                } else if (kVar3 != null) {
                    kVar3.f23793b = kVar4;
                    if (kVar3.f23792a == null) {
                        break;
                    }
                } else if (!f23796f.d(this, kVar2, kVar4)) {
                    break;
                }
                kVar2 = kVar4;
            }
            return;
        }
    }

    public boolean n(Object obj) {
        if (obj == null) {
            obj = f23797g;
        }
        if (!f23796f.c(this, null, obj)) {
            return false;
        }
        e(this, false);
        return true;
    }

    public boolean o(Throwable th) {
        th.getClass();
        if (!f23796f.c(this, null, new b(th))) {
            return false;
        }
        e(this, false);
        return true;
    }

    /* JADX WARN: Removed duplicated region for block: B:29:0x009f  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final String toString() {
        String strL;
        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()) {
            b(sb);
        } else {
            int length = sb.length();
            sb.append("PENDING");
            Object obj = this.f23798a;
            if (obj instanceof e) {
                sb.append(", setFuture=[");
                w wVar = ((e) obj).f23783b;
                try {
                    if (wVar == this) {
                        sb.append("this future");
                    } else {
                        sb.append(wVar);
                    }
                } catch (Exception e10) {
                    e = e10;
                    sb.append("Exception thrown from implementation: ");
                    sb.append(e.getClass());
                } catch (StackOverflowError e11) {
                    e = e11;
                    sb.append("Exception thrown from implementation: ");
                    sb.append(e.getClass());
                }
                sb.append("]");
            } else {
                try {
                    strL = l();
                } catch (Exception | StackOverflowError e12) {
                    strL = "Exception thrown from implementation: " + e12.getClass();
                }
                if (strL != null) {
                    if (strL.isEmpty()) {
                        strL = null;
                    }
                    if (strL != null) {
                        sb.append(", info=[");
                        sb.append(strL);
                        sb.append("]");
                    }
                }
            }
            if (isDone()) {
                sb.delete(length, sb.length());
                b(sb);
            }
        }
        sb.append("]");
        return sb.toString();
    }

    public Object get() throws InterruptedException {
        Object obj;
        if (!Thread.interrupted()) {
            Object obj2 = this.f23798a;
            if ((obj2 != null) & (!(obj2 instanceof e))) {
                return h(obj2);
            }
            k kVar = this.f23800c;
            k kVar2 = k.f23791c;
            if (kVar != kVar2) {
                k kVar3 = new k();
                do {
                    c0 c0Var = f23796f;
                    c0Var.l(kVar3, kVar);
                    if (c0Var.d(this, kVar, kVar3)) {
                        do {
                            LockSupport.park(this);
                            if (Thread.interrupted()) {
                                m(kVar3);
                                throw new InterruptedException();
                            }
                            obj = this.f23798a;
                        } while (!((obj != null) & (!(obj instanceof e))));
                        return h(obj);
                    }
                    kVar = this.f23800c;
                } while (kVar != kVar2);
            }
            Object obj3 = this.f23798a;
            Objects.requireNonNull(obj3);
            return h(obj3);
        }
        throw new InterruptedException();
    }
}
