package j;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/* JADX INFO: compiled from: AsyncTimeout.kt */
/* JADX INFO: loaded from: classes.dex */
public class b extends c0 {
    public static final a Companion = new a(null);
    private static final long IDLE_TIMEOUT_MILLIS;
    private static final long IDLE_TIMEOUT_NANOS;
    private static final int TIMEOUT_WRITE_SIZE = 65536;
    private static b head;
    private boolean inQueue;
    private b next;
    private long timeoutAt;

    /* JADX INFO: compiled from: AsyncTimeout.kt */
    public static final class a {
        public a(g.h0.c.g gVar) {
        }

        public final b a() throws InterruptedException {
            b bVar = b.head;
            g.h0.c.l.c(bVar);
            b bVar2 = bVar.next;
            if (bVar2 == null) {
                long jNanoTime = System.nanoTime();
                b.class.wait(b.IDLE_TIMEOUT_MILLIS);
                b bVar3 = b.head;
                g.h0.c.l.c(bVar3);
                if (bVar3.next != null || System.nanoTime() - jNanoTime < b.IDLE_TIMEOUT_NANOS) {
                    return null;
                }
                return b.head;
            }
            long jRemainingNanos = bVar2.remainingNanos(System.nanoTime());
            if (jRemainingNanos > 0) {
                long j2 = jRemainingNanos / 1000000;
                b.class.wait(j2, (int) (jRemainingNanos - (1000000 * j2)));
                return null;
            }
            b bVar4 = b.head;
            g.h0.c.l.c(bVar4);
            bVar4.next = bVar2.next;
            bVar2.next = null;
            return bVar2;
        }
    }

    /* JADX INFO: renamed from: j.b$b, reason: collision with other inner class name */
    /* JADX INFO: compiled from: AsyncTimeout.kt */
    public static final class C0086b extends Thread {
        public C0086b() {
            super("Okio Watchdog");
            setDaemon(true);
        }

        @Override // java.lang.Thread, java.lang.Runnable
        public void run() {
            b bVarA;
            while (true) {
                try {
                    synchronized (b.class) {
                        bVarA = b.Companion.a();
                        if (bVarA == b.head) {
                            b.head = null;
                            return;
                        }
                    }
                    if (bVarA != null) {
                        bVarA.timedOut();
                    }
                } catch (InterruptedException unused) {
                }
            }
        }
    }

    /* JADX INFO: compiled from: AsyncTimeout.kt */
    public static final class c implements z {

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        public final /* synthetic */ z f4597e;

        public c(z zVar) {
            this.f4597e = zVar;
        }

        @Override // j.z, java.io.Closeable, java.lang.AutoCloseable, java.nio.channels.Channel
        public void close() throws IOException {
            b bVar = b.this;
            bVar.enter();
            try {
                this.f4597e.close();
                if (bVar.exit()) {
                    throw bVar.access$newTimeoutException(null);
                }
            } catch (IOException e2) {
                if (!bVar.exit()) {
                    throw e2;
                }
                throw bVar.access$newTimeoutException(e2);
            } finally {
                bVar.exit();
            }
        }

        @Override // j.z, java.io.Flushable
        public void flush() throws IOException {
            b bVar = b.this;
            bVar.enter();
            try {
                this.f4597e.flush();
                if (bVar.exit()) {
                    throw bVar.access$newTimeoutException(null);
                }
            } catch (IOException e2) {
                if (!bVar.exit()) {
                    throw e2;
                }
                throw bVar.access$newTimeoutException(e2);
            } finally {
                bVar.exit();
            }
        }

        @Override // j.z
        public c0 timeout() {
            return b.this;
        }

        public String toString() {
            StringBuilder sbB = d.b.a.a.a.B("AsyncTimeout.sink(");
            sbB.append(this.f4597e);
            sbB.append(')');
            return sbB.toString();
        }

        @Override // j.z
        public void write(j.d dVar, long j2) throws IOException {
            g.h0.c.l.f(dVar, "source");
            f.a.y.a.z(dVar.f4601e, 0L, j2);
            while (true) {
                long j3 = 0;
                if (j2 <= 0) {
                    return;
                }
                w wVar = dVar.f4600b;
                g.h0.c.l.c(wVar);
                while (true) {
                    if (j3 >= b.TIMEOUT_WRITE_SIZE) {
                        break;
                    }
                    j3 += (long) (wVar.f4650c - wVar.f4649b);
                    if (j3 >= j2) {
                        j3 = j2;
                        break;
                    } else {
                        wVar = wVar.f4653f;
                        g.h0.c.l.c(wVar);
                    }
                }
                b bVar = b.this;
                bVar.enter();
                try {
                    this.f4597e.write(dVar, j3);
                    if (bVar.exit()) {
                        throw bVar.access$newTimeoutException(null);
                    }
                    j2 -= j3;
                } catch (IOException e2) {
                    if (!bVar.exit()) {
                        throw e2;
                    }
                    throw bVar.access$newTimeoutException(e2);
                } finally {
                    bVar.exit();
                }
            }
        }
    }

    /* JADX INFO: compiled from: AsyncTimeout.kt */
    public static final class d implements b0 {

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        public final /* synthetic */ b0 f4599e;

        public d(b0 b0Var) {
            this.f4599e = b0Var;
        }

        @Override // j.b0, java.io.Closeable, java.lang.AutoCloseable
        public void close() throws IOException {
            b bVar = b.this;
            bVar.enter();
            try {
                this.f4599e.close();
                if (bVar.exit()) {
                    throw bVar.access$newTimeoutException(null);
                }
            } catch (IOException e2) {
                if (!bVar.exit()) {
                    throw e2;
                }
                throw bVar.access$newTimeoutException(e2);
            } finally {
                bVar.exit();
            }
        }

        @Override // j.b0
        public long read(j.d dVar, long j2) throws IOException {
            g.h0.c.l.f(dVar, "sink");
            b bVar = b.this;
            bVar.enter();
            try {
                long j3 = this.f4599e.read(dVar, j2);
                if (bVar.exit()) {
                    throw bVar.access$newTimeoutException(null);
                }
                return j3;
            } catch (IOException e2) {
                if (bVar.exit()) {
                    throw bVar.access$newTimeoutException(e2);
                }
                throw e2;
            } finally {
                bVar.exit();
            }
        }

        @Override // j.b0
        public c0 timeout() {
            return b.this;
        }

        public String toString() {
            StringBuilder sbB = d.b.a.a.a.B("AsyncTimeout.source(");
            sbB.append(this.f4599e);
            sbB.append(')');
            return sbB.toString();
        }
    }

    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 j2) {
        return this.timeoutAt - j2;
    }

    public final IOException access$newTimeoutException(IOException iOException) {
        return newTimeoutException(iOException);
    }

    public final void enter() {
        if (!(!this.inQueue)) {
            throw new IllegalStateException("Unbalanced enter/exit".toString());
        }
        long jTimeoutNanos = timeoutNanos();
        boolean zHasDeadline = hasDeadline();
        if (jTimeoutNanos != 0 || zHasDeadline) {
            this.inQueue = true;
            Objects.requireNonNull(Companion);
            synchronized (b.class) {
                if (head == null) {
                    head = new b();
                    new C0086b().start();
                }
                long jNanoTime = System.nanoTime();
                if (jTimeoutNanos != 0 && zHasDeadline) {
                    this.timeoutAt = Math.min(jTimeoutNanos, deadlineNanoTime() - jNanoTime) + jNanoTime;
                } else if (jTimeoutNanos != 0) {
                    this.timeoutAt = jTimeoutNanos + jNanoTime;
                } else {
                    if (!zHasDeadline) {
                        throw new AssertionError();
                    }
                    this.timeoutAt = deadlineNanoTime();
                }
                long jRemainingNanos = remainingNanos(jNanoTime);
                b bVar = head;
                g.h0.c.l.c(bVar);
                while (bVar.next != null) {
                    b bVar2 = bVar.next;
                    g.h0.c.l.c(bVar2);
                    if (jRemainingNanos < bVar2.remainingNanos(jNanoTime)) {
                        break;
                    }
                    bVar = bVar.next;
                    g.h0.c.l.c(bVar);
                }
                this.next = bVar.next;
                bVar.next = this;
                if (bVar == head) {
                    b.class.notify();
                }
            }
        }
    }

    /* JADX WARN: Code restructure failed: missing block: B:11:0x001c, code lost:
    
        r2.next = r4.next;
        r4.next = null;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public final boolean exit() {
        /*
            r4 = this;
            boolean r0 = r4.inQueue
            r1 = 0
            if (r0 != 0) goto L6
            return r1
        L6:
            r4.inQueue = r1
            j.b$a r0 = j.b.Companion
            java.util.Objects.requireNonNull(r0)
            java.lang.Class<j.b> r0 = j.b.class
            monitor-enter(r0)
            j.b r2 = access$getHead$cp()     // Catch: java.lang.Throwable -> L31
        L14:
            if (r2 == 0) goto L2e
            j.b r3 = access$getNext$p(r2)     // Catch: java.lang.Throwable -> L31
            if (r3 != r4) goto L29
            j.b r3 = access$getNext$p(r4)     // Catch: java.lang.Throwable -> L31
            access$setNext$p(r2, r3)     // Catch: java.lang.Throwable -> L31
            r2 = 0
            access$setNext$p(r4, r2)     // Catch: java.lang.Throwable -> L31
            monitor-exit(r0)
            goto L30
        L29:
            j.b r2 = access$getNext$p(r2)     // Catch: java.lang.Throwable -> L31
            goto L14
        L2e:
            r1 = 1
            monitor-exit(r0)
        L30:
            return r1
        L31:
            r1 = move-exception
            monitor-exit(r0)
            throw r1
        */
        throw new UnsupportedOperationException("Method not decompiled: j.b.exit():boolean");
    }

    public IOException newTimeoutException(IOException iOException) {
        InterruptedIOException interruptedIOException = new InterruptedIOException("timeout");
        if (iOException != null) {
            interruptedIOException.initCause(iOException);
        }
        return interruptedIOException;
    }

    public final z sink(z zVar) {
        g.h0.c.l.f(zVar, "sink");
        return new c(zVar);
    }

    public final b0 source(b0 b0Var) {
        g.h0.c.l.f(b0Var, "source");
        return new d(b0Var);
    }

    public void timedOut() {
    }

    public final <T> T withTimeout(g.h0.b.a<? extends T> aVar) throws IOException {
        g.h0.c.l.f(aVar, "block");
        enter();
        try {
            T tInvoke = aVar.invoke();
            if (exit()) {
                throw access$newTimeoutException(null);
            }
            return tInvoke;
        } catch (IOException e2) {
            if (exit()) {
                throw access$newTimeoutException(e2);
            }
            throw e2;
        } finally {
            exit();
        }
    }
}
