package kotlinx.coroutines.sync;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import kotlin.coroutines.Continuation;
import kotlin.coroutines.intrinsics.CoroutineSingletons;
import kotlinx.coroutines.h;
import kotlinx.coroutines.y;

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public static final /* synthetic */ AtomicReferenceFieldUpdater f29545h = AtomicReferenceFieldUpdater.newUpdater(c.class, Object.class, "owner$volatile");
    private volatile /* synthetic */ Object owner$volatile;

    public c(boolean z10) {
        super(1, z10 ? 1 : 0);
        this.owner$volatile = z10 ? null : d.f29546a;
    }

    /* JADX WARN: Code restructure failed: missing block: B:12:0x0020, code lost:
    
        r0 = 1;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final boolean c(Object obj) {
        int i10;
        char c6;
        char c10;
        while (true) {
            AtomicIntegerFieldUpdater atomicIntegerFieldUpdater = e.f29551g;
            int i11 = atomicIntegerFieldUpdater.get(this);
            int i12 = this.f29552a;
            if (i11 > i12) {
                do {
                    i10 = atomicIntegerFieldUpdater.get(this);
                    if (i10 > i12) {
                    }
                } while (!atomicIntegerFieldUpdater.compareAndSet(this, i10, i12));
            } else {
                AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f29545h;
                if (i11 <= 0) {
                    if (obj != null) {
                        while (true) {
                            if (Math.max(atomicIntegerFieldUpdater.get(this), 0) != 0) {
                                c10 = 0;
                                break;
                            }
                            Object obj2 = atomicReferenceFieldUpdater.get(this);
                            if (obj2 != d.f29546a) {
                                c10 = obj2 == obj ? (char) 1 : (char) 2;
                            }
                        }
                        if (c10 == 1) {
                            c6 = 2;
                            break;
                        }
                        if (c10 == 2) {
                            break;
                        }
                    } else {
                        break;
                    }
                } else if (atomicIntegerFieldUpdater.compareAndSet(this, i11, i11 - 1)) {
                    atomicReferenceFieldUpdater.set(this, obj);
                    c6 = 0;
                    break;
                }
            }
        }
        if (c6 == 0) {
            return true;
        }
        if (c6 == 1) {
            return false;
        }
        if (c6 != 2) {
            throw new IllegalStateException("unexpected".toString());
        }
        throw new IllegalStateException(("This mutex is already locked by the specified owner: " + obj).toString());
    }

    /* JADX WARN: Code restructure failed: missing block: B:10:0x0023, code lost:
    
        r0.d(r1, r4.f29553b);
     */
    @Override // kotlinx.coroutines.sync.a
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object e(Continuation continuation) throws Throwable {
        boolean zC = c(null);
        kd.e eVar = kd.e.f29109a;
        if (zC) {
            return eVar;
        }
        h hVarC0 = hb.a.c0(nb.g.F(continuation));
        try {
            b bVar = new b(this, hVarC0);
            while (true) {
                int andDecrement = e.f29551g.getAndDecrement(this);
                if (andDecrement <= this.f29552a) {
                    if (andDecrement > 0) {
                        break;
                    }
                    if (a(bVar)) {
                        break;
                    }
                }
            }
            Object objS = hVarC0.s();
            CoroutineSingletons coroutineSingletons = CoroutineSingletons.f29226a;
            if (objS != coroutineSingletons) {
                objS = eVar;
            }
            return objS == coroutineSingletons ? objS : eVar;
        } catch (Throwable th) {
            hVarC0.A();
            throw th;
        }
    }

    @Override // kotlinx.coroutines.sync.a
    public final void g(Object obj) {
        while (Math.max(e.f29551g.get(this), 0) == 0) {
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f29545h;
            Object obj2 = atomicReferenceFieldUpdater.get(this);
            la.e eVar = d.f29546a;
            if (obj2 != eVar) {
                if (obj2 == obj || obj == null) {
                    while (!atomicReferenceFieldUpdater.compareAndSet(this, obj2, eVar)) {
                        if (atomicReferenceFieldUpdater.get(this) != obj2) {
                            break;
                        }
                    }
                    b();
                    return;
                }
                throw new IllegalStateException(("This mutex is locked by " + obj2 + ", but " + obj + " is expected").toString());
            }
        }
        throw new IllegalStateException("This mutex is not locked".toString());
    }

    public final String toString() {
        StringBuilder sb = new StringBuilder("Mutex@");
        sb.append(y.z(this));
        sb.append("[isLocked=");
        sb.append(Math.max(e.f29551g.get(this), 0) == 0);
        sb.append(",owner=");
        sb.append(f29545h.get(this));
        sb.append(']');
        return sb.toString();
    }
}
