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

import androidx.media3.exoplayer.upstream.CmcdData;
import java.util.Locale;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
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 x6 extends q9 implements Future {
    public static final Logger Q;
    public static final q9 R;
    public static final Object S;

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

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

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

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public volatile w6 f21335l;

    static {
        boolean z10;
        Throwable th;
        Throwable th2;
        q9 t6Var;
        try {
            z10 = Boolean.parseBoolean(System.getProperty("guava.concurrent.generate_cancellation_cause", "false"));
        } catch (SecurityException unused) {
            z10 = false;
        }
        f21332m = z10;
        Q = Logger.getLogger(x6.class.getName());
        try {
            t6Var = new v6();
            th2 = null;
            th = null;
        } catch (Error | RuntimeException e10) {
            try {
                th = null;
                th2 = e10;
                t6Var = new s6(AtomicReferenceFieldUpdater.newUpdater(w6.class, Thread.class, CmcdData.OBJECT_TYPE_AUDIO_ONLY), AtomicReferenceFieldUpdater.newUpdater(w6.class, w6.class, "b"), AtomicReferenceFieldUpdater.newUpdater(x6.class, w6.class, CmcdData.STREAM_TYPE_LIVE), AtomicReferenceFieldUpdater.newUpdater(x6.class, r6.class, "k"), AtomicReferenceFieldUpdater.newUpdater(x6.class, Object.class, "j"));
            } catch (Error | RuntimeException e11) {
                th = e11;
                th2 = e10;
                t6Var = new t6();
            }
        }
        R = t6Var;
        if (th != null) {
            Logger logger = Q;
            Level level = Level.SEVERE;
            logger.logp(level, "com.google.common.util.concurrent.AbstractFuture", "<clinit>", "UnsafeAtomicHelper is broken!", th2);
            logger.logp(level, "com.google.common.util.concurrent.AbstractFuture", "<clinit>", "SafeAtomicHelper is broken!", th);
        }
        S = new Object();
    }

    public static void J0(x6 x6Var) {
        for (w6 w6VarE = R.E(x6Var); w6VarE != null; w6VarE = w6VarE.f21313b) {
            Thread thread = w6VarE.f21312a;
            if (thread != null) {
                w6VarE.f21312a = null;
                LockSupport.unpark(thread);
            }
        }
        r6 r6VarE = R.e(x6Var);
        r6 r6Var = null;
        while (r6VarE != null) {
            r6 r6Var2 = r6VarE.f21181a;
            r6VarE.f21181a = r6Var;
            r6Var = r6VarE;
            r6VarE = r6Var2;
        }
        if (r6Var != null) {
            throw null;
        }
    }

    public static final Object L0(Object obj) throws ExecutionException {
        if (obj instanceof p6) {
            Throwable th = ((p6) obj).f21128a;
            CancellationException cancellationException = new CancellationException("Task was cancelled.");
            cancellationException.initCause(th);
            throw cancellationException;
        }
        if (obj instanceof q6) {
            ((q6) obj).getClass();
            throw new ExecutionException((Throwable) null);
        }
        if (obj == S) {
            return null;
        }
        return obj;
    }

    public final void I0(StringBuilder sb) {
        Object obj;
        boolean z10 = false;
        while (true) {
            try {
                try {
                    obj = get();
                    break;
                } catch (InterruptedException unused) {
                    z10 = true;
                } catch (Throwable th) {
                    if (z10) {
                        Thread.currentThread().interrupt();
                    }
                    throw th;
                }
            } catch (CancellationException unused2) {
                sb.append("CANCELLED");
                return;
            } catch (RuntimeException e10) {
                sb.append("UNKNOWN, cause=[");
                sb.append(e10.getClass());
                sb.append(" thrown from get()]");
                return;
            } catch (ExecutionException e11) {
                sb.append("FAILURE, cause=[");
                sb.append(e11.getCause());
                sb.append("]");
                return;
            }
        }
        if (z10) {
            Thread.currentThread().interrupt();
        }
        sb.append("SUCCESS, result=[");
        if (obj == null) {
            sb.append("null");
        } else if (obj == this) {
            sb.append("this future");
        } else {
            sb.append(obj.getClass().getName());
            sb.append("@");
            sb.append(Integer.toHexString(System.identityHashCode(obj)));
        }
        sb.append("]");
    }

    public final void K0(w6 w6Var) {
        w6Var.f21312a = null;
        while (true) {
            w6 w6Var2 = this.f21335l;
            if (w6Var2 != w6.f21311c) {
                w6 w6Var3 = null;
                while (w6Var2 != null) {
                    w6 w6Var4 = w6Var2.f21313b;
                    if (w6Var2.f21312a != null) {
                        w6Var3 = w6Var2;
                    } else if (w6Var3 != null) {
                        w6Var3.f21313b = w6Var4;
                        if (w6Var3.f21312a == null) {
                            break;
                        }
                    } else if (!R.p0(this, w6Var2, w6Var4)) {
                        break;
                    }
                    w6Var2 = w6Var4;
                }
                return;
            }
            return;
        }
    }

    @Override // java.util.concurrent.Future
    public final boolean cancel(boolean z10) {
        p6 p6Var;
        Object obj = this.f21333j;
        if (obj == null) {
            if (f21332m) {
                p6Var = new p6(z10, new CancellationException("Future.cancel() was called."));
            } else {
                p6Var = z10 ? p6.f21126b : p6.f21127c;
                p6Var.getClass();
            }
            if (R.l0(this, obj, p6Var)) {
                J0(this);
                return true;
            }
        }
        return false;
    }

    @Override // java.util.concurrent.Future
    public final Object get() throws InterruptedException {
        Object obj;
        if (Thread.interrupted()) {
            throw new InterruptedException();
        }
        Object obj2 = this.f21333j;
        if ((obj2 != null) && true) {
            return L0(obj2);
        }
        w6 w6Var = this.f21335l;
        w6 w6Var2 = w6.f21311c;
        if (w6Var != w6Var2) {
            w6 w6Var3 = new w6();
            do {
                q9 q9Var = R;
                q9Var.W(w6Var3, w6Var);
                if (q9Var.p0(this, w6Var, w6Var3)) {
                    do {
                        LockSupport.park(this);
                        if (Thread.interrupted()) {
                            K0(w6Var3);
                            throw new InterruptedException();
                        }
                        obj = this.f21333j;
                    } while (!((obj != null) & true));
                    return L0(obj);
                }
                w6Var = this.f21335l;
            } while (w6Var != w6Var2);
        }
        Object obj3 = this.f21333j;
        obj3.getClass();
        return L0(obj3);
    }

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

    @Override // java.util.concurrent.Future
    public final boolean isDone() {
        return (this.f21333j != null) & true;
    }

    /* JADX WARN: Multi-variable type inference failed */
    public final String toString() {
        String strConcat;
        String str;
        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 (this.f21333j instanceof p6) {
            sb.append("CANCELLED");
        } else if (isDone()) {
            I0(sb);
        } else {
            int length = sb.length();
            sb.append("PENDING");
            try {
                strConcat = null;
                if (this instanceof ScheduledFuture) {
                    str = "remaining delay=[" + ((ScheduledFuture) this).getDelay(TimeUnit.MILLISECONDS) + " ms]";
                } else {
                    str = null;
                }
                int i10 = i6.f20986a;
                if (str != null) {
                    if (!str.isEmpty()) {
                        strConcat = str;
                    }
                }
            } catch (RuntimeException e10) {
                e = e10;
                Class<?> cls = e.getClass();
                cls.toString();
                strConcat = "Exception thrown from implementation: ".concat(String.valueOf(cls));
            } catch (StackOverflowError e11) {
                e = e11;
                Class<?> cls2 = e.getClass();
                cls2.toString();
                strConcat = "Exception thrown from implementation: ".concat(String.valueOf(cls2));
            }
            if (strConcat != null) {
                sb.append(", info=[");
                sb.append(strConcat);
                sb.append("]");
            }
            if (isDone()) {
                sb.delete(length, sb.length());
                I0(sb);
            }
        }
        sb.append("]");
        return sb.toString();
    }

    /* JADX WARN: Removed duplicated region for block: B:46:0x0096  */
    /* JADX WARN: Removed duplicated region for block: B:59:0x00b8  */
    /* JADX WARN: Unsupported multi-entry loop pattern (BACK_EDGE: B:56:0x00ab -> B:36:0x0077). Please report as a decompilation issue!!! */
    @Override // java.util.concurrent.Future
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final 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.f21333j;
        boolean z10 = true;
        if ((obj != null) & true) {
            return L0(obj);
        }
        long jNanoTime = nanos > 0 ? System.nanoTime() + nanos : 0L;
        if (nanos >= 1000) {
            w6 w6Var = this.f21335l;
            w6 w6Var2 = w6.f21311c;
            if (w6Var != w6Var2) {
                w6 w6Var3 = new w6();
                do {
                    q9 q9Var = R;
                    q9Var.W(w6Var3, w6Var);
                    if (q9Var.p0(this, w6Var, w6Var3)) {
                        do {
                            LockSupport.parkNanos(this, Math.min(nanos, 2147483647999999999L));
                            if (Thread.interrupted()) {
                                K0(w6Var3);
                                throw new InterruptedException();
                            }
                            Object obj2 = this.f21333j;
                            if ((obj2 != null) & true) {
                                return L0(obj2);
                            }
                            nanos = jNanoTime - System.nanoTime();
                        } while (nanos >= 1000);
                        K0(w6Var3);
                        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 strConcat = string3.concat(" (plus ");
                                long j12 = -nanos;
                                long jConvert = timeUnit.convert(j12, TimeUnit.NANOSECONDS);
                                long nanos2 = j12 - timeUnit.toNanos(jConvert);
                                if (jConvert != 0 && nanos2 <= 1000) {
                                    z10 = false;
                                }
                                if (jConvert > 0) {
                                    String strConcat2 = strConcat + jConvert + " " + lowerCase;
                                    if (z10) {
                                        strConcat2 = strConcat2.concat(",");
                                    }
                                    strConcat = strConcat2.concat(" ");
                                }
                                if (z10) {
                                    strConcat = strConcat + nanos2 + " nanoseconds ";
                                }
                                string3 = strConcat.concat("delay)");
                            }
                            if (isDone()) {
                                throw new TimeoutException(string3.concat(" but future completed as timeout expired"));
                            }
                            throw new TimeoutException(defpackage.e.j(string3, " for ", string));
                        }
                        Object obj3 = this.f21333j;
                        if ((obj3 != null) & true) {
                            return L0(obj3);
                        }
                        if (!Thread.interrupted()) {
                            nanos = jNanoTime - System.nanoTime();
                            j11 = 0;
                            if (nanos <= j11) {
                            }
                        } else {
                            throw new InterruptedException();
                        }
                    } else {
                        w6Var = this.f21335l;
                    }
                } while (w6Var != w6Var2);
            }
            Object obj4 = this.f21333j;
            obj4.getClass();
            return L0(obj4);
        }
        j11 = 0;
        if (nanos <= j11) {
        }
    }
}
