package be;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import java.util.concurrent.locks.LockSupport;
import kotlin.jvm.internal.Ref$ObjectRef;
import kotlinx.coroutines.scheduling.CoroutineScheduler$WorkerState;
import org.jetbrains.annotations.Nullable;

/* JADX INFO: loaded from: classes.dex */
public final class a extends Thread {

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public static final /* synthetic */ AtomicIntegerFieldUpdater f4810i = AtomicIntegerFieldUpdater.newUpdater(a.class, "workerCtl$volatile");

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final l f4811a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final Ref$ObjectRef f4812b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public CoroutineScheduler$WorkerState f4813c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public long f4814d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public long f4815e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public int f4816f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public boolean f4817g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final /* synthetic */ b f4818h;
    private volatile int indexInArray;

    @Nullable
    private volatile Object nextParkedWorker;
    private volatile /* synthetic */ int workerCtl$volatile;

    public a(b bVar, int i10) {
        this.f4818h = bVar;
        setDaemon(true);
        setContextClassLoader(bVar.getClass().getClassLoader());
        this.f4811a = new l();
        this.f4812b = new Ref$ObjectRef();
        this.f4813c = CoroutineScheduler$WorkerState.f29537d;
        this.nextParkedWorker = b.f4822k;
        vd.c.f33810a.getClass();
        this.f4816f = vd.c.f33811b.a();
        f(i10);
    }

    /* JADX WARN: Code restructure failed: missing block: B:20:0x0045, code lost:
    
        r13 = be.l.f4849d.get(r3);
        r0 = be.l.f4848c.get(r3);
     */
    /* JADX WARN: Code restructure failed: missing block: B:21:0x0051, code lost:
    
        if (r13 == r0) goto L67;
     */
    /* JADX WARN: Code restructure failed: missing block: B:23:0x0059, code lost:
    
        if (be.l.f4850e.get(r3) != 0) goto L25;
     */
    /* JADX WARN: Code restructure failed: missing block: B:25:0x005c, code lost:
    
        r0 = r0 - 1;
        r1 = r3.c(r0, true);
     */
    /* JADX WARN: Code restructure failed: missing block: B:26:0x0062, code lost:
    
        if (r1 == null) goto L70;
     */
    /* JADX WARN: Code restructure failed: missing block: B:27:0x0064, code lost:
    
        r2 = r1;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final h a(boolean z10) {
        h hVarE;
        h hVarE2;
        long j10;
        CoroutineScheduler$WorkerState coroutineScheduler$WorkerState = this.f4813c;
        CoroutineScheduler$WorkerState coroutineScheduler$WorkerState2 = CoroutineScheduler$WorkerState.f29534a;
        h hVar = null;
        l lVar = this.f4811a;
        b bVar = this.f4818h;
        if (coroutineScheduler$WorkerState != coroutineScheduler$WorkerState2) {
            b bVar2 = this.f4818h;
            AtomicLongFieldUpdater atomicLongFieldUpdater = b.f4820i;
            do {
                j10 = atomicLongFieldUpdater.get(bVar2);
                if (((int) ((9223367638808264704L & j10) >> 42)) == 0) {
                    lVar.getClass();
                    loop1: while (true) {
                        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = l.f4847b;
                        h hVar2 = (h) atomicReferenceFieldUpdater.get(lVar);
                        if (hVar2 == null || hVar2.f4836b.f2073a != 1) {
                            break;
                        }
                        while (!atomicReferenceFieldUpdater.compareAndSet(lVar, hVar2, null)) {
                            if (atomicReferenceFieldUpdater.get(lVar) != hVar2) {
                                break;
                            }
                        }
                        hVar = hVar2;
                        break loop1;
                    }
                    if (hVar != null) {
                        return hVar;
                    }
                    h hVar3 = (h) bVar.f4828f.d();
                    return hVar3 == null ? i(1) : hVar3;
                }
            } while (!b.f4820i.compareAndSet(bVar2, j10, j10 - 4398046511104L));
            this.f4813c = CoroutineScheduler$WorkerState.f29534a;
        }
        if (z10) {
            boolean z11 = d(bVar.f4823a * 2) == 0;
            if (z11 && (hVarE2 = e()) != null) {
                return hVarE2;
            }
            lVar.getClass();
            h hVarB = (h) l.f4847b.getAndSet(lVar, null);
            if (hVarB == null) {
                hVarB = lVar.b();
            }
            if (hVarB != null) {
                return hVarB;
            }
            if (!z11 && (hVarE = e()) != null) {
                return hVarE;
            }
        } else {
            h hVarE3 = e();
            if (hVarE3 != null) {
                return hVarE3;
            }
        }
        return i(3);
    }

    public final int b() {
        return this.indexInArray;
    }

    public final Object c() {
        return this.nextParkedWorker;
    }

    public final int d(int i10) {
        int i11 = this.f4816f;
        int i12 = i11 ^ (i11 << 13);
        int i13 = i12 ^ (i12 >> 17);
        int i14 = i13 ^ (i13 << 5);
        this.f4816f = i14;
        int i15 = i10 - 1;
        return (i15 & i10) == 0 ? i14 & i15 : (i14 & Integer.MAX_VALUE) % i10;
    }

    public final h e() {
        int iD = d(2);
        b bVar = this.f4818h;
        if (iD == 0) {
            h hVar = (h) bVar.f4827e.d();
            return hVar != null ? hVar : (h) bVar.f4828f.d();
        }
        h hVar2 = (h) bVar.f4828f.d();
        return hVar2 != null ? hVar2 : (h) bVar.f4827e.d();
    }

    public final void f(int i10) {
        StringBuilder sb = new StringBuilder();
        sb.append(this.f4818h.f4826d);
        sb.append("-worker-");
        sb.append(i10 == 0 ? "TERMINATED" : String.valueOf(i10));
        setName(sb.toString());
        this.indexInArray = i10;
    }

    public final void g(Object obj) {
        this.nextParkedWorker = obj;
    }

    public final boolean h(CoroutineScheduler$WorkerState coroutineScheduler$WorkerState) {
        CoroutineScheduler$WorkerState coroutineScheduler$WorkerState2 = this.f4813c;
        boolean z10 = coroutineScheduler$WorkerState2 == CoroutineScheduler$WorkerState.f29534a;
        if (z10) {
            b.f4820i.addAndGet(this.f4818h, 4398046511104L);
        }
        if (coroutineScheduler$WorkerState2 != coroutineScheduler$WorkerState) {
            this.f4813c = coroutineScheduler$WorkerState;
        }
        return z10;
    }

    /* JADX WARN: Code restructure failed: missing block: B:35:0x0082, code lost:
    
        r19 = r6;
        r6 = -2;
        r5 = r5;
     */
    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r14v1, types: [T, be.h, java.lang.Object] */
    /* JADX WARN: Type inference failed for: r7v4 */
    /* JADX WARN: Type inference failed for: r7v5, types: [be.h] */
    /* JADX WARN: Type inference failed for: r7v9, types: [be.h] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final h i(int i10) {
        int i11;
        T tC;
        long j10;
        T t10;
        Object obj;
        AtomicLongFieldUpdater atomicLongFieldUpdater = b.f4820i;
        b bVar = this.f4818h;
        int i12 = (int) (atomicLongFieldUpdater.get(bVar) & 2097151);
        Object obj2 = null;
        if (i12 < 2) {
            return null;
        }
        int iD = d(i12);
        int i13 = 0;
        long jMin = Long.MAX_VALUE;
        while (i13 < i12) {
            int i14 = iD + 1;
            if (i14 > i12) {
                i14 = 1;
            }
            a aVar = (a) bVar.f4829g.b(i14);
            if (aVar == null || aVar == this) {
                i11 = i14;
            } else {
                l lVar = aVar.f4811a;
                if (i10 == 3) {
                    tC = lVar.b();
                } else {
                    lVar.getClass();
                    int i15 = l.f4849d.get(lVar);
                    int i16 = l.f4848c.get(lVar);
                    boolean z10 = i10 == 1;
                    while (i15 != i16 && (!z10 || l.f4850e.get(lVar) != 0)) {
                        int i17 = i15 + 1;
                        tC = lVar.c(i15, z10);
                        if (tC != 0) {
                            break;
                        }
                        i15 = i17;
                    }
                    tC = obj2;
                }
                Ref$ObjectRef ref$ObjectRef = this.f4812b;
                Object obj3 = obj2;
                if (tC == 0) {
                    while (true) {
                        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = l.f4847b;
                        ?? r14 = (h) atomicReferenceFieldUpdater.get(lVar);
                        if (r14 == 0) {
                            break;
                        }
                        if (((r14.f4836b.f2073a == 1 ? 1 : 2) & i10) == 0) {
                            break;
                        }
                        j.f4843f.getClass();
                        i11 = i14;
                        long jNanoTime = System.nanoTime() - r14.f4835a;
                        long j11 = j.f4839b;
                        if (jNanoTime < j11) {
                            j10 = j11 - jNanoTime;
                            t10 = 0;
                            break;
                        }
                        do {
                            obj = null;
                            if (atomicReferenceFieldUpdater.compareAndSet(lVar, r14, null)) {
                                ref$ObjectRef.element = r14;
                                break;
                            }
                        } while (atomicReferenceFieldUpdater.get(lVar) == r14);
                        i14 = i11;
                        obj3 = null;
                    }
                } else {
                    ref$ObjectRef.element = tC;
                    i11 = i14;
                    obj = obj2;
                }
                j10 = -1;
                t10 = obj;
                if (j10 == -1) {
                    h hVar = (h) ref$ObjectRef.element;
                    ref$ObjectRef.element = t10;
                    return hVar;
                }
                if (j10 > 0) {
                    jMin = Math.min(jMin, j10);
                }
            }
            i13++;
            iD = i11;
            obj2 = null;
        }
        if (jMin == Long.MAX_VALUE) {
            jMin = 0;
        }
        this.f4815e = jMin;
        return null;
    }

    @Override // java.lang.Thread, java.lang.Runnable
    public final void run() {
        AtomicIntegerFieldUpdater atomicIntegerFieldUpdater;
        boolean z10;
        boolean z11 = false;
        loop0: while (true) {
            boolean z12 = z11;
            while (true) {
                b bVar = this.f4818h;
                bVar.getClass();
                if (b.f4821j.get(bVar) == 0) {
                    CoroutineScheduler$WorkerState coroutineScheduler$WorkerState = this.f4813c;
                    CoroutineScheduler$WorkerState coroutineScheduler$WorkerState2 = CoroutineScheduler$WorkerState.f29538e;
                    if (coroutineScheduler$WorkerState == coroutineScheduler$WorkerState2) {
                        break loop0;
                    }
                    h hVarA = a(this.f4817g);
                    long j10 = -2097152;
                    if (hVarA != null) {
                        this.f4815e = 0L;
                        int i10 = hVarA.f4836b.f2073a;
                        this.f4814d = 0L;
                        if (this.f4813c == CoroutineScheduler$WorkerState.f29536c) {
                            this.f4813c = CoroutineScheduler$WorkerState.f29535b;
                        }
                        b bVar2 = this.f4818h;
                        if (i10 != 0 && h(CoroutineScheduler$WorkerState.f29535b) && !bVar2.p() && !bVar2.m(b.f4820i.get(bVar2))) {
                            bVar2.p();
                        }
                        bVar2.getClass();
                        try {
                            hVarA.run();
                        } catch (Throwable th) {
                            Thread threadCurrentThread = Thread.currentThread();
                            threadCurrentThread.getUncaughtExceptionHandler().uncaughtException(threadCurrentThread, th);
                        }
                        if (i10 != 0) {
                            b.f4820i.addAndGet(bVar2, -2097152L);
                            if (this.f4813c != coroutineScheduler$WorkerState2) {
                                this.f4813c = CoroutineScheduler$WorkerState.f29537d;
                            }
                        }
                    } else {
                        this.f4817g = z11;
                        if (this.f4815e == 0) {
                            Object obj = this.nextParkedWorker;
                            la.e eVar = b.f4822k;
                            if (obj != eVar ? true : z11) {
                                f4810i.set(this, -1);
                                while (this.nextParkedWorker != b.f4822k) {
                                    AtomicIntegerFieldUpdater atomicIntegerFieldUpdater2 = f4810i;
                                    if (atomicIntegerFieldUpdater2.get(this) != -1) {
                                        break;
                                    }
                                    b bVar3 = this.f4818h;
                                    bVar3.getClass();
                                    AtomicIntegerFieldUpdater atomicIntegerFieldUpdater3 = b.f4821j;
                                    if (atomicIntegerFieldUpdater3.get(bVar3) != 0) {
                                        break;
                                    }
                                    CoroutineScheduler$WorkerState coroutineScheduler$WorkerState3 = this.f4813c;
                                    CoroutineScheduler$WorkerState coroutineScheduler$WorkerState4 = CoroutineScheduler$WorkerState.f29538e;
                                    if (coroutineScheduler$WorkerState3 == coroutineScheduler$WorkerState4) {
                                        break;
                                    }
                                    h(CoroutineScheduler$WorkerState.f29536c);
                                    Thread.interrupted();
                                    if (this.f4814d == 0) {
                                        atomicIntegerFieldUpdater = atomicIntegerFieldUpdater2;
                                        this.f4814d = System.nanoTime() + this.f4818h.f4825c;
                                    } else {
                                        atomicIntegerFieldUpdater = atomicIntegerFieldUpdater2;
                                    }
                                    LockSupport.parkNanos(this.f4818h.f4825c);
                                    if (System.nanoTime() - this.f4814d >= 0) {
                                        this.f4814d = 0L;
                                        b bVar4 = this.f4818h;
                                        synchronized (bVar4.f4829g) {
                                            try {
                                                if (!(atomicIntegerFieldUpdater3.get(bVar4) != 0)) {
                                                    AtomicLongFieldUpdater atomicLongFieldUpdater = b.f4820i;
                                                    if (((int) (atomicLongFieldUpdater.get(bVar4) & 2097151)) > bVar4.f4823a && atomicIntegerFieldUpdater.compareAndSet(this, -1, 1)) {
                                                        int i11 = this.indexInArray;
                                                        z10 = false;
                                                        f(0);
                                                        bVar4.k(this, i11, 0);
                                                        int andDecrement = (int) (atomicLongFieldUpdater.getAndDecrement(bVar4) & 2097151);
                                                        if (andDecrement != i11) {
                                                            Object objB = bVar4.f4829g.b(andDecrement);
                                                            nb.g.f(objB);
                                                            a aVar = (a) objB;
                                                            bVar4.f4829g.c(i11, aVar);
                                                            aVar.f(i11);
                                                            bVar4.k(aVar, andDecrement, i11);
                                                        }
                                                        bVar4.f4829g.c(andDecrement, null);
                                                        this.f4813c = coroutineScheduler$WorkerState4;
                                                    }
                                                }
                                                z10 = false;
                                            } catch (Throwable th2) {
                                                throw th2;
                                            }
                                        }
                                    } else {
                                        z10 = false;
                                    }
                                    z11 = z10;
                                }
                            } else {
                                b bVar5 = this.f4818h;
                                bVar5.getClass();
                                if (this.nextParkedWorker == eVar) {
                                    while (true) {
                                        AtomicLongFieldUpdater atomicLongFieldUpdater2 = b.f4819h;
                                        long j11 = atomicLongFieldUpdater2.get(bVar5);
                                        int i12 = this.indexInArray;
                                        this.nextParkedWorker = bVar5.f4829g.b((int) (j11 & 2097151));
                                        if (atomicLongFieldUpdater2.compareAndSet(bVar5, j11, ((2097152 + j11) & j10) | ((long) i12))) {
                                            break;
                                        } else {
                                            j10 = -2097152;
                                        }
                                    }
                                }
                            }
                            z11 = z11;
                        } else {
                            if (z12) {
                                h(CoroutineScheduler$WorkerState.f29536c);
                                Thread.interrupted();
                                LockSupport.parkNanos(this.f4815e);
                                this.f4815e = 0L;
                                break;
                            }
                            z12 = true;
                        }
                    }
                } else {
                    break loop0;
                }
            }
        }
        h(CoroutineScheduler$WorkerState.f29538e);
    }
}
