package o; import java.io.IOException; import java.io.InterruptedIOException; import java.util.concurrent.TimeUnit; /* loaded from: classes.dex */ public class ghX extends C15139giB { public static final RVV Companion = new RVV(0); private static final long IDLE_TIMEOUT_MILLIS; private static final long IDLE_TIMEOUT_NANOS; private static final int TIMEOUT_WRITE_SIZE = 65536; private static ghX head; private boolean inQueue; private ghX next; private long timeoutAt; protected void timedOut() { } public final void enter() { long timeoutNanos = timeoutNanos(); boolean hasDeadline = hasDeadline(); if (timeoutNanos != 0 || hasDeadline) { RVV.b(this, timeoutNanos, hasDeadline); } } public final boolean exit() { return RVV.d(this); } /* loaded from: classes.dex */ public static final class IeS implements InterfaceC15168giw { private ghX a; private InterfaceC15168giw e; IeS(ghX ghx, InterfaceC15168giw interfaceC15168giw) { this.a = ghx; this.e = interfaceC15168giw; } public final String toString() { StringBuilder sb = new StringBuilder("AsyncTimeout.sink("); sb.append(this.e); sb.append(')'); return sb.toString(); } @Override // o.InterfaceC15168giw, java.io.Flushable public final void flush() { ghX ghx = this.a; InterfaceC15168giw interfaceC15168giw = this.e; ghx.enter(); try { interfaceC15168giw.flush(); C14866gag c14866gag = C14866gag.c; if (ghx.exit()) { throw ghx.access$newTimeoutException(null); } } catch (IOException e) { e = e; if (ghx.exit()) { e = ghx.access$newTimeoutException(e); } throw e; } finally { ghx.exit(); } } @Override // o.InterfaceC15168giw, java.io.Closeable, java.lang.AutoCloseable public final void close() { ghX ghx = this.a; InterfaceC15168giw interfaceC15168giw = this.e; ghx.enter(); try { interfaceC15168giw.close(); C14866gag c14866gag = C14866gag.c; if (ghx.exit()) { throw ghx.access$newTimeoutException(null); } } catch (IOException e) { e = e; if (ghx.exit()) { e = ghx.access$newTimeoutException(e); } throw e; } finally { ghx.exit(); } } @Override // o.InterfaceC15168giw public final /* bridge */ /* synthetic */ C15139giB timeout() { return this.a; } @Override // o.InterfaceC15168giw public final void write(C15111ghZ c15111ghZ, long j) { C14957gcv.e(c15111ghZ, ""); giE.d(c15111ghZ.c, 0L, j); while (true) { long j2 = 0; if (j <= 0) { return; } C15170giy c15170giy = c15111ghZ.d; C14957gcv.e(c15170giy); while (true) { if (j2 >= 65536) { break; } j2 += c15170giy.d - c15170giy.e; if (j2 >= j) { j2 = j; break; } else { c15170giy = c15170giy.a; C14957gcv.e(c15170giy); } } ghX ghx = this.a; InterfaceC15168giw interfaceC15168giw = this.e; ghx.enter(); try { interfaceC15168giw.write(c15111ghZ, j2); C14866gag c14866gag = C14866gag.c; if (ghx.exit()) { throw ghx.access$newTimeoutException(null); } j -= j2; } catch (IOException e) { e = e; if (ghx.exit()) { e = ghx.access$newTimeoutException(e); } throw e; } finally { ghx.exit(); } } } } public final InterfaceC15168giw sink(InterfaceC15168giw interfaceC15168giw) { C14957gcv.e(interfaceC15168giw, ""); return new IeS(this, interfaceC15168giw); } /* loaded from: classes.dex */ public static final class LWm implements giD { private ghX b; private giD e; LWm(ghX ghx, giD gid) { this.b = ghx; this.e = gid; } public final String toString() { StringBuilder sb = new StringBuilder("AsyncTimeout.source("); sb.append(this.e); sb.append(')'); return sb.toString(); } @Override // o.giD public final long read(C15111ghZ c15111ghZ, long j) { C14957gcv.e(c15111ghZ, ""); ghX ghx = this.b; giD gid = this.e; ghx.enter(); try { long read = gid.read(c15111ghZ, j); if (ghx.exit()) { throw ghx.access$newTimeoutException(null); } return read; } catch (IOException e) { e = e; if (ghx.exit()) { e = ghx.access$newTimeoutException(e); } throw e; } finally { ghx.exit(); } } @Override // o.giD, java.io.Closeable, java.lang.AutoCloseable public final void close() { ghX ghx = this.b; giD gid = this.e; ghx.enter(); try { gid.close(); C14866gag c14866gag = C14866gag.c; if (ghx.exit()) { throw ghx.access$newTimeoutException(null); } } catch (IOException e) { e = e; if (ghx.exit()) { e = ghx.access$newTimeoutException(e); } throw e; } finally { ghx.exit(); } } @Override // o.giD public final /* bridge */ /* synthetic */ C15139giB timeout() { return this.b; } } public final giD source(giD gid) { C14957gcv.e(gid, ""); return new LWm(this, gid); } public final T withTimeout(InterfaceC14890gbN interfaceC14890gbN) { C14957gcv.e(interfaceC14890gbN, ""); enter(); try { T invoke = interfaceC14890gbN.invoke(); if (exit()) { throw access$newTimeoutException(null); } return invoke; } catch (IOException e) { e = e; if (exit()) { e = access$newTimeoutException(e); } throw e; } finally { exit(); } } public final IOException access$newTimeoutException(IOException iOException) { return newTimeoutException(iOException); } protected IOException newTimeoutException(IOException iOException) { InterruptedIOException interruptedIOException = new InterruptedIOException("timeout"); if (iOException != null) { interruptedIOException.initCause(iOException); } return interruptedIOException; } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes.dex */ public static final class Sts extends Thread { public Sts() { super("Okio Watchdog"); setDaemon(true); } @Override // java.lang.Thread, java.lang.Runnable public final void run() { ghX d; while (true) { try { synchronized (ghX.class) { RVV rvv = ghX.Companion; d = RVV.d(); if (d == ghX.head) { RVV rvv2 = ghX.Companion; ghX.head = null; return; } C14866gag c14866gag = C14866gag.c; } if (d != null) { d.timedOut(); } } catch (InterruptedException unused) { continue; } } } } /* loaded from: classes.dex */ public static final class RVV { private RVV() { } public static final /* synthetic */ void b(ghX ghx, long j, boolean z) { synchronized (ghX.class) { if (!ghx.inQueue) { ghx.inQueue = true; if (ghX.head == null) { RVV rvv = ghX.Companion; ghX.head = new ghX(); new Sts().start(); } long nanoTime = System.nanoTime(); if (j != 0 && z) { ghx.timeoutAt = Math.min(j, ghx.deadlineNanoTime() - nanoTime) + nanoTime; } else if (j != 0) { ghx.timeoutAt = j + nanoTime; } else if (z) { ghx.timeoutAt = ghx.deadlineNanoTime(); } else { throw new AssertionError(); } long remainingNanos = ghx.remainingNanos(nanoTime); ghX ghx2 = ghX.head; C14957gcv.e(ghx2); while (ghx2.next != null) { ghX ghx3 = ghx2.next; C14957gcv.e(ghx3); if (remainingNanos < ghx3.remainingNanos(nanoTime)) { break; } ghx2 = ghx2.next; C14957gcv.e(ghx2); } ghx.next = ghx2.next; ghx2.next = ghx; if (ghx2 == ghX.head) { ghX.class.notify(); } C14866gag c14866gag = C14866gag.c; } else { throw new IllegalStateException("Unbalanced enter/exit".toString()); } } } public static ghX d() throws InterruptedException { ghX ghx = ghX.head; C14957gcv.e(ghx); ghX ghx2 = ghx.next; if (ghx2 != null) { long remainingNanos = ghx2.remainingNanos(System.nanoTime()); if (remainingNanos > 0) { long j = remainingNanos / 1000000; ghX.class.wait(j, (int) (remainingNanos - (1000000 * j))); return null; } ghX ghx3 = ghX.head; C14957gcv.e(ghx3); ghx3.next = ghx2.next; ghx2.next = null; return ghx2; } long nanoTime = System.nanoTime(); ghX.class.wait(ghX.IDLE_TIMEOUT_MILLIS); ghX ghx4 = ghX.head; C14957gcv.e(ghx4); if (ghx4.next != null || System.nanoTime() - nanoTime < ghX.IDLE_TIMEOUT_NANOS) { return null; } return ghX.head; } /* JADX INFO: Access modifiers changed from: private */ public static boolean d(ghX ghx) { synchronized (ghX.class) { if (!ghx.inQueue) { return false; } ghx.inQueue = false; for (ghX ghx2 = ghX.head; ghx2 != null; ghx2 = ghx2.next) { if (ghx2.next == ghx) { ghx2.next = ghx.next; ghx.next = null; return false; } } return true; } } public /* synthetic */ RVV(byte b) { this(); } } static { long millis = TimeUnit.SECONDS.toMillis(60L); IDLE_TIMEOUT_MILLIS = millis; IDLE_TIMEOUT_NANOS = TimeUnit.MILLISECONDS.toNanos(millis); } /* JADX INFO: Access modifiers changed from: private */ public final long remainingNanos(long j) { return this.timeoutAt - j; } }