package o;

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;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* loaded from: classes.dex */
public abstract class M implements YL {

    /* renamed from: o, reason: collision with root package name */
    public static final boolean f221o = Boolean.parseBoolean(System.getProperty("guava.concurrent.generate_cancellation_cause", "false"));
    public static final Logger p = Logger.getLogger(M.class.getName());
    public static final AbstractC2539te q;
    public static final Object r;
    public volatile Object l;
    public volatile H m;
    public volatile L n;

    static {
        AbstractC2539te k;
        try {
            k = new I(AtomicReferenceFieldUpdater.newUpdater(L.class, Thread.class, "a"), AtomicReferenceFieldUpdater.newUpdater(L.class, L.class, "b"), AtomicReferenceFieldUpdater.newUpdater(M.class, L.class, "n"), AtomicReferenceFieldUpdater.newUpdater(M.class, H.class, "m"), AtomicReferenceFieldUpdater.newUpdater(M.class, Object.class, "l"));
            th = null;
        } catch (Throwable th) {
            th = th;
            k = new K();
        }
        q = k;
        if (th != null) {
            p.log(Level.SEVERE, "SafeAtomicHelper is broken!", th);
        }
        r = new Object();
    }

    public static void c(M m) {
        H h;
        H h2;
        H h3 = null;
        while (true) {
            L l = m.n;
            if (q.q(m, l, L.c)) {
                while (l != null) {
                    Thread thread = l.a;
                    if (thread != null) {
                        l.a = null;
                        LockSupport.unpark(thread);
                    }
                    l = l.b;
                }
                do {
                    h = m.m;
                } while (!q.o(m, h, H.d));
                while (true) {
                    h2 = h3;
                    h3 = h;
                    if (h3 == null) {
                        break;
                    }
                    h = h3.c;
                    h3.c = h2;
                }
                while (h2 != null) {
                    h3 = h2.c;
                    Runnable runnable = h2.a;
                    if (runnable instanceof J) {
                        J j = (J) runnable;
                        m = j.l;
                        if (m.l == j) {
                            if (q.p(m, j, f(j.m))) {
                                break;
                            }
                        } else {
                            continue;
                        }
                    } else {
                        d(runnable, h2.b);
                    }
                    h2 = h3;
                }
                return;
            }
        }
    }

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

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

    public static Object f(YL yl) {
        Object obj;
        if (yl instanceof M) {
            Object obj2 = ((M) yl).l;
            if (!(obj2 instanceof E)) {
                return obj2;
            }
            E e = (E) obj2;
            return e.a ? e.b != null ? new E(e.b, false) : E.d : obj2;
        }
        boolean zIsCancelled = yl.isCancelled();
        boolean z = true;
        if ((!f221o) && zIsCancelled) {
            return E.d;
        }
        boolean z2 = false;
        while (true) {
            try {
                try {
                    obj = yl.get();
                    break;
                } catch (InterruptedException unused) {
                    z2 = z;
                } catch (Throwable th) {
                    if (z2) {
                        Thread.currentThread().interrupt();
                    }
                    throw th;
                }
            } catch (CancellationException e2) {
                if (zIsCancelled) {
                    return new E(e2, false);
                }
                return new G(new IllegalArgumentException("get() threw CancellationException, despite reporting isCancelled() == false: " + yl, e2));
            } catch (ExecutionException e3) {
                return new G(e3.getCause());
            } catch (Throwable th2) {
                return new G(th2);
            }
        }
        if (z2) {
            Thread.currentThread().interrupt();
        }
        return obj == null ? r : obj;
    }

    @Override // o.YL
    public final void a(Runnable runnable, Executor executor) {
        executor.getClass();
        H h = this.m;
        H h2 = H.d;
        if (h != h2) {
            H h3 = new H(runnable, executor);
            do {
                h3.c = h;
                if (q.o(this, h, h3)) {
                    return;
                } else {
                    h = this.m;
                }
            } while (h != h2);
        }
        d(runnable, executor);
    }

    public final void b(StringBuilder sb) {
        Object obj;
        boolean z = false;
        while (true) {
            try {
                try {
                    obj = get();
                    break;
                } catch (InterruptedException unused) {
                    z = true;
                } catch (Throwable th) {
                    if (z) {
                        Thread.currentThread().interrupt();
                    }
                    throw th;
                }
            } catch (CancellationException unused2) {
                sb.append("CANCELLED");
                return;
            } catch (RuntimeException e) {
                sb.append("UNKNOWN, cause=[");
                sb.append(e.getClass());
                sb.append(" thrown from get()]");
                return;
            } catch (ExecutionException e2) {
                sb.append("FAILURE, cause=[");
                sb.append(e2.getCause());
                sb.append("]");
                return;
            }
        }
        if (z) {
            Thread.currentThread().interrupt();
        }
        sb.append("SUCCESS, result=[");
        sb.append(obj == this ? "this future" : String.valueOf(obj));
        sb.append("]");
    }

    /* JADX WARN: Code restructure failed: missing block: B:29:0x0053, code lost:
    
        return true;
     */
    @Override // java.util.concurrent.Future
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public final boolean cancel(boolean r8) {
        /*
            r7 = this;
            java.lang.Object r0 = r7.l
            r1 = 1
            r2 = 0
            if (r0 != 0) goto L8
            r3 = r1
            goto L9
        L8:
            r3 = r2
        L9:
            boolean r4 = r0 instanceof o.J
            r3 = r3 | r4
            if (r3 == 0) goto L5b
            boolean r3 = o.M.f221o
            if (r3 == 0) goto L1f
            o.E r3 = new o.E
            java.util.concurrent.CancellationException r4 = new java.util.concurrent.CancellationException
            java.lang.String r5 = "Future.cancel() was called."
            r4.<init>(r5)
            r3.<init>(r4, r8)
            goto L26
        L1f:
            if (r8 == 0) goto L24
            o.E r3 = o.E.c
            goto L26
        L24:
            o.E r3 = o.E.d
        L26:
            r4 = r7
            r5 = r2
        L28:
            o.te r6 = o.M.q
            boolean r6 = r6.p(r4, r0, r3)
            if (r6 == 0) goto L54
            c(r4)
            boolean r4 = r0 instanceof o.J
            if (r4 == 0) goto L53
            o.J r0 = (o.J) r0
            o.YL r0 = r0.m
            boolean r4 = r0 instanceof o.M
            if (r4 == 0) goto L50
            r4 = r0
            o.M r4 = (o.M) r4
            java.lang.Object r0 = r4.l
            if (r0 != 0) goto L48
            r5 = r1
            goto L49
        L48:
            r5 = r2
        L49:
            boolean r6 = r0 instanceof o.J
            r5 = r5 | r6
            if (r5 == 0) goto L53
            r5 = r1
            goto L28
        L50:
            r0.cancel(r8)
        L53:
            return r1
        L54:
            java.lang.Object r0 = r4.l
            boolean r6 = r0 instanceof o.J
            if (r6 != 0) goto L28
            return r5
        L5b:
            return r2
        */
        throw new UnsupportedOperationException("Method not decompiled: o.M.cancel(boolean):boolean");
    }

    /* JADX WARN: Multi-variable type inference failed */
    public final String g() {
        Object obj = this.l;
        if (obj instanceof J) {
            StringBuilder sb = new StringBuilder("setFuture=[");
            YL yl = ((J) obj).m;
            return AbstractC2462sn.n(sb, yl == this ? "this future" : String.valueOf(yl), "]");
        }
        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;
        L l = L.c;
        long nanos = timeUnit.toNanos(j);
        if (Thread.interrupted()) {
            throw new InterruptedException();
        }
        Object obj = this.l;
        if ((obj != null) && (!(obj instanceof J))) {
            return e(obj);
        }
        long jNanoTime = nanos > 0 ? System.nanoTime() + nanos : 0L;
        if (nanos >= 1000) {
            L l2 = this.n;
            if (l2 != l) {
                L l3 = new L();
                z = true;
                do {
                    AbstractC2539te abstractC2539te = q;
                    abstractC2539te.N(l3, l2);
                    if (abstractC2539te.q(this, l2, l3)) {
                        do {
                            LockSupport.parkNanos(this, nanos);
                            if (Thread.interrupted()) {
                                h(l3);
                                throw new InterruptedException();
                            }
                            Object obj2 = this.l;
                            if ((obj2 != null) && (!(obj2 instanceof J))) {
                                return e(obj2);
                            }
                            nanos = jNanoTime - System.nanoTime();
                        } while (nanos >= 1000);
                        h(l3);
                    } else {
                        l2 = this.n;
                    }
                } while (l2 != l);
            }
            return e(this.l);
        }
        z = true;
        while (nanos > 0) {
            Object obj3 = this.l;
            if ((obj3 != null ? z : false) && (!(obj3 instanceof J))) {
                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);
        String strI = "Waited " + j + " " + timeUnit.toString().toLowerCase(locale);
        if (nanos + 1000 < 0) {
            String strI2 = AbstractC2462sn.i(strI, " (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 strI3 = strI2 + jConvert + " " + lowerCase;
                if (z2) {
                    strI3 = AbstractC2462sn.i(strI3, ",");
                }
                strI2 = AbstractC2462sn.i(strI3, " ");
            }
            if (z2) {
                strI2 = strI2 + nanos2 + " nanoseconds ";
            }
            strI = AbstractC2462sn.i(strI2, "delay)");
        }
        if (isDone()) {
            throw new TimeoutException(AbstractC2462sn.i(strI, " but future completed as timeout expired"));
        }
        throw new TimeoutException(strI + " for " + string);
    }

    public final void h(L l) {
        l.a = null;
        while (true) {
            L l2 = this.n;
            if (l2 == L.c) {
                return;
            }
            L l3 = null;
            while (l2 != null) {
                L l4 = l2.b;
                if (l2.a != null) {
                    l3 = l2;
                } else if (l3 != null) {
                    l3.b = l4;
                    if (l3.a == null) {
                        break;
                    }
                } else if (!q.q(this, l2, l4)) {
                    break;
                }
                l2 = l4;
            }
            return;
        }
    }

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

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

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

    @Override // java.util.concurrent.Future
    public final Object get() throws InterruptedException {
        Object obj;
        L l = L.c;
        if (!Thread.interrupted()) {
            Object obj2 = this.l;
            if ((obj2 != null) & (!(obj2 instanceof J))) {
                return e(obj2);
            }
            L l2 = this.n;
            if (l2 != l) {
                L l3 = new L();
                do {
                    AbstractC2539te abstractC2539te = q;
                    abstractC2539te.N(l3, l2);
                    if (abstractC2539te.q(this, l2, l3)) {
                        do {
                            LockSupport.park(this);
                            if (!Thread.interrupted()) {
                                obj = this.l;
                            } else {
                                h(l3);
                                throw new InterruptedException();
                            }
                        } while (!((obj != null) & (!(obj instanceof J))));
                        return e(obj);
                    }
                    l2 = this.n;
                } while (l2 != l);
            }
            return e(this.l);
        }
        throw new InterruptedException();
    }
}
