package kotlinx.coroutines.internal;

/* JADX INFO: compiled from: r8-map-id-ae6aab8d9a1033945587ffdd1bc6222159522c1d66e66a2c4cecb065134dd0a6 */
/* JADX INFO: loaded from: E:\ios\vf_dex\classes3.dex */
@kotlinx.coroutines.InternalCoroutinesApi
@kotlin.Metadata(d1 = {"\u0000@\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0010\u000f\n\u0000\n\u0002\u0010\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\b\n\u0000\n\u0002\u0010\u0002\n\u0002\b\u0003\n\u0002\u0010\u0011\n\u0002\b\b\n\u0002\u0010\u000b\n\u0002\b\u0010\n\u0002\u0018\u0002\n\u0002\b\u0002\b\u0017\u0018\u0000*\u0012\b\u0000\u0010\u0003*\u00020\u0001*\b\u0012\u0004\u0012\u00028\u00000\u00022\u00060\u0004j\u0002`\u0005B\u0007¢\u0006\u0004\b\u0006\u0010\u0007J\u0018\u0010\u000b\u001a\u00020\n2\u0006\u0010\t\u001a\u00020\bH\u0082\u0010¢\u0006\u0004\b\u000b\u0010\fJ\u0018\u0010\r\u001a\u00020\n2\u0006\u0010\t\u001a\u00020\bH\u0082\u0010¢\u0006\u0004\b\r\u0010\fJ\u0017\u0010\u000f\u001a\n\u0012\u0006\u0012\u0004\u0018\u00018\u00000\u000eH\u0002¢\u0006\u0004\b\u000f\u0010\u0010J\u001f\u0010\u0012\u001a\u00020\n2\u0006\u0010\t\u001a\u00020\b2\u0006\u0010\u0011\u001a\u00020\bH\u0002¢\u0006\u0004\b\u0012\u0010\u0013J\u000f\u0010\u0014\u001a\u0004\u0018\u00018\u0000¢\u0006\u0004\b\u0014\u0010\u0015J\u000f\u0010\u0011\u001a\u0004\u0018\u00018\u0000¢\u0006\u0004\b\u0011\u0010\u0015J\u0015\u0010\u0018\u001a\u00020\u00172\u0006\u0010\u0016\u001a\u00028\u0000¢\u0006\u0004\b\u0018\u0010\u0019J\u0011\u0010\u001a\u001a\u0004\u0018\u00018\u0000H\u0001¢\u0006\u0004\b\u001a\u0010\u0015J\u0017\u0010\t\u001a\u00028\u00002\u0006\u0010\u001b\u001a\u00020\bH\u0001¢\u0006\u0004\b\t\u0010\u001cJ\u0017\u0010\u001d\u001a\u00020\n2\u0006\u0010\u0016\u001a\u00028\u0000H\u0001¢\u0006\u0004\b\u001d\u0010\u001eR \u0010\u001d\u001a\f\u0012\u0006\u0012\u0004\u0018\u00018\u0000\u0018\u00010\u000e8\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b\u001d\u0010\u001fR$\u0010$\u001a\u00020\b2\u0006\u0010 \u001a\u00020\b8F@BX\u0086\u000e¢\u0006\f\u001a\u0004\b!\u0010\"\"\u0004\b#\u0010\fR\u0011\u0010'\u001a\u00020\u00178F¢\u0006\u0006\u001a\u0004\b%\u0010&R\u000b\u0010)\u001a\u00020(8\u0002X\u0082\u0004¨\u0006*"}, d2 = {"Lkotlinx/coroutines/internal/ThreadSafeHeap;", "Lkotlinx/coroutines/internal/ThreadSafeHeapNode;", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "T", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "Lkotlinx/coroutines/internal/SynchronizedObject;", "<init>", "()V", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "i", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "m", "(I)V", "l", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "g", "()[Lkotlinx/coroutines/internal/ThreadSafeHeapNode;", "j", "n", "(II)V", "f", "()Lkotlinx/coroutines/internal/ThreadSafeHeapNode;", "node", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "h", "(Lkotlinx/coroutines/internal/ThreadSafeHeapNode;)Z", "b", "index", "(I)Lkotlinx/coroutines/internal/ThreadSafeHeapNode;", "a", "(Lkotlinx/coroutines/internal/ThreadSafeHeapNode;)V", "[Lkotlinx/coroutines/internal/ThreadSafeHeapNode;", "value", "c", "()I", "k", "size", "e", "()Z", "isEmpty", "Lkotlinx/atomicfu/AtomicInt;", "_size", "kotlinx-coroutines-core"}, k = 1, mv = {2, 1, 0}, xi = 48)
@kotlin.jvm.internal.SourceDebugExtension
public class ThreadSafeHeap<T extends kotlinx.coroutines.internal.ThreadSafeHeapNode & java.lang.Comparable<? super T>> {
    private static final /* synthetic */ java.util.concurrent.atomic.AtomicIntegerFieldUpdater b = java.util.concurrent.atomic.AtomicIntegerFieldUpdater.newUpdater(kotlinx.coroutines.internal.ThreadSafeHeap.class, "_size$volatile");
    private volatile /* synthetic */ int _size$volatile;
    private kotlinx.coroutines.internal.ThreadSafeHeapNode[] a;

    private final kotlinx.coroutines.internal.ThreadSafeHeapNode[] g() {
        kotlinx.coroutines.internal.ThreadSafeHeapNode[] threadSafeHeapNodeArr = this.a;
        if (threadSafeHeapNodeArr == null) {
            kotlinx.coroutines.internal.ThreadSafeHeapNode[] threadSafeHeapNodeArr2 = new kotlinx.coroutines.internal.ThreadSafeHeapNode[4];
            this.a = threadSafeHeapNodeArr2;
            return threadSafeHeapNodeArr2;
        }
        if (c() < threadSafeHeapNodeArr.length) {
            return threadSafeHeapNodeArr;
        }
        java.lang.Object[] objArrCopyOf = java.util.Arrays.copyOf(threadSafeHeapNodeArr, c() * 2);
        kotlin.jvm.internal.Intrinsics.k(objArrCopyOf, "copyOf(...)");
        kotlinx.coroutines.internal.ThreadSafeHeapNode[] threadSafeHeapNodeArr3 = (kotlinx.coroutines.internal.ThreadSafeHeapNode[]) objArrCopyOf;
        this.a = threadSafeHeapNodeArr3;
        return threadSafeHeapNodeArr3;
    }

    private final void k(int i) {
        b.set(this, i);
    }

    /* JADX WARN: Code duplicated, block: B:10:0x002b  */
    private final void l(int i) {
        while (true) {
            int i2 = i * 2;
            int i5 = i2 + 1;
            if (i5 >= c()) {
                return;
            }
            kotlinx.coroutines.internal.ThreadSafeHeapNode[] threadSafeHeapNodeArr = this.a;
            kotlin.jvm.internal.Intrinsics.i(threadSafeHeapNodeArr);
            int i6 = i2 + 2;
            if (i6 < c()) {
                kotlinx.coroutines.internal.ThreadSafeHeapNode threadSafeHeapNode = threadSafeHeapNodeArr[i6];
                kotlin.jvm.internal.Intrinsics.i(threadSafeHeapNode);
                kotlinx.coroutines.internal.ThreadSafeHeapNode threadSafeHeapNode2 = threadSafeHeapNodeArr[i5];
                kotlin.jvm.internal.Intrinsics.i(threadSafeHeapNode2);
                if (((java.lang.Comparable) threadSafeHeapNode).compareTo(threadSafeHeapNode2) >= 0) {
                    i6 = i5;
                }
            } else {
                i6 = i5;
            }
            kotlinx.coroutines.internal.ThreadSafeHeapNode threadSafeHeapNode3 = threadSafeHeapNodeArr[i];
            kotlin.jvm.internal.Intrinsics.i(threadSafeHeapNode3);
            kotlinx.coroutines.internal.ThreadSafeHeapNode threadSafeHeapNode4 = threadSafeHeapNodeArr[i6];
            kotlin.jvm.internal.Intrinsics.i(threadSafeHeapNode4);
            if (((java.lang.Comparable) threadSafeHeapNode3).compareTo(threadSafeHeapNode4) <= 0) {
                return;
            }
            n(i, i6);
            i = i6;
        }
    }

    private final void m(int i) {
        while (i > 0) {
            kotlinx.coroutines.internal.ThreadSafeHeapNode[] threadSafeHeapNodeArr = this.a;
            kotlin.jvm.internal.Intrinsics.i(threadSafeHeapNodeArr);
            int i2 = (i - 1) / 2;
            kotlinx.coroutines.internal.ThreadSafeHeapNode threadSafeHeapNode = threadSafeHeapNodeArr[i2];
            kotlin.jvm.internal.Intrinsics.i(threadSafeHeapNode);
            kotlinx.coroutines.internal.ThreadSafeHeapNode threadSafeHeapNode2 = threadSafeHeapNodeArr[i];
            kotlin.jvm.internal.Intrinsics.i(threadSafeHeapNode2);
            if (((java.lang.Comparable) threadSafeHeapNode).compareTo(threadSafeHeapNode2) <= 0) {
                return;
            }
            n(i, i2);
            i = i2;
        }
    }

    private final void n(int i, int j) {
        kotlinx.coroutines.internal.ThreadSafeHeapNode[] threadSafeHeapNodeArr = this.a;
        kotlin.jvm.internal.Intrinsics.i(threadSafeHeapNodeArr);
        kotlinx.coroutines.internal.ThreadSafeHeapNode threadSafeHeapNode = threadSafeHeapNodeArr[j];
        kotlin.jvm.internal.Intrinsics.i(threadSafeHeapNode);
        kotlinx.coroutines.internal.ThreadSafeHeapNode threadSafeHeapNode2 = threadSafeHeapNodeArr[i];
        kotlin.jvm.internal.Intrinsics.i(threadSafeHeapNode2);
        threadSafeHeapNodeArr[i] = threadSafeHeapNode;
        threadSafeHeapNodeArr[j] = threadSafeHeapNode2;
        threadSafeHeapNode.setIndex(i);
        threadSafeHeapNode2.setIndex(j);
    }

    public final void a(kotlinx.coroutines.internal.ThreadSafeHeapNode node) {
        node.a(this);
        kotlinx.coroutines.internal.ThreadSafeHeapNode[] threadSafeHeapNodeArrG = g();
        int iC = c();
        k(iC + 1);
        threadSafeHeapNodeArrG[iC] = node;
        node.setIndex(iC);
        m(iC);
    }

    public final kotlinx.coroutines.internal.ThreadSafeHeapNode b() {
        kotlinx.coroutines.internal.ThreadSafeHeapNode[] threadSafeHeapNodeArr = this.a;
        if (threadSafeHeapNodeArr != null) {
            return threadSafeHeapNodeArr[0];
        }
        return null;
    }

    public final int c() {
        return b.get(this);
    }

    public final boolean e() {
        return c() == 0;
    }

    public final kotlinx.coroutines.internal.ThreadSafeHeapNode f() {
        kotlinx.coroutines.internal.ThreadSafeHeapNode threadSafeHeapNodeB;
        synchronized (this) {
            threadSafeHeapNodeB = b();
        }
        return threadSafeHeapNodeB;
    }

    public final boolean h(kotlinx.coroutines.internal.ThreadSafeHeapNode node) {
        boolean z;
        synchronized (this) {
            if (node.c() == null) {
                z = false;
            } else {
                i(node.getIndex());
                z = true;
            }
        }
        return z;
    }

    /* JADX WARN: Code duplicated, block: B:9:0x003a  */
    public final kotlinx.coroutines.internal.ThreadSafeHeapNode i(int index) {
        kotlinx.coroutines.internal.ThreadSafeHeapNode[] threadSafeHeapNodeArr = this.a;
        kotlin.jvm.internal.Intrinsics.i(threadSafeHeapNodeArr);
        k(c() - 1);
        if (index < c()) {
            n(index, c());
            int i = (index - 1) / 2;
            if (index > 0) {
                kotlinx.coroutines.internal.ThreadSafeHeapNode threadSafeHeapNode = threadSafeHeapNodeArr[index];
                kotlin.jvm.internal.Intrinsics.i(threadSafeHeapNode);
                kotlinx.coroutines.internal.ThreadSafeHeapNode threadSafeHeapNode2 = threadSafeHeapNodeArr[i];
                kotlin.jvm.internal.Intrinsics.i(threadSafeHeapNode2);
                if (((java.lang.Comparable) threadSafeHeapNode).compareTo(threadSafeHeapNode2) < 0) {
                    n(index, i);
                    m(i);
                } else {
                    l(index);
                }
            } else {
                l(index);
            }
        }
        kotlinx.coroutines.internal.ThreadSafeHeapNode threadSafeHeapNode3 = threadSafeHeapNodeArr[c()];
        kotlin.jvm.internal.Intrinsics.i(threadSafeHeapNode3);
        threadSafeHeapNode3.a(null);
        threadSafeHeapNode3.setIndex(-1);
        threadSafeHeapNodeArr[c()] = null;
        return threadSafeHeapNode3;
    }

    public final kotlinx.coroutines.internal.ThreadSafeHeapNode j() {
        kotlinx.coroutines.internal.ThreadSafeHeapNode threadSafeHeapNodeI;
        synchronized (this) {
            threadSafeHeapNodeI = c() > 0 ? i(0) : null;
        }
        return threadSafeHeapNodeI;
    }
}
