package l;

import j.h0;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.concurrent.TimeUnit;

/* JADX INFO: compiled from: AsyncTimeout.kt */
/* JADX INFO: loaded from: classes.dex */
public class d extends b0 {
    public static final a Companion = new a(null);
    private static final long IDLE_TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(60);
    private static final long IDLE_TIMEOUT_NANOS = TimeUnit.MILLISECONDS.toNanos(IDLE_TIMEOUT_MILLIS);
    private static final int TIMEOUT_WRITE_SIZE = 65536;
    private static d head;
    private boolean inQueue;
    private d next;
    private long timeoutAt;

    /* JADX INFO: compiled from: AsyncTimeout.kt */
    public static final class a {
        private a() {
        }

        /* JADX INFO: Access modifiers changed from: private */
        public final boolean d(d dVar) {
            synchronized (d.class) {
                for (d dVar2 = d.head; dVar2 != null; dVar2 = dVar2.next) {
                    if (dVar2.next == dVar) {
                        dVar2.next = dVar.next;
                        dVar.next = null;
                        return false;
                    }
                }
                return true;
            }
        }

        /* JADX INFO: Access modifiers changed from: private */
        public final void e(d dVar, long j2, boolean z) {
            synchronized (d.class) {
                if (d.head == null) {
                    d.head = new d();
                    new b().start();
                }
                long jNanoTime = System.nanoTime();
                if (j2 != 0 && z) {
                    dVar.timeoutAt = Math.min(j2, dVar.deadlineNanoTime() - jNanoTime) + jNanoTime;
                } else if (j2 != 0) {
                    dVar.timeoutAt = j2 + jNanoTime;
                } else {
                    if (!z) {
                        throw new AssertionError();
                    }
                    dVar.timeoutAt = dVar.deadlineNanoTime();
                }
                long jRemainingNanos = dVar.remainingNanos(jNanoTime);
                d dVar2 = d.head;
                if (dVar2 == null) {
                    j.p0.d.r.p();
                    throw null;
                }
                while (dVar2.next != null) {
                    d dVar3 = dVar2.next;
                    if (dVar3 == null) {
                        j.p0.d.r.p();
                        throw null;
                    }
                    if (jRemainingNanos < dVar3.remainingNanos(jNanoTime)) {
                        break;
                    }
                    dVar2 = dVar2.next;
                    if (dVar2 == null) {
                        j.p0.d.r.p();
                        throw null;
                    }
                }
                dVar.next = dVar2.next;
                dVar2.next = dVar;
                if (dVar2 == d.head) {
                    d.class.notify();
                }
                h0 h0Var = h0.a;
            }
        }

        public final d c() throws InterruptedException {
            d dVar = d.head;
            if (dVar == null) {
                j.p0.d.r.p();
                throw null;
            }
            d dVar2 = dVar.next;
            if (dVar2 == null) {
                long jNanoTime = System.nanoTime();
                d.class.wait(d.IDLE_TIMEOUT_MILLIS);
                d dVar3 = d.head;
                if (dVar3 == null) {
                    j.p0.d.r.p();
                    throw null;
                }
                if (dVar3.next != null || System.nanoTime() - jNanoTime < d.IDLE_TIMEOUT_NANOS) {
                    return null;
                }
                return d.head;
            }
            long jRemainingNanos = dVar2.remainingNanos(System.nanoTime());
            if (jRemainingNanos > 0) {
                long j2 = jRemainingNanos / 1000000;
                d.class.wait(j2, (int) (jRemainingNanos - (1000000 * j2)));
                return null;
            }
            d dVar4 = d.head;
            if (dVar4 == null) {
                j.p0.d.r.p();
                throw null;
            }
            dVar4.next = dVar2.next;
            dVar2.next = null;
            return dVar2;
        }

        public /* synthetic */ a(j.p0.d.j jVar) {
            this();
        }
    }

    /* JADX INFO: compiled from: AsyncTimeout.kt */
    private static final class b extends Thread {
        public b() {
            super("Okio Watchdog");
            setDaemon(true);
        }

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

    /* JADX INFO: compiled from: AsyncTimeout.kt */
    public static final class c implements y {
        final /* synthetic */ y d;

        c(y yVar) {
            this.d = yVar;
        }

        @Override // l.y
        /* JADX INFO: renamed from: c, reason: merged with bridge method [inline-methods] */
        public d timeout() {
            return d.this;
        }

        @Override // l.y, java.io.Closeable, java.lang.AutoCloseable, java.nio.channels.Channel
        public void close() throws IOException {
            d dVar = d.this;
            dVar.enter();
            try {
                this.d.close();
                h0 h0Var = h0.a;
                if (dVar.exit()) {
                    throw dVar.access$newTimeoutException(null);
                }
            } catch (IOException e) {
                if (!dVar.exit()) {
                    throw e;
                }
                throw dVar.access$newTimeoutException(e);
            } finally {
                dVar.exit();
            }
        }

        @Override // l.y, java.io.Flushable
        public void flush() throws IOException {
            d dVar = d.this;
            dVar.enter();
            try {
                this.d.flush();
                h0 h0Var = h0.a;
                if (dVar.exit()) {
                    throw dVar.access$newTimeoutException(null);
                }
            } catch (IOException e) {
                if (!dVar.exit()) {
                    throw e;
                }
                throw dVar.access$newTimeoutException(e);
            } finally {
                dVar.exit();
            }
        }

        public String toString() {
            return "AsyncTimeout.sink(" + this.d + ')';
        }

        @Override // l.y
        public void write(f fVar, long j2) throws IOException {
            j.p0.d.r.g(fVar, "source");
            l.c.b(fVar.K(), 0L, j2);
            while (true) {
                long j3 = 0;
                if (j2 > 0) {
                    v vVar = fVar.b;
                    if (vVar == null) {
                        j.p0.d.r.p();
                        throw null;
                    }
                    do {
                        if (j3 < d.TIMEOUT_WRITE_SIZE) {
                            j3 += (long) (vVar.c - vVar.b);
                            if (j3 >= j2) {
                                j3 = j2;
                            } else {
                                vVar = vVar.f;
                            }
                        }
                        d dVar = d.this;
                        dVar.enter();
                        try {
                            this.d.write(fVar, j3);
                            h0 h0Var = h0.a;
                            if (dVar.exit()) {
                                throw dVar.access$newTimeoutException(null);
                            }
                            j2 -= j3;
                        } catch (IOException e) {
                            if (!dVar.exit()) {
                                throw e;
                            }
                            throw dVar.access$newTimeoutException(e);
                        } finally {
                            dVar.exit();
                        }
                    } while (vVar != null);
                    j.p0.d.r.p();
                    throw null;
                }
                return;
            }
        }
    }

    /* JADX INFO: renamed from: l.d$d, reason: collision with other inner class name */
    /* JADX INFO: compiled from: AsyncTimeout.kt */
    public static final class C0094d implements a0 {
        final /* synthetic */ a0 d;

        C0094d(a0 a0Var) {
            this.d = a0Var;
        }

        @Override // l.a0
        /* JADX INFO: renamed from: c, reason: merged with bridge method [inline-methods] */
        public d timeout() {
            return d.this;
        }

        @Override // l.a0, java.io.Closeable, java.lang.AutoCloseable
        public void close() throws IOException {
            d dVar = d.this;
            dVar.enter();
            try {
                this.d.close();
                h0 h0Var = h0.a;
                if (dVar.exit()) {
                    throw dVar.access$newTimeoutException(null);
                }
            } catch (IOException e) {
                if (!dVar.exit()) {
                    throw e;
                }
                throw dVar.access$newTimeoutException(e);
            } finally {
                dVar.exit();
            }
        }

        @Override // l.a0
        public long read(f fVar, long j2) throws IOException {
            j.p0.d.r.g(fVar, "sink");
            d dVar = d.this;
            dVar.enter();
            try {
                long j3 = this.d.read(fVar, j2);
                if (dVar.exit()) {
                    throw dVar.access$newTimeoutException(null);
                }
                return j3;
            } catch (IOException e) {
                if (dVar.exit()) {
                    throw dVar.access$newTimeoutException(e);
                }
                throw e;
            } finally {
                dVar.exit();
            }
        }

        public String toString() {
            return "AsyncTimeout.source(" + this.d + ')';
        }
    }

    /* 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;
            Companion.e(this, jTimeoutNanos, zHasDeadline);
        }
    }

    public final boolean exit() {
        if (!this.inQueue) {
            return false;
        }
        this.inQueue = false;
        return Companion.d(this);
    }

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

    public final y sink(y yVar) {
        j.p0.d.r.g(yVar, "sink");
        return new c(yVar);
    }

    public final a0 source(a0 a0Var) {
        j.p0.d.r.g(a0Var, "source");
        return new C0094d(a0Var);
    }

    protected void timedOut() {
    }

    public final <T> T withTimeout(j.p0.c.a<? extends T> aVar) throws IOException {
        j.p0.d.r.g(aVar, "block");
        enter();
        try {
            try {
                T tInvoke = aVar.invoke();
                j.p0.d.p.b(1);
                if (exit()) {
                    throw access$newTimeoutException(null);
                }
                j.p0.d.p.a(1);
                return tInvoke;
            } catch (IOException e) {
                if (exit()) {
                    throw access$newTimeoutException(e);
                }
                throw e;
            }
        } catch (Throwable th) {
            j.p0.d.p.b(1);
            exit();
            j.p0.d.p.a(1);
            throw th;
        }
    }
}
