package g7;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import java.util.concurrent.locks.LockSupport;

/* JADX INFO: renamed from: g7.a, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C0990a extends Thread {

    /* JADX INFO: renamed from: y, reason: collision with root package name */
    public static final /* synthetic */ AtomicIntegerFieldUpdater f12959y = AtomicIntegerFieldUpdater.newUpdater(C0990a.class, "workerCtl$volatile");
    private volatile int indexInArray;
    private volatile Object nextParkedWorker;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public final m f12960q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public final P6.m f12961r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public EnumC0991b f12962s;

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

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

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

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public boolean f12966w;
    private volatile /* synthetic */ int workerCtl$volatile;

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    public final /* synthetic */ ExecutorC0992c f12967x;

    public C0990a(ExecutorC0992c executorC0992c, int i) {
        this.f12967x = executorC0992c;
        setDaemon(true);
        setContextClassLoader(executorC0992c.getClass().getClassLoader());
        this.f12960q = new m();
        this.f12961r = new P6.m();
        this.f12962s = EnumC0991b.f12971t;
        this.nextParkedWorker = ExecutorC0992c.f12974A;
        int iNanoTime = (int) System.nanoTime();
        this.f12965v = iNanoTime == 0 ? 42 : iNanoTime;
        f(i);
    }

    /* JADX WARN: Code restructure failed: missing block: B:20:0x0043, code lost:
    
        r13 = g7.m.f13002d.get(r3);
        r0 = g7.m.f13001c.get(r3);
     */
    /* JADX WARN: Code restructure failed: missing block: B:21:0x004f, code lost:
    
        if (r13 == r0) goto L68;
     */
    /* JADX WARN: Code restructure failed: missing block: B:23:0x0057, code lost:
    
        if (g7.m.f13003e.get(r3) != 0) goto L25;
     */
    /* JADX WARN: Code restructure failed: missing block: B:25:0x005a, code lost:
    
        r0 = r0 - 1;
        r1 = r3.b(r0, true);
     */
    /* JADX WARN: Code restructure failed: missing block: B:26:0x0060, code lost:
    
        if (r1 == null) goto L71;
     */
    /* JADX WARN: Code restructure failed: missing block: B:27:0x0062, code lost:
    
        r2 = r1;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final i a(boolean z8) {
        i iVarE;
        i iVarE2;
        ExecutorC0992c executorC0992c;
        long j8;
        EnumC0991b enumC0991b = this.f12962s;
        EnumC0991b enumC0991b2 = EnumC0991b.f12968q;
        i iVar = null;
        m mVar = this.f12960q;
        ExecutorC0992c executorC0992c2 = this.f12967x;
        if (enumC0991b != enumC0991b2) {
            AtomicLongFieldUpdater atomicLongFieldUpdater = ExecutorC0992c.f12976y;
            do {
                executorC0992c = this.f12967x;
                j8 = atomicLongFieldUpdater.get(executorC0992c);
                if (((int) ((9223367638808264704L & j8) >> 42)) == 0) {
                    mVar.getClass();
                    loop1: while (true) {
                        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = m.f13000b;
                        i iVar2 = (i) atomicReferenceFieldUpdater.get(mVar);
                        if (iVar2 == null || !iVar2.f12991r) {
                            break;
                        }
                        while (!atomicReferenceFieldUpdater.compareAndSet(mVar, iVar2, null)) {
                            if (atomicReferenceFieldUpdater.get(mVar) != iVar2) {
                                break;
                            }
                        }
                        iVar = iVar2;
                        break loop1;
                    }
                    if (iVar != null) {
                        return iVar;
                    }
                    i iVar3 = (i) executorC0992c2.f12983v.d();
                    return iVar3 == null ? i(1) : iVar3;
                }
            } while (!ExecutorC0992c.f12976y.compareAndSet(executorC0992c, j8, j8 - 4398046511104L));
            this.f12962s = enumC0991b2;
        }
        if (z8) {
            boolean z9 = d(executorC0992c2.f12978q * 2) == 0;
            if (z9 && (iVarE2 = e()) != null) {
                return iVarE2;
            }
            mVar.getClass();
            i iVarA = (i) m.f13000b.getAndSet(mVar, null);
            if (iVarA == null) {
                iVarA = mVar.a();
            }
            if (iVarA != null) {
                return iVarA;
            }
            if (!z9 && (iVarE = e()) != null) {
                return iVarE;
            }
        } else {
            i iVarE3 = e();
            if (iVarE3 != null) {
                return iVarE3;
            }
        }
        return i(3);
    }

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

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

    public final int d(int i) {
        int i5 = this.f12965v;
        int i8 = i5 ^ (i5 << 13);
        int i9 = i8 ^ (i8 >> 17);
        int i10 = i9 ^ (i9 << 5);
        this.f12965v = i10;
        int i11 = i - 1;
        return (i11 & i) == 0 ? i10 & i11 : (i10 & Integer.MAX_VALUE) % i;
    }

    public final i e() {
        int iD = d(2);
        ExecutorC0992c executorC0992c = this.f12967x;
        if (iD == 0) {
            i iVar = (i) executorC0992c.f12982u.d();
            return iVar != null ? iVar : (i) executorC0992c.f12983v.d();
        }
        i iVar2 = (i) executorC0992c.f12983v.d();
        return iVar2 != null ? iVar2 : (i) executorC0992c.f12982u.d();
    }

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

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

    public final boolean h(EnumC0991b enumC0991b) {
        EnumC0991b enumC0991b2 = this.f12962s;
        boolean z8 = enumC0991b2 == EnumC0991b.f12968q;
        if (z8) {
            ExecutorC0992c.f12976y.addAndGet(this.f12967x, 4398046511104L);
        }
        if (enumC0991b2 != enumC0991b) {
            this.f12962s = enumC0991b;
        }
        return z8;
    }

    /* JADX WARN: Code restructure failed: missing block: B:25:0x006b, code lost:
    
        r7 = r5;
     */
    /* JADX WARN: Code restructure failed: missing block: B:42:0x00a0, code lost:
    
        r7 = -2;
        r23 = r6;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final i i(int i) {
        int i5;
        long j8;
        i iVarB;
        long j9;
        long j10;
        AtomicLongFieldUpdater atomicLongFieldUpdater = ExecutorC0992c.f12976y;
        ExecutorC0992c executorC0992c = this.f12967x;
        int i8 = (int) (atomicLongFieldUpdater.get(executorC0992c) & 2097151);
        i iVar = null;
        if (i8 < 2) {
            return null;
        }
        int iD = d(i8);
        int i9 = 0;
        long jMin = Long.MAX_VALUE;
        while (i9 < i8) {
            int i10 = iD + 1;
            if (i10 > i8) {
                i10 = 1;
            }
            C0990a c0990a = (C0990a) executorC0992c.f12984w.b(i10);
            if (c0990a == null || c0990a == this) {
                i5 = i10;
            } else {
                m mVar = c0990a.f12960q;
                if (i != 3) {
                    mVar.getClass();
                    int i11 = m.f13002d.get(mVar);
                    int i12 = m.f13001c.get(mVar);
                    boolean z8 = i == 1;
                    while (true) {
                        if (i11 == i12) {
                            j8 = 0;
                            break;
                        }
                        j8 = 0;
                        if (!z8 || m.f13003e.get(mVar) != 0) {
                            int i13 = i11 + 1;
                            iVarB = mVar.b(i11, z8);
                            if (iVarB != null) {
                                break;
                            }
                            i11 = i13;
                        } else {
                            break;
                        }
                    }
                } else {
                    iVarB = mVar.a();
                    j8 = 0;
                }
                P6.m mVar2 = this.f12961r;
                if (iVarB != null) {
                    mVar2.f5535q = iVarB;
                    i5 = i10;
                    j10 = -1;
                    j9 = -1;
                } else {
                    while (true) {
                        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = m.f13000b;
                        i iVar2 = (i) atomicReferenceFieldUpdater.get(mVar);
                        if (iVar2 == null) {
                            j9 = -1;
                            break;
                        }
                        j9 = -1;
                        if (((iVar2.f12991r ? 1 : 2) & i) == 0) {
                            break;
                        }
                        k.f12998f.getClass();
                        i5 = i10;
                        long jNanoTime = System.nanoTime() - iVar2.f12990q;
                        long j11 = k.f12994b;
                        if (jNanoTime < j11) {
                            j10 = j11 - jNanoTime;
                            iVar = null;
                            break;
                        }
                        do {
                            iVar = null;
                            if (atomicReferenceFieldUpdater.compareAndSet(mVar, iVar2, null)) {
                                mVar2.f5535q = iVar2;
                                j10 = -1;
                                break;
                            }
                        } while (atomicReferenceFieldUpdater.get(mVar) == iVar2);
                        i10 = i5;
                        iVar = null;
                    }
                }
                if (j10 == j9) {
                    i iVar3 = (i) mVar2.f5535q;
                    mVar2.f5535q = iVar;
                    return iVar3;
                }
                if (j10 > j8) {
                    jMin = Math.min(jMin, j10);
                }
            }
            i9++;
            iD = i5;
            iVar = null;
        }
        if (jMin == Long.MAX_VALUE) {
            jMin = 0;
        }
        this.f12964u = jMin;
        return null;
    }

    /* JADX WARN: Code restructure failed: missing block: B:128:0x0004, code lost:
    
        continue;
     */
    /* JADX WARN: Code restructure failed: missing block: B:129:0x0004, code lost:
    
        continue;
     */
    /* JADX WARN: Code restructure failed: missing block: B:130:0x0004, code lost:
    
        continue;
     */
    @Override // java.lang.Thread, java.lang.Runnable
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void run() {
        AtomicLongFieldUpdater atomicLongFieldUpdater;
        long j8;
        int i;
        long j9;
        loop0: while (true) {
            boolean z8 = false;
            while (true) {
                ExecutorC0992c executorC0992c = this.f12967x;
                executorC0992c.getClass();
                if (ExecutorC0992c.f12977z.get(executorC0992c) != 1) {
                    EnumC0991b enumC0991b = this.f12962s;
                    EnumC0991b enumC0991b2 = EnumC0991b.f12972u;
                    if (enumC0991b == enumC0991b2) {
                        break loop0;
                    }
                    i iVarA = a(this.f12966w);
                    if (iVarA != null) {
                        this.f12964u = 0L;
                        this.f12963t = 0L;
                        if (this.f12962s == EnumC0991b.f12970s) {
                            this.f12962s = EnumC0991b.f12969r;
                        }
                        boolean z9 = iVarA.f12991r;
                        ExecutorC0992c executorC0992c2 = this.f12967x;
                        if (z9) {
                            if (h(EnumC0991b.f12969r) && !executorC0992c2.m() && !executorC0992c2.j(ExecutorC0992c.f12976y.get(executorC0992c2))) {
                                executorC0992c2.m();
                            }
                            executorC0992c2.getClass();
                            try {
                                iVarA.run();
                            } catch (Throwable th) {
                                Thread threadCurrentThread = Thread.currentThread();
                                threadCurrentThread.getUncaughtExceptionHandler().uncaughtException(threadCurrentThread, th);
                            }
                            ExecutorC0992c.f12976y.addAndGet(executorC0992c2, -2097152L);
                            if (this.f12962s != enumC0991b2) {
                                this.f12962s = EnumC0991b.f12971t;
                            }
                        } else {
                            executorC0992c2.getClass();
                            try {
                                iVarA.run();
                            } catch (Throwable th2) {
                                Thread threadCurrentThread2 = Thread.currentThread();
                                threadCurrentThread2.getUncaughtExceptionHandler().uncaughtException(threadCurrentThread2, th2);
                            }
                        }
                    } else {
                        this.f12966w = false;
                        if (this.f12964u == 0) {
                            Object obj = this.nextParkedWorker;
                            B3.f fVar = ExecutorC0992c.f12974A;
                            long j10 = 2097151;
                            if (obj != fVar) {
                                f12959y.set(this, -1);
                                while (this.nextParkedWorker != ExecutorC0992c.f12974A) {
                                    AtomicIntegerFieldUpdater atomicIntegerFieldUpdater = f12959y;
                                    if (atomicIntegerFieldUpdater.get(this) == -1) {
                                        ExecutorC0992c executorC0992c3 = this.f12967x;
                                        executorC0992c3.getClass();
                                        AtomicIntegerFieldUpdater atomicIntegerFieldUpdater2 = ExecutorC0992c.f12977z;
                                        if (atomicIntegerFieldUpdater2.get(executorC0992c3) == 1) {
                                            break;
                                        }
                                        EnumC0991b enumC0991b3 = this.f12962s;
                                        EnumC0991b enumC0991b4 = EnumC0991b.f12972u;
                                        if (enumC0991b3 == enumC0991b4) {
                                            break;
                                        }
                                        h(EnumC0991b.f12970s);
                                        Thread.interrupted();
                                        if (this.f12963t == 0) {
                                            j9 = j10;
                                            this.f12963t = System.nanoTime() + this.f12967x.f12980s;
                                        } else {
                                            j9 = j10;
                                        }
                                        LockSupport.parkNanos(this.f12967x.f12980s);
                                        if (System.nanoTime() - this.f12963t >= 0) {
                                            this.f12963t = 0L;
                                            ExecutorC0992c executorC0992c4 = this.f12967x;
                                            synchronized (executorC0992c4.f12984w) {
                                                try {
                                                    if (!(atomicIntegerFieldUpdater2.get(executorC0992c4) == 1)) {
                                                        AtomicLongFieldUpdater atomicLongFieldUpdater2 = ExecutorC0992c.f12976y;
                                                        if (((int) (atomicLongFieldUpdater2.get(executorC0992c4) & j9)) > executorC0992c4.f12978q) {
                                                            if (atomicIntegerFieldUpdater.compareAndSet(this, -1, 1)) {
                                                                int i5 = this.indexInArray;
                                                                f(0);
                                                                executorC0992c4.g(this, i5, 0);
                                                                int andDecrement = (int) (atomicLongFieldUpdater2.getAndDecrement(executorC0992c4) & j9);
                                                                if (andDecrement != i5) {
                                                                    Object objB = executorC0992c4.f12984w.b(andDecrement);
                                                                    P6.g.b(objB);
                                                                    C0990a c0990a = (C0990a) objB;
                                                                    executorC0992c4.f12984w.c(i5, c0990a);
                                                                    c0990a.f(i5);
                                                                    executorC0992c4.g(c0990a, andDecrement, i5);
                                                                }
                                                                executorC0992c4.f12984w.c(andDecrement, null);
                                                                this.f12962s = enumC0991b4;
                                                            }
                                                        }
                                                    }
                                                } catch (Throwable th3) {
                                                    throw th3;
                                                }
                                            }
                                        }
                                        j10 = j9;
                                    }
                                }
                            } else {
                                ExecutorC0992c executorC0992c5 = this.f12967x;
                                executorC0992c5.getClass();
                                if (this.nextParkedWorker == fVar) {
                                    do {
                                        atomicLongFieldUpdater = ExecutorC0992c.f12975x;
                                        j8 = atomicLongFieldUpdater.get(executorC0992c5);
                                        i = this.indexInArray;
                                        this.nextParkedWorker = executorC0992c5.f12984w.b((int) (j8 & 2097151));
                                    } while (!atomicLongFieldUpdater.compareAndSet(executorC0992c5, j8, ((2097152 + j8) & (-2097152)) | ((long) i)));
                                }
                            }
                        } else {
                            if (z8) {
                                h(EnumC0991b.f12970s);
                                Thread.interrupted();
                                LockSupport.parkNanos(this.f12964u);
                                this.f12964u = 0L;
                                break;
                            }
                            z8 = true;
                        }
                    }
                } else {
                    break loop0;
                }
            }
        }
        h(EnumC0991b.f12972u);
    }
}
