package s;

import com.google.android.gms.internal.measurement.X1;
import d3.Q0;
import d5.AbstractC0844a;
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;

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

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

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public static final Logger f16726u = Logger.getLogger(h.class.getName());

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

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

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

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public volatile d f16730r;

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

    static {
        X1 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, "s"), AtomicReferenceFieldUpdater.newUpdater(h.class, d.class, "r"), AtomicReferenceFieldUpdater.newUpdater(h.class, Object.class, "q"));
            th = null;
        } catch (Throwable th) {
            th = th;
            fVar = new f();
        }
        f16727v = fVar;
        if (th != null) {
            f16726u.log(Level.SEVERE, "SafeAtomicHelper is broken!", th);
        }
        f16728w = new Object();
    }

    public static void d(h hVar) {
        g gVar;
        d dVar;
        d dVar2;
        d dVar3;
        do {
            gVar = hVar.f16731s;
        } while (!f16727v.d(hVar, gVar, g.f16722c));
        while (true) {
            dVar = null;
            if (gVar == null) {
                break;
            }
            Thread thread = gVar.f16723a;
            if (thread != null) {
                gVar.f16723a = null;
                LockSupport.unpark(thread);
            }
            gVar = gVar.f16724b;
        }
        hVar.c();
        do {
            dVar2 = hVar.f16730r;
        } while (!f16727v.b(hVar, dVar2, d.f16714d));
        while (true) {
            dVar3 = dVar;
            dVar = dVar2;
            if (dVar == null) {
                break;
            }
            dVar2 = dVar.f16717c;
            dVar.f16717c = dVar3;
        }
        while (dVar3 != null) {
            d dVar4 = dVar3.f16717c;
            e(dVar3.f16715a, dVar3.f16716b);
            dVar3 = dVar4;
        }
    }

    public static void e(G3.b bVar, Q0 q0) {
        try {
            q0.execute(bVar);
        } catch (RuntimeException e8) {
            f16726u.log(Level.SEVERE, "RuntimeException while executing runnable " + bVar + " with executor " + q0, (Throwable) e8);
        }
    }

    public static Object f(Object obj) throws ExecutionException {
        if (obj instanceof C1513a) {
            CancellationException cancellationException = ((C1513a) obj).f16711b;
            CancellationException cancellationException2 = new CancellationException("Task was cancelled.");
            cancellationException2.initCause(cancellationException);
            throw cancellationException2;
        }
        if (obj instanceof c) {
            throw new ExecutionException(((c) obj).f16713a);
        }
        if (obj == f16728w) {
            return null;
        }
        return obj;
    }

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

    @Override // G3.c
    public final void a(G3.b bVar, Q0 q0) {
        d dVar = this.f16730r;
        d dVar2 = d.f16714d;
        if (dVar != dVar2) {
            d dVar3 = new d(bVar, q0);
            do {
                dVar3.f16717c = dVar;
                if (f16727v.b(this, dVar, dVar3)) {
                    return;
                } else {
                    dVar = this.f16730r;
                }
            } while (dVar != dVar2);
        }
        e(bVar, q0);
    }

    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 e8) {
            sb.append("UNKNOWN, cause=[");
            sb.append(e8.getClass());
            sb.append(" thrown from get()]");
        } catch (ExecutionException e9) {
            sb.append("FAILURE, cause=[");
            sb.append(e9.getCause());
            sb.append("]");
        }
    }

    @Override // java.util.concurrent.Future
    public final boolean cancel(boolean z8) {
        Object obj = this.f16729q;
        if (obj != null) {
            return false;
        }
        if (!f16727v.c(this, obj, f16725t ? new C1513a(z8, new CancellationException("Future.cancel() was called.")) : z8 ? C1513a.f16708c : C1513a.f16709d)) {
            return false;
        }
        d(this);
        return true;
    }

    @Override // java.util.concurrent.Future
    public final Object get(long j8, TimeUnit timeUnit) throws InterruptedException, TimeoutException {
        long nanos = timeUnit.toNanos(j8);
        if (Thread.interrupted()) {
            throw new InterruptedException();
        }
        Object obj = this.f16729q;
        if (obj != null) {
            return f(obj);
        }
        long jNanoTime = nanos > 0 ? System.nanoTime() + nanos : 0L;
        if (nanos >= 1000) {
            g gVar = this.f16731s;
            g gVar2 = g.f16722c;
            if (gVar != gVar2) {
                g gVar3 = new g();
                do {
                    X1 x12 = f16727v;
                    x12.E(gVar3, gVar);
                    if (x12.d(this, gVar, gVar3)) {
                        do {
                            LockSupport.parkNanos(this, nanos);
                            if (Thread.interrupted()) {
                                i(gVar3);
                                throw new InterruptedException();
                            }
                            Object obj2 = this.f16729q;
                            if (obj2 != null) {
                                return f(obj2);
                            }
                            nanos = jNanoTime - System.nanoTime();
                        } while (nanos >= 1000);
                        i(gVar3);
                    } else {
                        gVar = this.f16731s;
                    }
                } while (gVar != gVar2);
            }
            return f(this.f16729q);
        }
        while (nanos > 0) {
            Object obj3 = this.f16729q;
            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 strL = "Waited " + j8 + " " + timeUnit.toString().toLowerCase(locale);
        if (nanos + 1000 < 0) {
            String strL2 = AbstractC0844a.l(strL, " (plus ");
            long j9 = -nanos;
            long jConvert = timeUnit.convert(j9, TimeUnit.NANOSECONDS);
            long nanos2 = j9 - timeUnit.toNanos(jConvert);
            boolean z8 = jConvert == 0 || nanos2 > 1000;
            if (jConvert > 0) {
                String strL3 = strL2 + jConvert + " " + lowerCase;
                if (z8) {
                    strL3 = AbstractC0844a.l(strL3, ",");
                }
                strL2 = AbstractC0844a.l(strL3, " ");
            }
            if (z8) {
                strL2 = strL2 + nanos2 + " nanoseconds ";
            }
            strL = AbstractC0844a.l(strL2, "delay)");
        }
        if (isDone()) {
            throw new TimeoutException(AbstractC0844a.l(strL, " but future completed as timeout expired"));
        }
        throw new TimeoutException(strL + " 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.f16723a = null;
        while (true) {
            g gVar2 = this.f16731s;
            if (gVar2 == g.f16722c) {
                return;
            }
            g gVar3 = null;
            while (gVar2 != null) {
                g gVar4 = gVar2.f16724b;
                if (gVar2.f16723a != null) {
                    gVar3 = gVar2;
                } else if (gVar3 != null) {
                    gVar3.f16724b = gVar4;
                    if (gVar3.f16723a == null) {
                        break;
                    }
                } else if (!f16727v.d(this, gVar2, gVar4)) {
                    break;
                }
                gVar2 = gVar4;
            }
            return;
        }
    }

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

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

    public boolean j(Object obj) {
        if (obj == null) {
            obj = f16728w;
        }
        if (!f16727v.c(this, null, obj)) {
            return false;
        }
        d(this);
        return true;
    }

    public boolean k(Throwable th) {
        th.getClass();
        if (!f16727v.c(this, null, new c(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.f16729q instanceof C1513a) {
            sb.append("CANCELLED");
        } else if (isDone()) {
            b(sb);
        } else {
            try {
                strH = h();
            } catch (RuntimeException e8) {
                strH = "Exception thrown from implementation: " + e8.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.f16729q;
            if (obj2 != null) {
                return f(obj2);
            }
            g gVar = this.f16731s;
            g gVar2 = g.f16722c;
            if (gVar != gVar2) {
                g gVar3 = new g();
                do {
                    X1 x12 = f16727v;
                    x12.E(gVar3, gVar);
                    if (x12.d(this, gVar, gVar3)) {
                        do {
                            LockSupport.park(this);
                            if (!Thread.interrupted()) {
                                obj = this.f16729q;
                            } else {
                                i(gVar3);
                                throw new InterruptedException();
                            }
                        } while (obj == null);
                        return f(obj);
                    }
                    gVar = this.f16731s;
                } while (gVar != gVar2);
            }
            return f(this.f16729q);
        }
        throw new InterruptedException();
    }
}
