package dq;

import dn.l;
import dn.q;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import kotlin.coroutines.jvm.internal.h;
import kotlin.jvm.internal.v;
import rm.n0;
import tp.i0;
import tp.o;
import tp.p;
import tp.q0;
import tp.r;
import tp.y2;
import vm.g;
import zp.e0;
import zp.h0;

/* loaded from: classes3.dex */
public class b extends d implements dq.a {

    /* renamed from: i, reason: collision with root package name */
    private static final AtomicReferenceFieldUpdater f16537i = AtomicReferenceFieldUpdater.newUpdater(b.class, Object.class, "owner");

    /* renamed from: h, reason: collision with root package name */
    private final q f16538h;
    private volatile Object owner;

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes3.dex */
    public final class a implements o, y2 {

        /* renamed from: p, reason: collision with root package name */
        public final p f16539p;

        /* renamed from: q, reason: collision with root package name */
        public final Object f16540q;

        /* JADX INFO: Access modifiers changed from: package-private */
        /* renamed from: dq.b$a$a, reason: collision with other inner class name */
        /* loaded from: classes3.dex */
        public static final class C0355a extends v implements l {

            /* renamed from: p, reason: collision with root package name */
            final /* synthetic */ b f16542p;

            /* renamed from: q, reason: collision with root package name */
            final /* synthetic */ a f16543q;

            /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
            C0355a(b bVar, a aVar) {
                super(1);
                this.f16542p = bVar;
                this.f16543q = aVar;
            }

            public final void a(Throwable th2) {
                this.f16542p.d(this.f16543q.f16540q);
            }

            @Override // dn.l
            public /* bridge */ /* synthetic */ Object invoke(Object obj) {
                a((Throwable) obj);
                return n0.f34904a;
            }
        }

        /* JADX INFO: Access modifiers changed from: package-private */
        /* renamed from: dq.b$a$b, reason: collision with other inner class name */
        /* loaded from: classes3.dex */
        public static final class C0356b extends v implements l {

            /* renamed from: p, reason: collision with root package name */
            final /* synthetic */ b f16544p;

            /* renamed from: q, reason: collision with root package name */
            final /* synthetic */ a f16545q;

            /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
            C0356b(b bVar, a aVar) {
                super(1);
                this.f16544p = bVar;
                this.f16545q = aVar;
            }

            public final void a(Throwable th2) {
                b.f16537i.set(this.f16544p, this.f16545q.f16540q);
                this.f16544p.d(this.f16545q.f16540q);
            }

            @Override // dn.l
            public /* bridge */ /* synthetic */ Object invoke(Object obj) {
                a((Throwable) obj);
                return n0.f34904a;
            }
        }

        public a(p pVar, Object obj) {
            this.f16539p = pVar;
            this.f16540q = obj;
        }

        @Override // tp.o
        public Object B(Throwable th2) {
            return this.f16539p.B(th2);
        }

        @Override // tp.o
        public boolean L(Throwable th2) {
            return this.f16539p.L(th2);
        }

        @Override // tp.o
        public boolean R() {
            return this.f16539p.R();
        }

        @Override // tp.o
        public void W(Object obj) {
            this.f16539p.W(obj);
        }

        @Override // tp.y2
        public void a(e0 e0Var, int i10) {
            this.f16539p.a(e0Var, i10);
        }

        @Override // tp.o
        /* renamed from: b, reason: merged with bridge method [inline-methods] */
        public void z(n0 n0Var, l lVar) {
            b.f16537i.set(b.this, this.f16540q);
            this.f16539p.z(n0Var, new C0355a(b.this, this));
        }

        @Override // tp.o
        /* renamed from: c, reason: merged with bridge method [inline-methods] */
        public void C(i0 i0Var, n0 n0Var) {
            this.f16539p.C(i0Var, n0Var);
        }

        @Override // tp.o
        /* renamed from: d, reason: merged with bridge method [inline-methods] */
        public Object M(n0 n0Var, Object obj, l lVar) {
            Object M = this.f16539p.M(n0Var, obj, new C0356b(b.this, this));
            if (M != null) {
                b.f16537i.set(b.this, this.f16540q);
            }
            return M;
        }

        @Override // vm.d
        public g getContext() {
            return this.f16539p.getContext();
        }

        @Override // vm.d
        public void resumeWith(Object obj) {
            this.f16539p.resumeWith(obj);
        }

        @Override // tp.o
        public void x(l lVar) {
            this.f16539p.x(lVar);
        }
    }

    /* renamed from: dq.b$b, reason: collision with other inner class name */
    /* loaded from: classes3.dex */
    static final class C0357b extends v implements q {

        /* JADX INFO: Access modifiers changed from: package-private */
        /* renamed from: dq.b$b$a */
        /* loaded from: classes3.dex */
        public static final class a extends v implements l {

            /* renamed from: p, reason: collision with root package name */
            final /* synthetic */ b f16547p;

            /* renamed from: q, reason: collision with root package name */
            final /* synthetic */ Object f16548q;

            /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
            a(b bVar, Object obj) {
                super(1);
                this.f16547p = bVar;
                this.f16548q = obj;
            }

            public final void a(Throwable th2) {
                this.f16547p.d(this.f16548q);
            }

            @Override // dn.l
            public /* bridge */ /* synthetic */ Object invoke(Object obj) {
                a((Throwable) obj);
                return n0.f34904a;
            }
        }

        C0357b() {
            super(3);
        }

        public final l a(cq.a aVar, Object obj, Object obj2) {
            return new a(b.this, obj);
        }

        @Override // dn.q
        public /* bridge */ /* synthetic */ Object invoke(Object obj, Object obj2, Object obj3) {
            android.support.v4.media.session.b.a(obj);
            return a(null, obj2, obj3);
        }
    }

    public b(boolean z10) {
        super(1, z10 ? 1 : 0);
        this.owner = z10 ? null : c.f16549a;
        this.f16538h = new C0357b();
    }

    private final int o(Object obj) {
        h0 h0Var;
        while (c()) {
            Object obj2 = f16537i.get(this);
            h0Var = c.f16549a;
            if (obj2 != h0Var) {
                return obj2 == obj ? 1 : 2;
            }
        }
        return 0;
    }

    static /* synthetic */ Object p(b bVar, Object obj, vm.d dVar) {
        Object f10;
        if (bVar.b(obj)) {
            return n0.f34904a;
        }
        Object q10 = bVar.q(obj, dVar);
        f10 = wm.d.f();
        return q10 == f10 ? q10 : n0.f34904a;
    }

    private final Object q(Object obj, vm.d dVar) {
        vm.d d10;
        Object f10;
        Object f11;
        d10 = wm.c.d(dVar);
        p b10 = r.b(d10);
        try {
            e(new a(b10, obj));
            Object r10 = b10.r();
            f10 = wm.d.f();
            if (r10 == f10) {
                h.c(dVar);
            }
            f11 = wm.d.f();
            return r10 == f11 ? r10 : n0.f34904a;
        } catch (Throwable th2) {
            b10.G();
            throw th2;
        }
    }

    private final int r(Object obj) {
        while (!k()) {
            if (obj == null) {
                return 1;
            }
            int o10 = o(obj);
            if (o10 == 1) {
                return 2;
            }
            if (o10 == 2) {
                return 1;
            }
        }
        f16537i.set(this, obj);
        return 0;
    }

    @Override // dq.a
    public Object a(Object obj, vm.d dVar) {
        return p(this, obj, dVar);
    }

    @Override // dq.a
    public boolean b(Object obj) {
        int r10 = r(obj);
        if (r10 == 0) {
            return true;
        }
        if (r10 == 1) {
            return false;
        }
        if (r10 != 2) {
            throw new IllegalStateException("unexpected".toString());
        }
        throw new IllegalStateException(("This mutex is already locked by the specified owner: " + obj).toString());
    }

    @Override // dq.a
    public boolean c() {
        return i() == 0;
    }

    @Override // dq.a
    public void d(Object obj) {
        h0 h0Var;
        h0 h0Var2;
        while (c()) {
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f16537i;
            Object obj2 = atomicReferenceFieldUpdater.get(this);
            h0Var = c.f16549a;
            if (obj2 != h0Var) {
                if (!(obj2 == obj || obj == null)) {
                    throw new IllegalStateException(("This mutex is locked by " + obj2 + ", but " + obj + " is expected").toString());
                }
                h0Var2 = c.f16549a;
                if (androidx.concurrent.futures.b.a(atomicReferenceFieldUpdater, this, obj2, h0Var2)) {
                    j();
                    return;
                }
            }
        }
        throw new IllegalStateException("This mutex is not locked".toString());
    }

    public String toString() {
        return "Mutex@" + q0.b(this) + "[isLocked=" + c() + ",owner=" + f16537i.get(this) + ']';
    }
}
