package c5;

import I2.C0317a;
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.C;
import kotlin.jvm.internal.o;

/* JADX INFO: loaded from: classes3.dex */
public final class a extends Thread {
    public static final /* synthetic */ AtomicIntegerFieldUpdater l = AtomicIntegerFieldUpdater.newUpdater(a.class, "workerCtl$volatile");

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final C f13443b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public b f13444c;

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

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public int f13447f;
    private volatile int indexInArray;

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

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public final /* synthetic */ c f13449k;
    private volatile Object nextParkedWorker;
    private volatile /* synthetic */ int workerCtl$volatile;

    public a(c cVar, int i6) {
        this.f13449k = cVar;
        setDaemon(true);
        setContextClassLoader(cVar.getClass().getClassLoader());
        this.f13442a = new m();
        this.f13443b = new C();
        this.f13444c = b.f13453d;
        this.nextParkedWorker = c.f13458n;
        int iNanoTime = (int) System.nanoTime();
        this.f13447f = iNanoTime == 0 ? 42 : iNanoTime;
        f(i6);
    }

    /* JADX WARN: Code restructure failed: missing block: B:20:0x0043, code lost:
    
        r13 = c5.m.f13483d.get(r3);
        r0 = c5.m.f13482c.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 (c5.m.f13484e.get(r3) != 0) goto L25;
     */
    /* JADX WARN: Code restructure failed: missing block: B:25:0x005a, code lost:
    
        r0 = r0 - 1;
        r1 = r3.c(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 z6) {
        i iVarE;
        i iVarE2;
        c cVar;
        long j4;
        b bVar = this.f13444c;
        b bVar2 = b.f13450a;
        i iVar = null;
        m mVar = this.f13442a;
        c cVar2 = this.f13449k;
        if (bVar != bVar2) {
            AtomicLongFieldUpdater atomicLongFieldUpdater = c.l;
            do {
                cVar = this.f13449k;
                j4 = atomicLongFieldUpdater.get(cVar);
                if (((int) ((9223367638808264704L & j4) >> 42)) == 0) {
                    mVar.getClass();
                    loop1: while (true) {
                        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = m.f13481b;
                        i iVar2 = (i) atomicReferenceFieldUpdater.get(mVar);
                        if (iVar2 == null || !iVar2.f13472b) {
                            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) cVar2.f13464f.d();
                    return iVar3 == null ? i(1) : iVar3;
                }
            } while (!c.l.compareAndSet(cVar, j4, j4 - 4398046511104L));
            this.f13444c = b.f13450a;
        }
        if (z6) {
            boolean z7 = d(cVar2.f13459a * 2) == 0;
            if (z7 && (iVarE2 = e()) != null) {
                return iVarE2;
            }
            mVar.getClass();
            i iVarB = (i) m.f13481b.getAndSet(mVar, null);
            if (iVarB == null) {
                iVarB = mVar.b();
            }
            if (iVarB != null) {
                return iVarB;
            }
            if (!z7 && (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 i6) {
        int i7 = this.f13447f;
        int i8 = i7 ^ (i7 << 13);
        int i9 = i8 ^ (i8 >> 17);
        int i10 = i9 ^ (i9 << 5);
        this.f13447f = i10;
        int i11 = i6 - 1;
        return (i11 & i6) == 0 ? i10 & i11 : (i10 & Integer.MAX_VALUE) % i6;
    }

    public final i e() {
        int iD = d(2);
        c cVar = this.f13449k;
        if (iD == 0) {
            i iVar = (i) cVar.f13463e.d();
            return iVar != null ? iVar : (i) cVar.f13464f.d();
        }
        i iVar2 = (i) cVar.f13464f.d();
        return iVar2 != null ? iVar2 : (i) cVar.f13463e.d();
    }

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

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

    public final boolean h(b bVar) {
        b bVar2 = this.f13444c;
        boolean z6 = bVar2 == b.f13450a;
        if (z6) {
            c.l.addAndGet(this.f13449k, 4398046511104L);
        }
        if (bVar2 != bVar) {
            this.f13444c = bVar;
        }
        return z6;
    }

    /* 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 i6) {
        int i7;
        long j4;
        i iVarC;
        long j6;
        long j7;
        AtomicLongFieldUpdater atomicLongFieldUpdater = c.l;
        c cVar = this.f13449k;
        int i8 = (int) (atomicLongFieldUpdater.get(cVar) & 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;
            }
            a aVar = (a) cVar.f13465j.b(i10);
            if (aVar == null || aVar == this) {
                i7 = i10;
            } else {
                m mVar = aVar.f13442a;
                if (i6 != 3) {
                    mVar.getClass();
                    int i11 = m.f13483d.get(mVar);
                    int i12 = m.f13482c.get(mVar);
                    boolean z6 = i6 == 1;
                    while (true) {
                        if (i11 == i12) {
                            j4 = 0;
                            break;
                        }
                        j4 = 0;
                        if (!z6 || m.f13484e.get(mVar) != 0) {
                            int i13 = i11 + 1;
                            iVarC = mVar.c(i11, z6);
                            if (iVarC != null) {
                                break;
                            }
                            i11 = i13;
                        } else {
                            break;
                        }
                    }
                } else {
                    iVarC = mVar.b();
                    j4 = 0;
                }
                C c6 = this.f13443b;
                if (iVarC != null) {
                    c6.f20115a = iVarC;
                    i7 = i10;
                    j7 = -1;
                    j6 = -1;
                } else {
                    while (true) {
                        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = m.f13481b;
                        i iVar2 = (i) atomicReferenceFieldUpdater.get(mVar);
                        if (iVar2 == null) {
                            j6 = -1;
                            break;
                        }
                        j6 = -1;
                        if (((iVar2.f13472b ? 1 : 2) & i6) == 0) {
                            break;
                        }
                        k.f13479f.getClass();
                        i7 = i10;
                        long jNanoTime = System.nanoTime() - iVar2.f13471a;
                        long j8 = k.f13475b;
                        if (jNanoTime < j8) {
                            j7 = j8 - jNanoTime;
                            iVar = null;
                            break;
                        }
                        do {
                            iVar = null;
                            if (atomicReferenceFieldUpdater.compareAndSet(mVar, iVar2, null)) {
                                c6.f20115a = iVar2;
                                j7 = -1;
                                break;
                            }
                        } while (atomicReferenceFieldUpdater.get(mVar) == iVar2);
                        i10 = i7;
                        iVar = null;
                    }
                }
                if (j7 == j6) {
                    i iVar3 = (i) c6.f20115a;
                    c6.f20115a = iVar;
                    return iVar3;
                }
                if (j7 > j4) {
                    jMin = Math.min(jMin, j7);
                }
            }
            i9++;
            iD = i7;
            iVar = null;
        }
        if (jMin == Long.MAX_VALUE) {
            jMin = 0;
        }
        this.f13446e = 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 j4;
        int i6;
        long j6;
        loop0: while (true) {
            boolean z6 = false;
            while (true) {
                c cVar = this.f13449k;
                cVar.getClass();
                if (c.f13457m.get(cVar) == 0) {
                    b bVar = this.f13444c;
                    b bVar2 = b.f13454e;
                    if (bVar == bVar2) {
                        break loop0;
                    }
                    i iVarA = a(this.f13448j);
                    if (iVarA != null) {
                        this.f13446e = 0L;
                        this.f13445d = 0L;
                        if (this.f13444c == b.f13452c) {
                            this.f13444c = b.f13451b;
                        }
                        boolean z7 = iVarA.f13472b;
                        c cVar2 = this.f13449k;
                        if (z7) {
                            if (h(b.f13451b) && !cVar2.g() && !cVar2.f(c.l.get(cVar2))) {
                                cVar2.g();
                            }
                            cVar2.getClass();
                            try {
                                iVarA.run();
                            } catch (Throwable th) {
                                Thread threadCurrentThread = Thread.currentThread();
                                threadCurrentThread.getUncaughtExceptionHandler().uncaughtException(threadCurrentThread, th);
                            }
                            c.l.addAndGet(cVar2, -2097152L);
                            if (this.f13444c != bVar2) {
                                this.f13444c = b.f13453d;
                            }
                        } else {
                            cVar2.getClass();
                            try {
                                iVarA.run();
                            } catch (Throwable th2) {
                                Thread threadCurrentThread2 = Thread.currentThread();
                                threadCurrentThread2.getUncaughtExceptionHandler().uncaughtException(threadCurrentThread2, th2);
                            }
                        }
                    } else {
                        this.f13448j = false;
                        if (this.f13446e == 0) {
                            Object obj = this.nextParkedWorker;
                            C0317a c0317a = c.f13458n;
                            long j7 = 2097151;
                            if (obj != c0317a) {
                                l.set(this, -1);
                                while (this.nextParkedWorker != c.f13458n) {
                                    AtomicIntegerFieldUpdater atomicIntegerFieldUpdater = l;
                                    if (atomicIntegerFieldUpdater.get(this) == -1) {
                                        c cVar3 = this.f13449k;
                                        cVar3.getClass();
                                        AtomicIntegerFieldUpdater atomicIntegerFieldUpdater2 = c.f13457m;
                                        if (atomicIntegerFieldUpdater2.get(cVar3) != 0) {
                                            break;
                                        }
                                        b bVar3 = this.f13444c;
                                        b bVar4 = b.f13454e;
                                        if (bVar3 == bVar4) {
                                            break;
                                        }
                                        h(b.f13452c);
                                        Thread.interrupted();
                                        if (this.f13445d == 0) {
                                            j6 = j7;
                                            this.f13445d = System.nanoTime() + this.f13449k.f13461c;
                                        } else {
                                            j6 = j7;
                                        }
                                        LockSupport.parkNanos(this.f13449k.f13461c);
                                        if (System.nanoTime() - this.f13445d >= 0) {
                                            this.f13445d = 0L;
                                            c cVar4 = this.f13449k;
                                            synchronized (cVar4.f13465j) {
                                                try {
                                                    if (!(atomicIntegerFieldUpdater2.get(cVar4) != 0)) {
                                                        AtomicLongFieldUpdater atomicLongFieldUpdater2 = c.l;
                                                        if (((int) (atomicLongFieldUpdater2.get(cVar4) & j6)) > cVar4.f13459a) {
                                                            if (atomicIntegerFieldUpdater.compareAndSet(this, -1, 1)) {
                                                                int i7 = this.indexInArray;
                                                                f(0);
                                                                cVar4.d(this, i7, 0);
                                                                int andDecrement = (int) (atomicLongFieldUpdater2.getAndDecrement(cVar4) & j6);
                                                                if (andDecrement != i7) {
                                                                    Object objB = cVar4.f13465j.b(andDecrement);
                                                                    o.e(objB);
                                                                    a aVar = (a) objB;
                                                                    cVar4.f13465j.c(i7, aVar);
                                                                    aVar.f(i7);
                                                                    cVar4.d(aVar, andDecrement, i7);
                                                                }
                                                                cVar4.f13465j.c(andDecrement, null);
                                                                this.f13444c = bVar4;
                                                            }
                                                        }
                                                    }
                                                } catch (Throwable th3) {
                                                    throw th3;
                                                }
                                            }
                                        }
                                        j7 = j6;
                                    }
                                }
                            } else {
                                c cVar5 = this.f13449k;
                                cVar5.getClass();
                                if (this.nextParkedWorker == c0317a) {
                                    do {
                                        atomicLongFieldUpdater = c.f13456k;
                                        j4 = atomicLongFieldUpdater.get(cVar5);
                                        i6 = this.indexInArray;
                                        this.nextParkedWorker = cVar5.f13465j.b((int) (j4 & 2097151));
                                    } while (!atomicLongFieldUpdater.compareAndSet(cVar5, j4, ((2097152 + j4) & (-2097152)) | ((long) i6)));
                                }
                            }
                        } else {
                            if (z6) {
                                h(b.f13452c);
                                Thread.interrupted();
                                LockSupport.parkNanos(this.f13446e);
                                this.f13446e = 0L;
                                break;
                            }
                            z6 = true;
                        }
                    }
                } else {
                    break loop0;
                }
            }
        }
        h(b.f13454e);
    }
}
