package k9; import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; import k9.t0; /* loaded from: classes2.dex */ public abstract class g1 extends h1 implements t0 { /* renamed from: e, reason: collision with root package name */ private static final /* synthetic */ AtomicReferenceFieldUpdater f11345e = AtomicReferenceFieldUpdater.newUpdater(g1.class, Object.class, "_queue"); /* renamed from: f, reason: collision with root package name */ private static final /* synthetic */ AtomicReferenceFieldUpdater f11346f = AtomicReferenceFieldUpdater.newUpdater(g1.class, Object.class, "_delayed"); private volatile /* synthetic */ Object _queue = null; private volatile /* synthetic */ Object _delayed = null; private volatile /* synthetic */ int _isCompleted = 0; private final class a extends c { /* renamed from: d, reason: collision with root package name */ private final m f11347d; public a(long j10, m mVar) { super(j10); this.f11347d = mVar; } @Override // java.lang.Runnable public void run() { this.f11347d.g(g1.this, s8.v.f14109a); } @Override // k9.g1.c public String toString() { return d9.j.l(super.toString(), this.f11347d); } } private static final class b extends c { /* renamed from: d, reason: collision with root package name */ private final Runnable f11349d; public b(long j10, Runnable runnable) { super(j10); this.f11349d = runnable; } @Override // java.lang.Runnable public void run() { this.f11349d.run(); } @Override // k9.g1.c public String toString() { return d9.j.l(super.toString(), this.f11349d); } } public static abstract class c implements Runnable, Comparable, b1, kotlinx.coroutines.internal.e0 { /* renamed from: a, reason: collision with root package name */ public long f11350a; /* renamed from: b, reason: collision with root package name */ private Object f11351b; /* renamed from: c, reason: collision with root package name */ private int f11352c = -1; public c(long j10) { this.f11350a = j10; } @Override // kotlinx.coroutines.internal.e0 public void a(kotlinx.coroutines.internal.d0 d0Var) { kotlinx.coroutines.internal.y yVar; Object obj = this.f11351b; yVar = j1.f11358a; if (!(obj != yVar)) { throw new IllegalArgumentException("Failed requirement.".toString()); } this.f11351b = d0Var; } @Override // kotlinx.coroutines.internal.e0 public kotlinx.coroutines.internal.d0 c() { Object obj = this.f11351b; if (obj instanceof kotlinx.coroutines.internal.d0) { return (kotlinx.coroutines.internal.d0) obj; } return null; } @Override // kotlinx.coroutines.internal.e0 public void d(int i10) { this.f11352c = i10; } @Override // k9.b1 public final synchronized void e() { kotlinx.coroutines.internal.y yVar; kotlinx.coroutines.internal.y yVar2; Object obj = this.f11351b; yVar = j1.f11358a; if (obj == yVar) { return; } d dVar = obj instanceof d ? (d) obj : null; if (dVar != null) { dVar.g(this); } yVar2 = j1.f11358a; this.f11351b = yVar2; } @Override // kotlinx.coroutines.internal.e0 public int f() { return this.f11352c; } @Override // java.lang.Comparable /* renamed from: g, reason: merged with bridge method [inline-methods] */ public int compareTo(c cVar) { long j10 = this.f11350a - cVar.f11350a; if (j10 > 0) { return 1; } return j10 < 0 ? -1 : 0; } public final synchronized int h(long j10, d dVar, g1 g1Var) { kotlinx.coroutines.internal.y yVar; Object obj = this.f11351b; yVar = j1.f11358a; if (obj == yVar) { return 2; } synchronized (dVar) { c cVar = (c) dVar.b(); if (g1Var.Y0()) { return 1; } if (cVar == null) { dVar.f11353b = j10; } else { long j11 = cVar.f11350a; if (j11 - j10 < 0) { j10 = j11; } if (j10 - dVar.f11353b > 0) { dVar.f11353b = j10; } } long j12 = this.f11350a; long j13 = dVar.f11353b; if (j12 - j13 < 0) { this.f11350a = j13; } dVar.a(this); return 0; } } public final boolean i(long j10) { return j10 - this.f11350a >= 0; } public String toString() { return "Delayed[nanos=" + this.f11350a + ']'; } } public static final class d extends kotlinx.coroutines.internal.d0 { /* renamed from: b, reason: collision with root package name */ public long f11353b; public d(long j10) { this.f11353b = j10; } } private final void U0() { kotlinx.coroutines.internal.y yVar; kotlinx.coroutines.internal.y yVar2; while (true) { Object obj = this._queue; if (obj == null) { AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f11345e; yVar = j1.f11359b; if (androidx.concurrent.futures.b.a(atomicReferenceFieldUpdater, this, null, yVar)) { return; } } else { if (obj instanceof kotlinx.coroutines.internal.p) { ((kotlinx.coroutines.internal.p) obj).d(); return; } yVar2 = j1.f11359b; if (obj == yVar2) { return; } kotlinx.coroutines.internal.p pVar = new kotlinx.coroutines.internal.p(8, true); pVar.a((Runnable) obj); if (androidx.concurrent.futures.b.a(f11345e, this, obj, pVar)) { return; } } } } private final Runnable V0() { kotlinx.coroutines.internal.y yVar; while (true) { Object obj = this._queue; if (obj == null) { return null; } if (obj instanceof kotlinx.coroutines.internal.p) { kotlinx.coroutines.internal.p pVar = (kotlinx.coroutines.internal.p) obj; Object j10 = pVar.j(); if (j10 != kotlinx.coroutines.internal.p.f11475h) { return (Runnable) j10; } androidx.concurrent.futures.b.a(f11345e, this, obj, pVar.i()); } else { yVar = j1.f11359b; if (obj == yVar) { return null; } if (androidx.concurrent.futures.b.a(f11345e, this, obj, null)) { return (Runnable) obj; } } } } private final boolean X0(Runnable runnable) { kotlinx.coroutines.internal.y yVar; while (true) { Object obj = this._queue; if (Y0()) { return false; } if (obj == null) { if (androidx.concurrent.futures.b.a(f11345e, this, null, runnable)) { return true; } } else if (obj instanceof kotlinx.coroutines.internal.p) { kotlinx.coroutines.internal.p pVar = (kotlinx.coroutines.internal.p) obj; int a10 = pVar.a(runnable); if (a10 == 0) { return true; } if (a10 == 1) { androidx.concurrent.futures.b.a(f11345e, this, obj, pVar.i()); } else if (a10 == 2) { return false; } } else { yVar = j1.f11359b; if (obj == yVar) { return false; } kotlinx.coroutines.internal.p pVar2 = new kotlinx.coroutines.internal.p(8, true); pVar2.a((Runnable) obj); pVar2.a(runnable); if (androidx.concurrent.futures.b.a(f11345e, this, obj, pVar2)) { return true; } } } } /* JADX INFO: Access modifiers changed from: private */ /* JADX WARN: Type inference failed for: r0v0, types: [boolean, int] */ public final boolean Y0() { return this._isCompleted; } private final void b1() { k9.c.a(); long nanoTime = System.nanoTime(); while (true) { d dVar = (d) this._delayed; c cVar = dVar == null ? null : (c) dVar.i(); if (cVar == null) { return; } else { R0(nanoTime, cVar); } } } private final int e1(long j10, c cVar) { if (Y0()) { return 1; } d dVar = (d) this._delayed; if (dVar == null) { androidx.concurrent.futures.b.a(f11346f, this, null, new d(j10)); Object obj = this._delayed; d9.j.b(obj); dVar = (d) obj; } return cVar.h(j10, dVar, this); } private final void g1(boolean z10) { this._isCompleted = z10 ? 1 : 0; } private final boolean h1(c cVar) { d dVar = (d) this._delayed; return (dVar == null ? null : (c) dVar.e()) == cVar; } @Override // k9.g0 public final void E0(v8.g gVar, Runnable runnable) { W0(runnable); } @Override // k9.f1 protected long K0() { long b10; kotlinx.coroutines.internal.y yVar; if (super.K0() == 0) { return 0L; } Object obj = this._queue; if (obj != null) { if (!(obj instanceof kotlinx.coroutines.internal.p)) { yVar = j1.f11359b; return obj == yVar ? Long.MAX_VALUE : 0L; } if (!((kotlinx.coroutines.internal.p) obj).g()) { return 0L; } } d dVar = (d) this._delayed; c cVar = dVar == null ? null : (c) dVar.e(); if (cVar == null) { return Long.MAX_VALUE; } long j10 = cVar.f11350a; k9.c.a(); b10 = g9.f.b(j10 - System.nanoTime(), 0L); return b10; } @Override // k9.t0 public void R(long j10, m mVar) { long c10 = j1.c(j10); if (c10 < 4611686018427387903L) { k9.c.a(); long nanoTime = System.nanoTime(); a aVar = new a(c10 + nanoTime, mVar); p.a(mVar, aVar); d1(nanoTime, aVar); } } public void W0(Runnable runnable) { if (X0(runnable)) { S0(); } else { p0.f11381g.W0(runnable); } } @Override // k9.t0 public b1 Y(long j10, Runnable runnable, v8.g gVar) { return t0.a.a(this, j10, runnable, gVar); } protected boolean Z0() { kotlinx.coroutines.internal.y yVar; if (!O0()) { return false; } d dVar = (d) this._delayed; if (dVar != null && !dVar.d()) { return false; } Object obj = this._queue; if (obj != null) { if (obj instanceof kotlinx.coroutines.internal.p) { return ((kotlinx.coroutines.internal.p) obj).g(); } yVar = j1.f11359b; if (obj != yVar) { return false; } } return true; } public long a1() { kotlinx.coroutines.internal.e0 e0Var; if (P0()) { return 0L; } d dVar = (d) this._delayed; if (dVar != null && !dVar.d()) { k9.c.a(); long nanoTime = System.nanoTime(); do { synchronized (dVar) { kotlinx.coroutines.internal.e0 b10 = dVar.b(); if (b10 != null) { c cVar = (c) b10; e0Var = cVar.i(nanoTime) ? X0(cVar) : false ? dVar.h(0) : null; } } } while (((c) e0Var) != null); } Runnable V0 = V0(); if (V0 == null) { return K0(); } V0.run(); return 0L; } protected final void c1() { this._queue = null; this._delayed = null; } public final void d1(long j10, c cVar) { int e12 = e1(j10, cVar); if (e12 == 0) { if (h1(cVar)) { S0(); } } else if (e12 == 1) { R0(j10, cVar); } else if (e12 != 2) { throw new IllegalStateException("unexpected result".toString()); } } protected final b1 f1(long j10, Runnable runnable) { long c10 = j1.c(j10); if (c10 >= 4611686018427387903L) { return i2.f11356a; } k9.c.a(); long nanoTime = System.nanoTime(); b bVar = new b(c10 + nanoTime, runnable); d1(nanoTime, bVar); return bVar; } @Override // k9.f1 public void shutdown() { t2.f11393a.b(); g1(true); U0(); while (a1() <= 0) { } b1(); } }