package androidx.concurrent.futures;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.gms.measurement.internal.u1;
import com.google.common.util.concurrent.w;
import java.util.Locale;
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: classes.dex */
public abstract class g implements w {

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

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static final com.bumptech.glide.h f989c;

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

    @Nullable
    volatile c listeners;

    @Nullable
    volatile Object value;

    @Nullable
    volatile f waiters;

    static {
        com.bumptech.glide.h eVar;
        try {
            eVar = new d(AtomicReferenceFieldUpdater.newUpdater(f.class, Thread.class, "thread"), AtomicReferenceFieldUpdater.newUpdater(f.class, f.class, "next"), AtomicReferenceFieldUpdater.newUpdater(g.class, f.class, "waiters"), AtomicReferenceFieldUpdater.newUpdater(g.class, c.class, "listeners"), AtomicReferenceFieldUpdater.newUpdater(g.class, Object.class, "value"));
            th = null;
        } catch (Throwable th) {
            th = th;
            eVar = new e();
        }
        f989c = eVar;
        if (th != null) {
            f988b.log(Level.SEVERE, "SafeAtomicHelper is broken!", th);
        }
        f990d = new Object();
    }

    public static void c(g gVar) {
        f fVar;
        c cVar;
        c cVar2;
        c cVar3;
        do {
            fVar = gVar.waiters;
        } while (!f989c.g(gVar, fVar, f.f986a));
        while (true) {
            cVar = null;
            if (fVar == null) {
                break;
            }
            Thread thread = fVar.thread;
            if (thread != null) {
                fVar.thread = null;
                LockSupport.unpark(thread);
            }
            fVar = fVar.next;
        }
        gVar.b();
        do {
            cVar2 = gVar.listeners;
        } while (!f989c.c(gVar, cVar2, c.f978c));
        while (true) {
            cVar3 = cVar;
            cVar = cVar2;
            if (cVar == null) {
                break;
            }
            cVar2 = cVar.next;
            cVar.next = cVar3;
        }
        while (cVar3 != null) {
            c cVar4 = cVar3.next;
            d(cVar3.f979a, cVar3.f980b);
            cVar3 = cVar4;
        }
    }

    private static CancellationException cancellationExceptionWithCause(@Nullable String str, @Nullable Throwable th) {
        CancellationException cancellationException = new CancellationException(str);
        cancellationException.initCause(th);
        return cancellationException;
    }

    @NonNull
    public static <T> T checkNotNull(@Nullable T t10) {
        t10.getClass();
        return t10;
    }

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

    public static Object e(Object obj) throws ExecutionException {
        if (obj instanceof a) {
            throw cancellationExceptionWithCause("Task was cancelled.", ((a) obj).cause);
        }
        if (obj instanceof b) {
            throw new ExecutionException(((b) obj).f977a);
        }
        if (obj == f990d) {
            return null;
        }
        return obj;
    }

    public final void a(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=[");
        sb.append(obj == this ? "this future" : String.valueOf(obj));
        sb.append("]");
    }

    public void b() {
    }

    @Override // java.util.concurrent.Future
    public final boolean cancel(boolean z10) {
        Object obj = this.value;
        if (obj == null) {
            if (f989c.e(this, obj, f987a ? new a(z10, new CancellationException("Future.cancel() was called.")) : z10 ? a.f974b : a.f975c)) {
                c(this);
                return true;
            }
        }
        return false;
    }

    public final void f(f fVar) {
        fVar.thread = null;
        while (true) {
            f fVar2 = this.waiters;
            if (fVar2 == f.f986a) {
                return;
            }
            f fVar3 = null;
            while (fVar2 != null) {
                f fVar4 = fVar2.next;
                if (fVar2.thread != null) {
                    fVar3 = fVar2;
                } else if (fVar3 != null) {
                    fVar3.next = fVar4;
                    if (fVar3.thread == null) {
                        break;
                    }
                } else if (!f989c.g(this, fVar2, fVar4)) {
                    break;
                }
                fVar2 = fVar4;
            }
            return;
        }
    }

    @Override // com.google.common.util.concurrent.w
    public final void g(Runnable runnable, Executor executor) {
        checkNotNull(runnable);
        checkNotNull(executor);
        c cVar = this.listeners;
        c cVar2 = c.f978c;
        if (cVar != cVar2) {
            c cVar3 = new c(runnable, executor);
            do {
                cVar3.next = cVar;
                if (f989c.c(this, cVar, cVar3)) {
                    return;
                } else {
                    cVar = this.listeners;
                }
            } while (cVar != cVar2);
        }
        d(runnable, executor);
    }

    /* JADX WARN: Removed duplicated region for block: B:46:0x008a  */
    /* JADX WARN: Removed duplicated region for block: B:59:0x00ac  */
    /* JADX WARN: Unsupported multi-entry loop pattern (BACK_EDGE: B:56:0x009f -> B:36:0x006e). 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.value;
        if ((obj != null) && true) {
            return e(obj);
        }
        long jNanoTime = nanos > 0 ? System.nanoTime() + nanos : 0L;
        if (nanos >= 1000) {
            f fVar = this.waiters;
            f fVar2 = f.f986a;
            if (fVar != fVar2) {
                f fVar3 = new f();
                do {
                    com.bumptech.glide.h hVar = f989c;
                    hVar.y(fVar3, fVar);
                    if (hVar.g(this, fVar, fVar3)) {
                        do {
                            LockSupport.parkNanos(this, nanos);
                            if (Thread.interrupted()) {
                                f(fVar3);
                                throw new InterruptedException();
                            }
                            Object obj2 = this.value;
                            if ((obj2 != null) && true) {
                                return e(obj2);
                            }
                            nanos = jNanoTime - System.nanoTime();
                        } while (nanos >= 1000);
                        f(fVar3);
                        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.value;
                        if ((obj3 != null) && true) {
                            return e(obj3);
                        }
                        if (Thread.interrupted()) {
                            throw new InterruptedException();
                        }
                        nanos = jNanoTime - System.nanoTime();
                        j11 = 0;
                        if (nanos <= j11) {
                        }
                    } else {
                        fVar = this.waiters;
                    }
                } while (fVar != fVar2);
            }
            return e(this.value);
        }
        j11 = 0;
        if (nanos <= j11) {
        }
    }

    public boolean h(Throwable th) {
        if (!f989c.e(this, null, new b((Throwable) checkNotNull(th)))) {
            return false;
        }
        c(this);
        return true;
    }

    public final boolean i() {
        Object obj = this.value;
        return (obj instanceof a) && ((a) obj).f976a;
    }

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

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

    public final void maybePropagateCancellationTo(@Nullable Future<?> future) {
        if ((future != null) && (this.value instanceof a)) {
            future.cancel(i());
        }
    }

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

    public boolean set(@Nullable Object obj) {
        if (obj == null) {
            obj = f990d;
        }
        if (!f989c.e(this, null, obj)) {
            return false;
        }
        c(this);
        return true;
    }

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

    @Override // java.util.concurrent.Future
    public final Object get() throws InterruptedException {
        Object obj;
        if (Thread.interrupted()) {
            throw new InterruptedException();
        }
        Object obj2 = this.value;
        if ((obj2 != null) & true) {
            return e(obj2);
        }
        f fVar = this.waiters;
        f fVar2 = f.f986a;
        if (fVar != fVar2) {
            f fVar3 = new f();
            do {
                com.bumptech.glide.h hVar = f989c;
                hVar.y(fVar3, fVar);
                if (hVar.g(this, fVar, fVar3)) {
                    do {
                        LockSupport.park(this);
                        if (Thread.interrupted()) {
                            f(fVar3);
                            throw new InterruptedException();
                        }
                        obj = this.value;
                    } while (!((obj != null) & true));
                    return e(obj);
                }
                fVar = this.waiters;
            } while (fVar != fVar2);
        }
        return e(this.value);
    }
}
