package t;

import L1.Q0;
import com.google.android.gms.internal.measurement.J1;
import java.util.Locale;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
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;
import s2.InterfaceFutureC1014b;
import s2.RunnableC1013a;

/* JADX INFO: loaded from: classes.dex */
public abstract class h implements InterfaceFutureC1014b {

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public static final boolean f9963n = Boolean.parseBoolean(System.getProperty("guava.concurrent.generate_cancellation_cause", "false"));

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public static final Logger f9964o = Logger.getLogger(h.class.getName());
    public static final J1 p;
    public static final Object q;

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

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

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public volatile g f9967m;

    static {
        J1 fVar;
        try {
            fVar = new e(AtomicReferenceFieldUpdater.newUpdater(g.class, Thread.class, "a"), AtomicReferenceFieldUpdater.newUpdater(g.class, g.class, "b"), AtomicReferenceFieldUpdater.newUpdater(h.class, g.class, "m"), AtomicReferenceFieldUpdater.newUpdater(h.class, C1020d.class, "l"), AtomicReferenceFieldUpdater.newUpdater(h.class, Object.class, "k"));
            th = null;
        } catch (Throwable th) {
            th = th;
            fVar = new f();
        }
        p = fVar;
        if (th != null) {
            f9964o.log(Level.SEVERE, "SafeAtomicHelper is broken!", th);
        }
        q = new Object();
    }

    public static void d(h hVar) {
        g gVar;
        C1020d c1020d;
        C1020d c1020d2;
        C1020d c1020d3;
        do {
            gVar = hVar.f9967m;
        } while (!p.f(hVar, gVar, g.f9960c));
        while (true) {
            c1020d = null;
            if (gVar == null) {
                break;
            }
            Thread thread = gVar.f9961a;
            if (thread != null) {
                gVar.f9961a = null;
                LockSupport.unpark(thread);
            }
            gVar = gVar.f9962b;
        }
        hVar.c();
        do {
            c1020d2 = hVar.f9966l;
        } while (!p.d(hVar, c1020d2, C1020d.f9951d));
        while (true) {
            c1020d3 = c1020d;
            c1020d = c1020d2;
            if (c1020d == null) {
                break;
            }
            c1020d2 = c1020d.f9954c;
            c1020d.f9954c = c1020d3;
        }
        while (c1020d3 != null) {
            C1020d c1020d4 = c1020d3.f9954c;
            e(c1020d3.f9952a, c1020d3.f9953b);
            c1020d3 = c1020d4;
        }
    }

    public static void e(RunnableC1013a runnableC1013a, Q0 q02) {
        try {
            q02.execute(runnableC1013a);
        } catch (RuntimeException e2) {
            f9964o.log(Level.SEVERE, "RuntimeException while executing runnable " + runnableC1013a + " with executor " + q02, (Throwable) e2);
        }
    }

    public static Object f(Object obj) throws ExecutionException {
        if (obj instanceof C1017a) {
            CancellationException cancellationException = ((C1017a) obj).f9948b;
            CancellationException cancellationException2 = new CancellationException("Task was cancelled.");
            cancellationException2.initCause(cancellationException);
            throw cancellationException2;
        }
        if (obj instanceof C1019c) {
            throw new ExecutionException(((C1019c) obj).f9950a);
        }
        if (obj == q) {
            return null;
        }
        return obj;
    }

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

    @Override // s2.InterfaceFutureC1014b
    public final void a(RunnableC1013a runnableC1013a, Q0 q02) {
        C1020d c1020d = this.f9966l;
        C1020d c1020d2 = C1020d.f9951d;
        if (c1020d != c1020d2) {
            C1020d c1020d3 = new C1020d(runnableC1013a, q02);
            do {
                c1020d3.f9954c = c1020d;
                if (p.d(this, c1020d, c1020d3)) {
                    return;
                } else {
                    c1020d = this.f9966l;
                }
            } while (c1020d != c1020d2);
        }
        e(runnableC1013a, q02);
    }

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

    @Override // java.util.concurrent.Future
    public final boolean cancel(boolean z5) {
        Object obj = this.f9965k;
        if (obj != null) {
            return false;
        }
        if (!p.e(this, obj, f9963n ? new C1017a(z5, new CancellationException("Future.cancel() was called.")) : z5 ? C1017a.f9945c : C1017a.f9946d)) {
            return false;
        }
        d(this);
        return true;
    }

    @Override // java.util.concurrent.Future
    public final Object get(long j5, TimeUnit timeUnit) throws InterruptedException, TimeoutException {
        long nanos = timeUnit.toNanos(j5);
        if (Thread.interrupted()) {
            throw new InterruptedException();
        }
        Object obj = this.f9965k;
        if (obj != null) {
            return f(obj);
        }
        long jNanoTime = nanos > 0 ? System.nanoTime() + nanos : 0L;
        if (nanos >= 1000) {
            g gVar = this.f9967m;
            g gVar2 = g.f9960c;
            if (gVar != gVar2) {
                g gVar3 = new g();
                do {
                    J1 j12 = p;
                    j12.q(gVar3, gVar);
                    if (j12.f(this, gVar, gVar3)) {
                        do {
                            LockSupport.parkNanos(this, nanos);
                            if (Thread.interrupted()) {
                                i(gVar3);
                                throw new InterruptedException();
                            }
                            Object obj2 = this.f9965k;
                            if (obj2 != null) {
                                return f(obj2);
                            }
                            nanos = jNanoTime - System.nanoTime();
                        } while (nanos >= 1000);
                        i(gVar3);
                    } else {
                        gVar = this.f9967m;
                    }
                } while (gVar != gVar2);
            }
            return f(this.f9965k);
        }
        while (nanos > 0) {
            Object obj3 = this.f9965k;
            if (obj3 != null) {
                return f(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 strC = "Waited " + j5 + " " + timeUnit.toString().toLowerCase(locale);
        if (nanos + 1000 < 0) {
            String strC2 = s0.c.c(strC, " (plus ");
            long j6 = -nanos;
            long jConvert = timeUnit.convert(j6, TimeUnit.NANOSECONDS);
            long nanos2 = j6 - timeUnit.toNanos(jConvert);
            boolean z5 = jConvert == 0 || nanos2 > 1000;
            if (jConvert > 0) {
                String strC3 = strC2 + jConvert + " " + lowerCase;
                if (z5) {
                    strC3 = s0.c.c(strC3, ",");
                }
                strC2 = s0.c.c(strC3, " ");
            }
            if (z5) {
                strC2 = strC2 + nanos2 + " nanoseconds ";
            }
            strC = s0.c.c(strC2, "delay)");
        }
        if (isDone()) {
            throw new TimeoutException(s0.c.c(strC, " but future completed as timeout expired"));
        }
        throw new TimeoutException(strC + " for " + string);
    }

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

    public final void i(g gVar) {
        gVar.f9961a = null;
        while (true) {
            g gVar2 = this.f9967m;
            if (gVar2 == g.f9960c) {
                return;
            }
            g gVar3 = null;
            while (gVar2 != null) {
                g gVar4 = gVar2.f9962b;
                if (gVar2.f9961a != null) {
                    gVar3 = gVar2;
                } else if (gVar3 != null) {
                    gVar3.f9962b = gVar4;
                    if (gVar3.f9961a == null) {
                        break;
                    }
                } else if (!p.f(this, gVar2, gVar4)) {
                    break;
                }
                gVar2 = gVar4;
            }
            return;
        }
    }

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

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

    public boolean j(Object obj) {
        if (obj == null) {
            obj = q;
        }
        if (!p.e(this, null, obj)) {
            return false;
        }
        d(this);
        return true;
    }

    public boolean k(Throwable th) {
        th.getClass();
        if (!p.e(this, null, new C1019c(th))) {
            return false;
        }
        d(this);
        return true;
    }

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

    public void c() {
    }

    @Override // java.util.concurrent.Future
    public final Object get() throws InterruptedException {
        Object obj;
        if (!Thread.interrupted()) {
            Object obj2 = this.f9965k;
            if (obj2 != null) {
                return f(obj2);
            }
            g gVar = this.f9967m;
            g gVar2 = g.f9960c;
            if (gVar != gVar2) {
                g gVar3 = new g();
                do {
                    J1 j12 = p;
                    j12.q(gVar3, gVar);
                    if (j12.f(this, gVar, gVar3)) {
                        do {
                            LockSupport.park(this);
                            if (!Thread.interrupted()) {
                                obj = this.f9965k;
                            } else {
                                i(gVar3);
                                throw new InterruptedException();
                            }
                        } while (obj == null);
                        return f(obj);
                    }
                    gVar = this.f9967m;
                } while (gVar != gVar2);
            }
            return f(this.f9965k);
        }
        throw new InterruptedException();
    }
}
