package androidx.work.impl.utils.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 h implements w {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final boolean f4675a = 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 f4676b = Logger.getLogger(h.class.getName());

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

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

    @Nullable
    volatile c listeners;

    @Nullable
    volatile Object value;

    @Nullable
    volatile g waiters;

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

    public static void b(h hVar) {
        c cVar;
        c cVar2;
        c cVar3 = null;
        while (true) {
            g gVar = hVar.waiters;
            if (f4677c.h(hVar, gVar, g.f4674a)) {
                while (gVar != null) {
                    Thread thread = gVar.thread;
                    if (thread != null) {
                        gVar.thread = null;
                        LockSupport.unpark(thread);
                    }
                    gVar = gVar.next;
                }
                do {
                    cVar = hVar.listeners;
                } while (!f4677c.d(hVar, cVar, c.f4664c));
                while (true) {
                    cVar2 = cVar3;
                    cVar3 = cVar;
                    if (cVar3 == null) {
                        break;
                    }
                    cVar = cVar3.next;
                    cVar3.next = cVar2;
                }
                while (cVar2 != null) {
                    cVar3 = cVar2.next;
                    Runnable runnable = cVar2.f4665a;
                    if (runnable instanceof e) {
                        e eVar = (e) runnable;
                        hVar = eVar.f4672a;
                        if (hVar.value == eVar) {
                            if (f4677c.f(hVar, eVar, e(eVar.f4673b))) {
                                break;
                            }
                        } else {
                            continue;
                        }
                    } else {
                        c(runnable, cVar2.f4666b);
                    }
                    cVar2 = cVar3;
                }
                return;
            }
        }
    }

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

    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 Object d(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).f4663a);
        }
        if (obj == f4678d) {
            return null;
        }
        return obj;
    }

    public static Object e(w wVar) {
        if (wVar instanceof h) {
            Object obj = ((h) wVar).value;
            if (!(obj instanceof a)) {
                return obj;
            }
            a aVar = (a) obj;
            return aVar.f4661a ? aVar.cause != null ? new a(false, aVar.cause) : a.f4660c : obj;
        }
        boolean zIsCancelled = wVar.isCancelled();
        if ((!f4675a) && zIsCancelled) {
            return a.f4660c;
        }
        try {
            Object objF = f(wVar);
            return objF == null ? f4678d : objF;
        } catch (CancellationException e10) {
            if (zIsCancelled) {
                return new a(false, e10);
            }
            return new b(new IllegalArgumentException("get() threw CancellationException, despite reporting isCancelled() == false: " + wVar, e10));
        } catch (ExecutionException e11) {
            return new b(e11.getCause());
        } catch (Throwable th) {
            return new b(th);
        }
    }

    public static Object f(Future future) {
        Object obj;
        boolean z10 = false;
        while (true) {
            try {
                obj = future.get();
                break;
            } catch (InterruptedException unused) {
                z10 = true;
            } catch (Throwable th) {
                if (z10) {
                    Thread.currentThread().interrupt();
                }
                throw th;
            }
        }
        if (z10) {
            Thread.currentThread().interrupt();
        }
        return obj;
    }

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

    @Override // java.util.concurrent.Future
    public final boolean cancel(boolean z10) {
        Object obj = this.value;
        if (!(obj == null) && !(obj instanceof e)) {
            return false;
        }
        a aVar = f4675a ? new a(z10, new CancellationException("Future.cancel() was called.")) : z10 ? a.f4659b : a.f4660c;
        h hVar = this;
        boolean z11 = false;
        while (true) {
            if (f4677c.f(hVar, obj, aVar)) {
                b(hVar);
                if (!(obj instanceof e)) {
                    return true;
                }
                w wVar = ((e) obj).f4673b;
                if (!(wVar instanceof h)) {
                    wVar.cancel(z10);
                    return true;
                }
                hVar = (h) wVar;
                obj = hVar.value;
                if (!(obj == null) && !(obj instanceof e)) {
                    return true;
                }
                z11 = true;
            } else {
                obj = hVar.value;
                if (!(obj instanceof e)) {
                    return z11;
                }
            }
        }
    }

    @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.f4664c;
        if (cVar != cVar2) {
            c cVar3 = new c(runnable, executor);
            do {
                cVar3.next = cVar;
                if (f4677c.d(this, cVar, cVar3)) {
                    return;
                } else {
                    cVar = this.listeners;
                }
            } while (cVar != cVar2);
        }
        c(runnable, executor);
    }

    /* JADX WARN: Removed duplicated region for block: B:46:0x0090  */
    /* JADX WARN: Removed duplicated region for block: B:59:0x00b5  */
    /* JADX WARN: Unsupported multi-entry loop pattern (BACK_EDGE: B:56:0x00a8 -> B:36:0x0074). 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) && (!(obj instanceof e))) {
            return d(obj);
        }
        long jNanoTime = nanos > 0 ? System.nanoTime() + nanos : 0L;
        if (nanos >= 1000) {
            g gVar = this.waiters;
            g gVar2 = g.f4674a;
            if (gVar != gVar2) {
                g gVar3 = new g();
                do {
                    com.bumptech.glide.h hVar = f4677c;
                    hVar.z(gVar3, gVar);
                    if (hVar.h(this, gVar, gVar3)) {
                        do {
                            LockSupport.parkNanos(this, nanos);
                            if (Thread.interrupted()) {
                                h(gVar3);
                                throw new InterruptedException();
                            }
                            Object obj2 = this.value;
                            if ((obj2 != null) && (!(obj2 instanceof e))) {
                                return d(obj2);
                            }
                            nanos = jNanoTime - System.nanoTime();
                        } while (nanos >= 1000);
                        h(gVar3);
                        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) && (!(obj3 instanceof e))) {
                            return d(obj3);
                        }
                        if (Thread.interrupted()) {
                            throw new InterruptedException();
                        }
                        nanos = jNanoTime - System.nanoTime();
                        j11 = 0;
                        if (nanos <= j11) {
                        }
                    } else {
                        gVar = this.waiters;
                    }
                } while (gVar != gVar2);
            }
            return d(this.value);
        }
        j11 = 0;
        if (nanos <= j11) {
        }
    }

    public final void h(g gVar) {
        gVar.thread = null;
        while (true) {
            g gVar2 = this.waiters;
            if (gVar2 == g.f4674a) {
                return;
            }
            g gVar3 = null;
            while (gVar2 != null) {
                g gVar4 = gVar2.next;
                if (gVar2.thread != null) {
                    gVar3 = gVar2;
                } else if (gVar3 != null) {
                    gVar3.next = gVar4;
                    if (gVar3.thread == null) {
                        break;
                    }
                } else if (!f4677c.h(this, gVar2, gVar4)) {
                    break;
                }
                gVar2 = gVar4;
            }
            return;
        }
    }

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

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

    public final void maybePropagateCancellationTo(@Nullable Future<?> future) {
        boolean z10 = false;
        if ((future != null) && (this.value instanceof a)) {
            Object obj = this.value;
            if ((obj instanceof a) && ((a) obj).f4661a) {
                z10 = true;
            }
            future.cancel(z10);
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    @Nullable
    public String pendingToString() {
        Object obj = this.value;
        if (obj instanceof e) {
            StringBuilder sb = new StringBuilder("setFuture=[");
            w wVar = ((e) obj).f4673b;
            return defpackage.e.o(sb, wVar == this ? "this future" : String.valueOf(wVar), "]");
        }
        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 = f4678d;
        }
        if (!f4677c.f(this, null, obj)) {
            return false;
        }
        b(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()) {
            Object obj2 = this.value;
            if ((obj2 != null) & (!(obj2 instanceof e))) {
                return d(obj2);
            }
            g gVar = this.waiters;
            g gVar2 = g.f4674a;
            if (gVar != gVar2) {
                g gVar3 = new g();
                do {
                    com.bumptech.glide.h hVar = f4677c;
                    hVar.z(gVar3, gVar);
                    if (hVar.h(this, gVar, gVar3)) {
                        do {
                            LockSupport.park(this);
                            if (Thread.interrupted()) {
                                h(gVar3);
                                throw new InterruptedException();
                            }
                            obj = this.value;
                        } while (!((obj != null) & (!(obj instanceof e))));
                        return d(obj);
                    }
                    gVar = this.waiters;
                } while (gVar != gVar2);
            }
            return d(this.value);
        }
        throw new InterruptedException();
    }
}
