package g0; import d0.z.d.m; import java.io.IOException; import java.io.InterruptedIOException; import java.util.concurrent.TimeUnit; import kotlin.jvm.internal.DefaultConstructorMarker; /* compiled from: AsyncTimeout.kt */ public class b extends y { public static final long e; public static final long f; public static b g; public static final a h = new a(null); public boolean i; public b j; public long k; /* compiled from: AsyncTimeout.kt */ public static final class a { public a(DefaultConstructorMarker defaultConstructorMarker) { } public final b a() throws InterruptedException { b bVar = b.g; if (bVar == null) { m.throwNpe(); } b bVar2 = bVar.j; if (bVar2 == null) { long nanoTime = System.nanoTime(); b.class.wait(b.e); b bVar3 = b.g; if (bVar3 == null) { m.throwNpe(); } if (bVar3.j != null || System.nanoTime() - nanoTime < b.f) { return null; } return b.g; } long nanoTime2 = bVar2.k - System.nanoTime(); if (nanoTime2 > 0) { long j = nanoTime2 / 1000000; b.class.wait(j, (int) (nanoTime2 - (1000000 * j))); return null; } b bVar4 = b.g; if (bVar4 == null) { m.throwNpe(); } bVar4.j = bVar2.j; bVar2.j = null; return bVar2; } } /* compiled from: AsyncTimeout.kt */ /* renamed from: g0.b$b reason: collision with other inner class name */ public static final class C0311b extends Thread { public C0311b() { super("Okio Watchdog"); setDaemon(true); } @Override // java.lang.Thread, java.lang.Runnable public void run() { b a; while (true) { try { synchronized (b.class) { a = b.h.a(); if (a == b.g) { b.g = null; return; } } if (a != null) { a.l(); } } catch (InterruptedException unused) { } } } } static { long millis = TimeUnit.SECONDS.toMillis(60); e = millis; f = TimeUnit.MILLISECONDS.toNanos(millis); } public final void i() { b bVar; if (!this.i) { long j = this.d; boolean z2 = this.b; int i = (j > 0 ? 1 : (j == 0 ? 0 : -1)); if (i != 0 || z2) { this.i = true; synchronized (b.class) { if (g == null) { g = new b(); new C0311b().start(); } long nanoTime = System.nanoTime(); if (i != 0 && z2) { this.k = Math.min(j, c() - nanoTime) + nanoTime; } else if (i != 0) { this.k = j + nanoTime; } else if (z2) { this.k = c(); } else { throw new AssertionError(); } long j2 = this.k - nanoTime; b bVar2 = g; if (bVar2 == null) { m.throwNpe(); } while (true) { bVar = bVar2.j; if (bVar == null) { break; } else if (j2 < bVar.k - nanoTime) { break; } else { bVar2 = bVar; } } this.j = bVar; bVar2.j = this; if (bVar2 == g) { b.class.notify(); } } return; } return; } throw new IllegalStateException("Unbalanced enter/exit".toString()); } public final boolean j() { boolean z2 = false; if (!this.i) { return false; } this.i = false; synchronized (b.class) { b bVar = g; while (true) { if (bVar == null) { z2 = true; break; } b bVar2 = bVar.j; if (bVar2 == this) { bVar.j = this.j; this.j = null; break; } bVar = bVar2; } } return z2; } public IOException k(IOException iOException) { InterruptedIOException interruptedIOException = new InterruptedIOException("timeout"); if (iOException != null) { interruptedIOException.initCause(iOException); } return interruptedIOException; } public void l() { } }