package d;

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 j, boolean z) {
            synchronized (d.class) {
                if (d.head == null) {
                    d.head = new d();
                    new b().start();
                }
                long jNanoTime = System.nanoTime();
                if (j != 0 && z) {
                    dVar.timeoutAt = Math.min(j, dVar.deadlineNanoTime() - jNanoTime) + jNanoTime;
                } else if (j != 0) {
                    dVar.timeoutAt = j + jNanoTime;
                } else {
                    if (!z) {
                        throw new AssertionError();
                    }
                    dVar.timeoutAt = dVar.deadlineNanoTime();
                }
                long jRemainingNanos = dVar.remainingNanos(jNanoTime);
                d dVar2 = d.head;
                if (dVar2 == null) {
                    c.q.d.j.g();
                    throw null;
                }
                while (dVar2.next != null) {
                    d dVar3 = dVar2.next;
                    if (dVar3 == null) {
                        c.q.d.j.g();
                        throw null;
                    }
                    if (jRemainingNanos < dVar3.remainingNanos(jNanoTime)) {
                        break;
                    }
                    dVar2 = dVar2.next;
                    if (dVar2 == null) {
                        c.q.d.j.g();
                        throw null;
                    }
                }
                dVar.next = dVar2.next;
                dVar2.next = dVar;
                if (dVar2 == d.head) {
                    d.class.notify();
                }
                c.j jVar = c.j.f1417a;
            }
        }

        public final d c() throws InterruptedException {
            d dVar = d.head;
            if (dVar == null) {
                c.q.d.j.g();
                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) {
                    c.q.d.j.g();
                    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 j = jRemainingNanos / 1000000;
                d.class.wait(j, (int) (jRemainingNanos - (1000000 * j)));
                return null;
            }
            d dVar4 = d.head;
            if (dVar4 == null) {
                c.q.d.j.g();
                throw null;
            }
            dVar4.next = dVar2.next;
            dVar2.next = null;
            return dVar2;
        }

        public /* synthetic */ a(c.q.d.g gVar) {
            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;
                        }
                        c.j jVar = c.j.f1417a;
                    }
                    if (dVarC != null) {
                        dVarC.timedOut();
                    }
                } catch (InterruptedException unused) {
                }
            }
        }
    }

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

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        final /* synthetic */ y f1559c;

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

        @Override // d.y, java.io.Closeable, java.lang.AutoCloseable
        public void close() throws IOException {
            d.this.enter();
            try {
                try {
                    this.f1559c.close();
                    d.this.exit$jvm(true);
                } catch (IOException e2) {
                    throw d.this.exit$jvm(e2);
                }
            } catch (Throwable th) {
                d.this.exit$jvm(false);
                throw th;
            }
        }

        @Override // d.y, java.io.Flushable
        public void flush() throws IOException {
            d.this.enter();
            try {
                try {
                    this.f1559c.flush();
                    d.this.exit$jvm(true);
                } catch (IOException e2) {
                    throw d.this.exit$jvm(e2);
                }
            } catch (Throwable th) {
                d.this.exit$jvm(false);
                throw th;
            }
        }

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

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

        @Override // d.y
        public void write(f fVar, long j) throws IOException {
            c.q.d.j.c(fVar, "source");
            d.c.b(fVar.b0(), 0L, j);
            while (true) {
                long j2 = 0;
                if (j > 0) {
                    v vVar = fVar.f1563b;
                    if (vVar == null) {
                        c.q.d.j.g();
                        throw null;
                    }
                    do {
                        if (j2 < d.TIMEOUT_WRITE_SIZE) {
                            j2 += (long) (vVar.f1608c - vVar.f1607b);
                            if (j2 >= j) {
                                j2 = j;
                            } else {
                                vVar = vVar.f1611f;
                            }
                        }
                        d.this.enter();
                        try {
                            try {
                                this.f1559c.write(fVar, j2);
                                j -= j2;
                                d.this.exit$jvm(true);
                            } catch (IOException e2) {
                                throw d.this.exit$jvm(e2);
                            }
                        } catch (Throwable th) {
                            d.this.exit$jvm(false);
                            throw th;
                        }
                    } while (vVar != null);
                    c.q.d.j.g();
                    throw null;
                }
                return;
            }
        }
    }

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

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        final /* synthetic */ a0 f1561c;

        C0036d(a0 a0Var) {
            this.f1561c = a0Var;
        }

        @Override // d.a0, java.io.Closeable, java.lang.AutoCloseable
        public void close() throws IOException {
            d.this.enter();
            try {
                try {
                    this.f1561c.close();
                    d.this.exit$jvm(true);
                } catch (IOException e2) {
                    throw d.this.exit$jvm(e2);
                }
            } catch (Throwable th) {
                d.this.exit$jvm(false);
                throw th;
            }
        }

        @Override // d.a0
        public long read(f fVar, long j) throws IOException {
            c.q.d.j.c(fVar, "sink");
            d.this.enter();
            try {
                try {
                    long j2 = this.f1561c.read(fVar, j);
                    d.this.exit$jvm(true);
                    return j2;
                } catch (IOException e2) {
                    throw d.this.exit$jvm(e2);
                }
            } catch (Throwable th) {
                d.this.exit$jvm(false);
                throw th;
            }
        }

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

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

    /* JADX INFO: Access modifiers changed from: private */
    public final long remainingNanos(long j) {
        return this.timeoutAt - j;
    }

    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);
    }

    public final void exit$jvm(boolean z) throws IOException {
        if (exit() && z) {
            throw newTimeoutException(null);
        }
    }

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

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

    public final a0 source(a0 a0Var) {
        c.q.d.j.c(a0Var, "source");
        return new C0036d(a0Var);
    }

    protected void timedOut() {
    }

    public final IOException exit$jvm(IOException iOException) {
        c.q.d.j.c(iOException, "cause");
        return !exit() ? iOException : newTimeoutException(iOException);
    }
}
