package a5;

import I2.C0317a;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceArray;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/* JADX INFO: loaded from: classes3.dex */
public final class l {

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final /* synthetic */ AtomicReferenceFieldUpdater f10838e = AtomicReferenceFieldUpdater.newUpdater(l.class, Object.class, "_next$volatile");

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final /* synthetic */ AtomicLongFieldUpdater f10839f = AtomicLongFieldUpdater.newUpdater(l.class, "_state$volatile");

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final C0317a f10840g = new C0317a("REMOVE_FROZEN", 4);
    private volatile /* synthetic */ Object _next$volatile;
    private volatile /* synthetic */ long _state$volatile;

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

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final int f10843c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final /* synthetic */ AtomicReferenceArray f10844d;

    public l(int i6, boolean z6) {
        this.f10841a = i6;
        this.f10842b = z6;
        int i7 = i6 - 1;
        this.f10843c = i7;
        this.f10844d = new AtomicReferenceArray(i6);
        if (i7 > 1073741823) {
            throw new IllegalStateException("Check failed.");
        }
        if ((i6 & i7) != 0) {
            throw new IllegalStateException("Check failed.");
        }
    }

    public final int a(Runnable runnable) {
        while (true) {
            AtomicLongFieldUpdater atomicLongFieldUpdater = f10839f;
            long j4 = atomicLongFieldUpdater.get(this);
            if ((3458764513820540928L & j4) != 0) {
                return (2305843009213693952L & j4) != 0 ? 2 : 1;
            }
            int i6 = (int) (1073741823 & j4);
            int i7 = (int) ((1152921503533105152L & j4) >> 30);
            int i8 = this.f10843c;
            if (((i7 + 2) & i8) == (i6 & i8)) {
                return 1;
            }
            AtomicReferenceArray atomicReferenceArray = this.f10844d;
            if (!this.f10842b && atomicReferenceArray.get(i7 & i8) != null) {
                int i9 = this.f10841a;
                if (i9 < 1024 || ((i7 - i6) & 1073741823) > (i9 >> 1)) {
                    return 1;
                }
            } else if (atomicLongFieldUpdater.compareAndSet(this, j4, ((-1152921503533105153L) & j4) | (((long) ((i7 + 1) & 1073741823)) << 30))) {
                atomicReferenceArray.set(i7 & i8, runnable);
                l lVarC = this;
                while ((atomicLongFieldUpdater.get(lVarC) & 1152921504606846976L) != 0) {
                    lVarC = lVarC.c();
                    AtomicReferenceArray atomicReferenceArray2 = lVarC.f10844d;
                    int i10 = lVarC.f10843c & i7;
                    Object obj = atomicReferenceArray2.get(i10);
                    if ((obj instanceof k) && ((k) obj).f10837a == i7) {
                        atomicReferenceArray2.set(i10, runnable);
                    } else {
                        lVarC = null;
                    }
                    if (lVarC == null) {
                        return 0;
                    }
                }
                return 0;
            }
        }
    }

    public final boolean b() {
        AtomicLongFieldUpdater atomicLongFieldUpdater;
        long j4;
        do {
            atomicLongFieldUpdater = f10839f;
            j4 = atomicLongFieldUpdater.get(this);
            if ((j4 & 2305843009213693952L) != 0) {
                return true;
            }
            if ((1152921504606846976L & j4) != 0) {
                return false;
            }
        } while (!atomicLongFieldUpdater.compareAndSet(this, j4, 2305843009213693952L | j4));
        return true;
    }

    public final l c() {
        AtomicLongFieldUpdater atomicLongFieldUpdater;
        long j4;
        l lVar;
        while (true) {
            atomicLongFieldUpdater = f10839f;
            j4 = atomicLongFieldUpdater.get(this);
            if ((j4 & 1152921504606846976L) != 0) {
                lVar = this;
                break;
            }
            long j6 = 1152921504606846976L | j4;
            lVar = this;
            if (atomicLongFieldUpdater.compareAndSet(lVar, j4, j6)) {
                j4 = j6;
                break;
            }
        }
        while (true) {
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f10838e;
            l lVar2 = (l) atomicReferenceFieldUpdater.get(this);
            if (lVar2 != null) {
                return lVar2;
            }
            l lVar3 = new l(lVar.f10841a * 2, lVar.f10842b);
            int i6 = (int) (1073741823 & j4);
            int i7 = (int) ((1152921503533105152L & j4) >> 30);
            while (true) {
                int i8 = lVar.f10843c;
                int i9 = i6 & i8;
                if (i9 == (i8 & i7)) {
                    break;
                }
                Object kVar = lVar.f10844d.get(i9);
                if (kVar == null) {
                    kVar = new k(i6);
                }
                lVar3.f10844d.set(lVar3.f10843c & i6, kVar);
                i6++;
            }
            atomicLongFieldUpdater.set(lVar3, (-1152921504606846977L) & j4);
            while (!atomicReferenceFieldUpdater.compareAndSet(this, null, lVar3) && atomicReferenceFieldUpdater.get(this) == null) {
            }
        }
    }

    /* JADX WARN: Code restructure failed: missing block: B:16:0x0040, code lost:
    
        return null;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object d() {
        l lVarC = this;
        while (true) {
            AtomicLongFieldUpdater atomicLongFieldUpdater = f10839f;
            long j4 = atomicLongFieldUpdater.get(lVarC);
            if ((j4 & 1152921504606846976L) != 0) {
                return f10840g;
            }
            int i6 = (int) (j4 & 1073741823);
            int i7 = lVarC.f10843c;
            int i8 = ((int) ((1152921503533105152L & j4) >> 30)) & i7;
            int i9 = i7 & i6;
            if (i8 == i9) {
                break;
            }
            AtomicReferenceArray atomicReferenceArray = lVarC.f10844d;
            Object obj = atomicReferenceArray.get(i9);
            boolean z6 = lVarC.f10842b;
            if (obj == null) {
                if (z6) {
                    break;
                }
            } else {
                if (obj instanceof k) {
                    break;
                }
                long j6 = (i6 + 1) & 1073741823;
                if (atomicLongFieldUpdater.compareAndSet(lVarC, j4, (j4 & (-1073741824)) | j6)) {
                    atomicReferenceArray.set(i9, null);
                    return obj;
                }
                lVarC = this;
                if (z6) {
                    while (true) {
                        AtomicLongFieldUpdater atomicLongFieldUpdater2 = f10839f;
                        long j7 = atomicLongFieldUpdater2.get(lVarC);
                        int i10 = (int) (j7 & 1073741823);
                        if ((j7 & 1152921504606846976L) != 0) {
                            lVarC = lVarC.c();
                        } else {
                            l lVar = lVarC;
                            lVarC = lVar;
                            if (atomicLongFieldUpdater2.compareAndSet(lVar, j7, (j7 & (-1073741824)) | j6)) {
                                lVarC.f10844d.set(lVarC.f10843c & i10, null);
                                lVarC = null;
                            } else {
                                continue;
                            }
                        }
                        if (lVarC == null) {
                            return obj;
                        }
                    }
                }
            }
        }
    }
}
