content: package okio;
  
  import java.io.IOException;
  import java.io.InterruptedIOException;
  import java.util.concurrent.TimeUnit;
  import kotlin.Metadata;
  import kotlin.Unit;
  import kotlin.jvm.functions.Function0;
  import kotlin.jvm.internal.DefaultConstructorMarker;
  import kotlin.jvm.internal.InlineMarker;
  import kotlin.jvm.internal.Intrinsics;
  
  @Metadata(bv = {1, 0, 3}, d1 = {"\u0000@\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u000b\n\u0002\b\u0002\n\u0002\u0010\t\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u0002\n\u0002\b\u0005\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0004\n\u0002\u0018\u0002\n\u0002\b\u0004\b\u0016\u0018\u0000 \u001b2\u00020\u0001:\u0002\u001b\u001cB\u0005¢\u0006\u0002\u0010\u0002J\u0012\u0010\b\u001a\u00020\t2\b\u0010\n\u001a\u0004\u0018\u00010\tH\u0001J\u0006\u0010\u000b\u001a\u00020\fJ\u0006\u0010\r\u001a\u00020\u0004J\u0012\u0010\u000e\u001a\u00020\t2\b\u0010\n\u001a\u0004\u0018\u00010\tH\u0014J\u0010\u0010\u000f\u001a\u00020\u00072\u0006\u0010\u0010\u001a\u00020\u0007H\u0002J\u000e\u0010\u0011\u001a\u00020\u00122\u0006\u0010\u0011\u001a\u00020\u0012J\u000e\u0010\u0013\u001a\u00020\u00142\u0006\u0010\u0013\u001a\u00020\u0014J\b\u0010\u0015\u001a\u00020\fH\u0014J%\u0010\u0016\u001a\u0002H\u0017\"\u0004\b\u0000\u0010\u00172\f\u0010\u0018\u001a\b\u0012\u0004\u0012\u0002H\u00170\u0019H\bø\u0001\u0000¢\u0006\u0002\u0010\u001aR\u000e\u0010\u0003\u001a\u00020\u0004X\u000e¢\u0006\u0002\n\u0000R\u0010\u0010\u0005\u001a\u0004\u0018\u00010\u0000X\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0006\u001a\u00020\u0007X\u000e¢\u0006\u0002\n\u0000\u0002\u0007\n\u0005\b20\u0001¨\u0006\u001d"}, d2 = {"Lokio/AsyncTimeout;", "Lokio/Timeout;", "()V", "inQueue", "", "next", "timeoutAt", "", "access$newTimeoutException", "Ljava/io/IOException;", "cause", "enter", "", "exit", "newTimeoutException", "remainingNanos", "now", "sink", "Lokio/Sink;", "source", "Lokio/Source;", "timedOut", "withTimeout", "T", "block", "Lkotlin/Function0;", "(Lkotlin/jvm/functions/Function0;)Ljava/lang/Object;", "Companion", "Watchdog", "okio"}, k = 1, mv = {1, 4, 0})
  /* compiled from: AsyncTimeout.kt */
  public class AsyncTimeout extends Timeout {
      public static final Companion Companion = new Companion((DefaultConstructorMarker) null);
      /* access modifiers changed from: private */
      public static final long IDLE_TIMEOUT_MILLIS;
      /* access modifiers changed from: private */
      public static final long IDLE_TIMEOUT_NANOS;
      private static final int TIMEOUT_WRITE_SIZE = 65536;
      /* access modifiers changed from: private */
      public static AsyncTimeout head;
      private boolean inQueue;
      /* access modifiers changed from: private */
      public AsyncTimeout next;
      /* access modifiers changed from: private */
      public long timeoutAt;
  
      @Metadata(bv = {1, 0, 3}, d1 = {"\u00002\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0010\t\n\u0002\b\u0002\n\u0002\u0010\b\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0010\u000b\n\u0002\b\u0002\n\u0002\u0010\u0002\n\u0002\b\u0003\b\u0003\u0018\u00002\u00020\u0001B\u0007\b\u0002¢\u0006\u0002\u0010\u0002J\u000f\u0010\n\u001a\u0004\u0018\u00010\tH\u0000¢\u0006\u0002\b\u000bJ\u0010\u0010\f\u001a\u00020\r2\u0006\u0010\u000e\u001a\u00020\tH\u0002J \u0010\u000f\u001a\u00020\u00102\u0006\u0010\u000e\u001a\u00020\t2\u0006\u0010\u0011\u001a\u00020\u00042\u0006\u0010\u0012\u001a\u00020\rH\u0002R\u000e\u0010\u0003\u001a\u00020\u0004X\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0005\u001a\u00020\u0004X\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0006\u001a\u00020\u0007XT¢\u0006\u0002\n\u0000R\u0010\u0010\b\u001a\u0004\u0018\u00010\tX\u000e¢\u0006\u0002\n\u0000¨\u0006\u0013"}, d2 = {"Lokio/AsyncTimeout$Companion;", "", "()V", "IDLE_TIMEOUT_MILLIS", "", "IDLE_TIMEOUT_NANOS", "TIMEOUT_WRITE_SIZE", "", "head", "Lokio/AsyncTimeout;", "awaitTimeout", "awaitTimeout$okio", "cancelScheduledTimeout", "", "node", "scheduleTimeout", "", "timeoutNanos", "hasDeadline", "okio"}, k = 1, mv = {1, 4, 0})
      /* compiled from: AsyncTimeout.kt */
      public static final class Companion {
          private Companion() {
          }
  
          public /* synthetic */ Companion(DefaultConstructorMarker $constructor_marker) {
              this();
          }
  
          /* access modifiers changed from: private */
          public final boolean cancelScheduledTimeout(AsyncTimeout node) {
              synchronized (AsyncTimeout.class) {
                  for (AsyncTimeout access$getHead$cp = AsyncTimeout.head; access$getHead$cp != null; access$getHead$cp = access$getHead$cp.next) {
                      if (access$getHead$cp.next == node) {
                          access$getHead$cp.next = node.next;
                          AsyncTimeout asyncTimeout = null;
                          node.next = null;
                          return false;
                      }
                  }
                  return true;
              }
          }
  
          /* access modifiers changed from: private */
          public final void scheduleTimeout(AsyncTimeout node, long timeoutNanos, boolean hasDeadline) {
              synchronized (AsyncTimeout.class) {
                  if (AsyncTimeout.head == null) {
                      AsyncTimeout.head = new AsyncTimeout();
                      new Watchdog().start();
                  }
                  long nanoTime = System.nanoTime();
                  if (timeoutNanos != 0 && hasDeadline) {
                      node.timeoutAt = Math.min(timeoutNanos, node.deadlineNanoTime() - nanoTime) + nanoTime;
                  } else if (timeoutNanos != 0) {
                      node.timeoutAt = nanoTime + timeoutNanos;
                  } else if (hasDeadline) {
                      node.timeoutAt = node.deadlineNanoTime();
                  } else {
                      throw new AssertionError();
                  }
                  long access$remainingNanos = node.remainingNanos(nanoTime);
                  AsyncTimeout access$getHead$cp = AsyncTimeout.head;
                  Intrinsics.checkNotNull(access$getHead$cp);
                  while (true) {
                      if (access$getHead$cp.next == null) {
                          break;
                      }
                      AsyncTimeout access$getNext$p = access$getHead$cp.next;
                      Intrinsics.checkNotNull(access$getNext$p);
                      if (access$remainingNanos < access$getNext$p.remainingNanos(nanoTime)) {
                          break;
                      }
                      AsyncTimeout access$getNext$p2 = access$getHead$cp.next;
                      Intrinsics.checkNotNull(access$getNext$p2);
                      access$getHead$cp = access$getNext$p2;
                  }
                  node.next = access$getHead$cp.next;
                  access$getHead$cp.next = node;
                  if (access$getHead$cp == AsyncTimeout.head) {
                      AsyncTimeout.class.notify();
                  }
                  Unit unit = Unit.INSTANCE;
              }
          }
  
          public final AsyncTimeout awaitTimeout$okio() throws InterruptedException {
              AsyncTimeout access$getHead$cp = AsyncTimeout.head;
              Intrinsics.checkNotNull(access$getHead$cp);
              AsyncTimeout access$getNext$p = access$getHead$cp.next;
              if (access$getNext$p == null) {
                  long nanoTime = System.nanoTime();
                  AsyncTimeout.class.wait(AsyncTimeout.IDLE_TIMEOUT_MILLIS);
                  AsyncTimeout access$getHead$cp2 = AsyncTimeout.head;
                  Intrinsics.checkNotNull(access$getHead$cp2);
                  if (access$getHead$cp2.next != null || System.nanoTime() - nanoTime < AsyncTimeout.IDLE_TIMEOUT_NANOS) {
                      return null;
                  }
                  return AsyncTimeout.head;
              }
              long access$remainingNanos = access$getNext$p.remainingNanos(System.nanoTime());
              if (access$remainingNanos > 0) {
                  long j = access$remainingNanos / 1000000;
                  AsyncTimeout.class.wait(j, (int) (access$remainingNanos - (1000000 * j)));
                  return null;
              }
              AsyncTimeout access$getHead$cp3 = AsyncTimeout.head;
              Intrinsics.checkNotNull(access$getHead$cp3);
              access$getHead$cp3.next = access$getNext$p.next;
              AsyncTimeout asyncTimeout = null;
              access$getNext$p.next = null;
              return access$getNext$p;
          }
      }
  
      @Metadata(bv = {1, 0, 3}, d1 = {"\u0000\u0012\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u0002\n\u0000\b\u0002\u0018\u00002\u00020\u0001B\u0007\b\u0000¢\u0006\u0002\u0010\u0002J\b\u0010\u0003\u001a\u00020\u0004H\u0016¨\u0006\u0005"}, d2 = {"Lokio/AsyncTimeout$Watchdog;", "Ljava/lang/Thread;", "()V", "run", "", "okio"}, k = 1, mv = {1, 4, 0})
      /* compiled from: AsyncTimeout.kt */
      private static final class Watchdog extends Thread {
          public Watchdog() {
              super("Okio Watchdog");
              setDaemon(true);
          }
  
          /* JADX WARNING: Code restructure failed: missing block: B:16:0x0026, code lost:
              if (r1 == null) goto L_0x0000;
           */
          /* JADX WARNING: Code restructure failed: missing block: B:17:0x0028, code lost:
              r1.timedOut();
           */
          /* JADX WARNING: Exception block dominator not found, dom blocks: [] */
          /* Code decompiled incorrectly, please refer to instructions dump. */
          public void run() {
              /*
                  r6 = this;
              L_0x0000:
                  r0 = 0
                  r1 = r0
                  okio.AsyncTimeout r1 = (okio.AsyncTimeout) r1     // Catch:{ InterruptedException -> 0x002f }
                  r1 = r0
                  java.lang.Class<okio.AsyncTimeout> r2 = okio.AsyncTimeout.class
                  r3 = 0
                  monitor-enter(r2)     // Catch:{ InterruptedException -> 0x002f }
                  r4 = 0
                  okio.AsyncTimeout$Companion r5 = okio.AsyncTimeout.Companion     // Catch:{ all -> 0x002c }
                  okio.AsyncTimeout r5 = r5.awaitTimeout$okio()     // Catch:{ all -> 0x002c }
                  r1 = r5
                  okio.AsyncTimeout r5 = okio.AsyncTimeout.head     // Catch:{ all -> 0x002c }
                  if (r1 != r5) goto L_0x0021
                  r5 = r0
                  okio.AsyncTimeout r5 = (okio.AsyncTimeout) r5     // Catch:{ all -> 0x002c }
                  okio.AsyncTimeout.head = r0     // Catch:{ all -> 0x002c }
                  monitor-exit(r2)     // Catch:{ InterruptedException -> 0x002f }
                  return
              L_0x0021:
                  kotlin.Unit r0 = kotlin.Unit.INSTANCE     // Catch:{ all -> 0x002c }
                  monitor-exit(r2)     // Catch:{ InterruptedException -> 0x002f }
                  if (r1 == 0) goto L_0x0030
                  r1.timedOut()     // Catch:{ InterruptedException -> 0x002f }
                  goto L_0x0030
              L_0x002c:
                  r0 = move-exception
                  monitor-exit(r2)     // Catch:{ InterruptedException -> 0x002f }
                  throw r0     // Catch:{ InterruptedException -> 0x002f }
              L_0x002f:
                  r0 = move-exception
              L_0x0030:
                  goto L_0x0000
              */
              throw new UnsupportedOperationException("Method not decompiled: okio.AsyncTimeout.Watchdog.run():void");
          }
      }
  
      static {
          long millis = TimeUnit.SECONDS.toMillis(60);
          IDLE_TIMEOUT_MILLIS = millis;
          IDLE_TIMEOUT_NANOS = TimeUnit.MILLISECONDS.toNanos(millis);
      }
  
      /* access modifiers changed from: private */
      public final long remainingNanos(long now) {
          return this.timeoutAt - now;
      }
  
      public final IOException access$newTimeoutException(IOException cause) {
          return newTimeoutException(cause);
      }
  
      public final void enter() {
          if (!this.inQueue) {
              long timeoutNanos = timeoutNanos();
              boolean hasDeadline = hasDeadline();
              if (timeoutNanos != 0 || hasDeadline) {
                  this.inQueue = true;
                  Companion.scheduleTimeout(this, timeoutNanos, hasDeadline);
                  return;
              }
              return;
          }
          throw new IllegalStateException("Unbalanced enter/exit".toString());
      }
  
      public final boolean exit() {
          if (!this.inQueue) {
              return false;
          }
          this.inQueue = false;
          return Companion.cancelScheduledTimeout(this);
      }
  
      /* access modifiers changed from: protected */
      public IOException newTimeoutException(IOException cause) {
          InterruptedIOException interruptedIOException = new InterruptedIOException("timeout");
          if (cause != null) {
              interruptedIOException.initCause(cause);
          }
          return interruptedIOException;
      }
  
      public final Sink sink(Sink sink) {
          Intrinsics.checkNotNullParameter(sink, "sink");
          return new AsyncTimeout$sink$1(this, sink);
      }
  
      public final Source source(Source source) {
          Intrinsics.checkNotNullParameter(source, "source");
          return new AsyncTimeout$source$1(this, source);
      }
  
      /* access modifiers changed from: protected */
      public void timedOut() {
      }
  
      public final <T> T withTimeout(Function0<? extends T> block) {
          Intrinsics.checkNotNullParameter(block, "block");
          enter();
          try {
              T invoke = block.invoke();
              InlineMarker.finallyStart(1);
              if (!exit()) {
                  InlineMarker.finallyEnd(1);
                  return invoke;
              }
              throw access$newTimeoutException((IOException) null);
          } catch (IOException e) {
              throw (!exit() ? e : access$newTimeoutException(e));
          } catch (Throwable th) {
              InlineMarker.finallyStart(1);
              if (!exit() || 0 == 0) {
                  InlineMarker.finallyEnd(1);
                  throw th;
              }
              throw access$newTimeoutException((IOException) null);
          }
      }
  }
  