package Nm6q_10;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.concurrent.TimeUnit;
/* compiled from: AsyncTimeout.kt */
/* loaded from: classes.dex */
public class Mqv2r_3 extends KVtv3R_25 {
    public static Mqv2r_3 QxJ9d_1;

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public Mqv2r_3 f338KQgRr5e_0;
    public long PGPF6hQ_2;

    /* renamed from: QxJ9d_1  reason: collision with other field name */
    public boolean f339QxJ9d_1;
    public static final KQgRr5e_0 KQgRr5e_0 = new KQgRr5e_0(null);

    /* renamed from: Mqv2r_3  reason: collision with root package name */
    public static final long f3105Mqv2r_3 = TimeUnit.SECONDS.toMillis(60);

    /* renamed from: X6FRs_4  reason: collision with root package name */
    public static final long f3106X6FRs_4 = TimeUnit.MILLISECONDS.toNanos(f3105Mqv2r_3);

    /* compiled from: AsyncTimeout.kt */
    /* loaded from: classes.dex */
    public static final class KQgRr5e_0 {
        public KQgRr5e_0() {
        }

        public /* synthetic */ KQgRr5e_0(FSaI4_7.Xgtqvk_8.QxJ9d_1.Mqv2r_3 mqv2r_3) {
            this();
        }

        public final void KQgRr5e_0(Mqv2r_3 mqv2r_3, long j, boolean z) {
            synchronized (Mqv2r_3.class) {
                if (Mqv2r_3.QxJ9d_1 == null) {
                    Mqv2r_3.QxJ9d_1 = new Mqv2r_3();
                    new QxJ9d_1().start();
                }
                long nanoTime = System.nanoTime();
                if (j != 0 && z) {
                    mqv2r_3.PGPF6hQ_2 = Math.min(j, mqv2r_3.KQgRr5e_0() - nanoTime) + nanoTime;
                } else if (j != 0) {
                    mqv2r_3.PGPF6hQ_2 = j + nanoTime;
                } else if (z) {
                    mqv2r_3.PGPF6hQ_2 = mqv2r_3.KQgRr5e_0();
                } else {
                    throw new AssertionError();
                }
                long KQgRr5e_0 = mqv2r_3.KQgRr5e_0(nanoTime);
                Mqv2r_3 mqv2r_32 = Mqv2r_3.QxJ9d_1;
                if (mqv2r_32 != null) {
                    while (mqv2r_32.f338KQgRr5e_0 != null) {
                        Mqv2r_3 mqv2r_33 = mqv2r_32.f338KQgRr5e_0;
                        if (mqv2r_33 != null) {
                            if (KQgRr5e_0 < mqv2r_33.KQgRr5e_0(nanoTime)) {
                                break;
                            }
                            mqv2r_32 = mqv2r_32.f338KQgRr5e_0;
                            if (mqv2r_32 == null) {
                                FSaI4_7.Xgtqvk_8.QxJ9d_1.IFxWkV_5.KQgRr5e_0();
                                throw null;
                            }
                        } else {
                            FSaI4_7.Xgtqvk_8.QxJ9d_1.IFxWkV_5.KQgRr5e_0();
                            throw null;
                        }
                    }
                    mqv2r_3.f338KQgRr5e_0 = mqv2r_32.f338KQgRr5e_0;
                    mqv2r_32.f338KQgRr5e_0 = mqv2r_3;
                    if (mqv2r_32 == Mqv2r_3.QxJ9d_1) {
                        Mqv2r_3.class.notify();
                    }
                    FSaI4_7.X6FRs_4 x6FRs_4 = FSaI4_7.X6FRs_4.KQgRr5e_0;
                } else {
                    FSaI4_7.Xgtqvk_8.QxJ9d_1.IFxWkV_5.KQgRr5e_0();
                    throw null;
                }
            }
        }

        public final boolean KQgRr5e_0(Mqv2r_3 mqv2r_3) {
            synchronized (Mqv2r_3.class) {
                for (Mqv2r_3 mqv2r_32 = Mqv2r_3.QxJ9d_1; mqv2r_32 != null; mqv2r_32 = mqv2r_32.f338KQgRr5e_0) {
                    if (mqv2r_32.f338KQgRr5e_0 == mqv2r_3) {
                        mqv2r_32.f338KQgRr5e_0 = mqv2r_3.f338KQgRr5e_0;
                        mqv2r_3.f338KQgRr5e_0 = null;
                        return false;
                    }
                }
                return true;
            }
        }

        public final Mqv2r_3 KQgRr5e_0() throws InterruptedException {
            Mqv2r_3 mqv2r_3 = Mqv2r_3.QxJ9d_1;
            if (mqv2r_3 != null) {
                Mqv2r_3 mqv2r_32 = mqv2r_3.f338KQgRr5e_0;
                if (mqv2r_32 != null) {
                    long KQgRr5e_0 = mqv2r_32.KQgRr5e_0(System.nanoTime());
                    if (KQgRr5e_0 <= 0) {
                        Mqv2r_3 mqv2r_33 = Mqv2r_3.QxJ9d_1;
                        if (mqv2r_33 != null) {
                            mqv2r_33.f338KQgRr5e_0 = mqv2r_32.f338KQgRr5e_0;
                            mqv2r_32.f338KQgRr5e_0 = null;
                            return mqv2r_32;
                        }
                        FSaI4_7.Xgtqvk_8.QxJ9d_1.IFxWkV_5.KQgRr5e_0();
                        throw null;
                    }
                    long j = KQgRr5e_0 / 1000000;
                    Mqv2r_3.class.wait(j, (int) (KQgRr5e_0 - (1000000 * j)));
                    return null;
                }
                long nanoTime = System.nanoTime();
                Mqv2r_3.class.wait(Mqv2r_3.f3105Mqv2r_3);
                Mqv2r_3 mqv2r_34 = Mqv2r_3.QxJ9d_1;
                if (mqv2r_34 != null) {
                    if (mqv2r_34.f338KQgRr5e_0 != null || System.nanoTime() - nanoTime < Mqv2r_3.f3106X6FRs_4) {
                        return null;
                    }
                    return Mqv2r_3.QxJ9d_1;
                }
                FSaI4_7.Xgtqvk_8.QxJ9d_1.IFxWkV_5.KQgRr5e_0();
                throw null;
            }
            FSaI4_7.Xgtqvk_8.QxJ9d_1.IFxWkV_5.KQgRr5e_0();
            throw null;
        }
    }

    /* compiled from: AsyncTimeout.kt */
    /* renamed from: Nm6q_10.Mqv2r_3$Mqv2r_3  reason: collision with other inner class name */
    /* loaded from: classes.dex */
    public static final class C0032Mqv2r_3 implements G83uNX_24 {
        public final /* synthetic */ G83uNX_24 KQgRr5e_0;

        public C0032Mqv2r_3(G83uNX_24 g83uNX_24) {
            this.KQgRr5e_0 = g83uNX_24;
        }

        @Override // Nm6q_10.G83uNX_24, java.io.Closeable, java.lang.AutoCloseable, java.nio.channels.Channel
        public void close() {
            Mqv2r_3.this.QxJ9d_1();
            try {
                try {
                    this.KQgRr5e_0.close();
                    Mqv2r_3.this.KQgRr5e_0(true);
                } catch (IOException e) {
                    throw Mqv2r_3.this.KQgRr5e_0(e);
                }
            } catch (Throwable th) {
                Mqv2r_3.this.KQgRr5e_0(false);
                throw th;
            }
        }

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

        @Override // Nm6q_10.G83uNX_24
        public long KQgRr5e_0(X6FRs_4 x6FRs_4, long j) {
            FSaI4_7.Xgtqvk_8.QxJ9d_1.IFxWkV_5.QxJ9d_1(x6FRs_4, "sink");
            Mqv2r_3.this.QxJ9d_1();
            try {
                try {
                    long KQgRr5e_0 = this.KQgRr5e_0.KQgRr5e_0(x6FRs_4, j);
                    Mqv2r_3.this.KQgRr5e_0(true);
                    return KQgRr5e_0;
                } catch (IOException e) {
                    throw Mqv2r_3.this.KQgRr5e_0(e);
                }
            } catch (Throwable th) {
                Mqv2r_3.this.KQgRr5e_0(false);
                throw th;
            }
        }

        @Override // Nm6q_10.G83uNX_24
        public Mqv2r_3 KQgRr5e_0() {
            return Mqv2r_3.this;
        }
    }

    /* compiled from: AsyncTimeout.kt */
    /* loaded from: classes.dex */
    public static final class PGPF6hQ_2 implements H5YG8fq_22 {
        public final /* synthetic */ H5YG8fq_22 KQgRr5e_0;

        public PGPF6hQ_2(H5YG8fq_22 h5YG8fq_22) {
            this.KQgRr5e_0 = h5YG8fq_22;
        }

        @Override // Nm6q_10.H5YG8fq_22, java.io.Closeable, java.lang.AutoCloseable
        public void close() {
            Mqv2r_3.this.QxJ9d_1();
            try {
                try {
                    this.KQgRr5e_0.close();
                    Mqv2r_3.this.KQgRr5e_0(true);
                } catch (IOException e) {
                    throw Mqv2r_3.this.KQgRr5e_0(e);
                }
            } catch (Throwable th) {
                Mqv2r_3.this.KQgRr5e_0(false);
                throw th;
            }
        }

        @Override // Nm6q_10.H5YG8fq_22, java.io.Flushable
        public void flush() {
            Mqv2r_3.this.QxJ9d_1();
            try {
                try {
                    this.KQgRr5e_0.flush();
                    Mqv2r_3.this.KQgRr5e_0(true);
                } catch (IOException e) {
                    throw Mqv2r_3.this.KQgRr5e_0(e);
                }
            } catch (Throwable th) {
                Mqv2r_3.this.KQgRr5e_0(false);
                throw th;
            }
        }

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

        @Override // Nm6q_10.H5YG8fq_22
        public void KQgRr5e_0(X6FRs_4 x6FRs_4, long j) {
            FSaI4_7.Xgtqvk_8.QxJ9d_1.IFxWkV_5.QxJ9d_1(x6FRs_4, "source");
            Nm6q_10.PGPF6hQ_2.KQgRr5e_0(x6FRs_4.PGPF6hQ_2(), 0L, j);
            while (true) {
                long j2 = 0;
                if (j <= 0) {
                    return;
                }
                BVpCZyH_19 bVpCZyH_19 = x6FRs_4.f348KQgRr5e_0;
                if (bVpCZyH_19 != null) {
                    while (true) {
                        if (j2 >= 65536) {
                            break;
                        }
                        j2 += bVpCZyH_19.QxJ9d_1 - bVpCZyH_19.KQgRr5e_0;
                        if (j2 >= j) {
                            j2 = j;
                            break;
                        }
                        bVpCZyH_19 = bVpCZyH_19.f318KQgRr5e_0;
                        if (bVpCZyH_19 == null) {
                            FSaI4_7.Xgtqvk_8.QxJ9d_1.IFxWkV_5.KQgRr5e_0();
                            throw null;
                        }
                    }
                    Mqv2r_3.this.QxJ9d_1();
                    try {
                        try {
                            this.KQgRr5e_0.KQgRr5e_0(x6FRs_4, j2);
                            j -= j2;
                            Mqv2r_3.this.KQgRr5e_0(true);
                        } catch (IOException e) {
                            throw Mqv2r_3.this.KQgRr5e_0(e);
                        }
                    } catch (Throwable th) {
                        Mqv2r_3.this.KQgRr5e_0(false);
                        throw th;
                    }
                } else {
                    FSaI4_7.Xgtqvk_8.QxJ9d_1.IFxWkV_5.KQgRr5e_0();
                    throw null;
                }
            }
        }

        @Override // Nm6q_10.H5YG8fq_22
        public Mqv2r_3 KQgRr5e_0() {
            return Mqv2r_3.this;
        }
    }

    /* compiled from: AsyncTimeout.kt */
    /* loaded from: classes.dex */
    public static final class QxJ9d_1 extends Thread {
        public QxJ9d_1() {
            super("Okio Watchdog");
            setDaemon(true);
        }

        @Override // java.lang.Thread, java.lang.Runnable
        public void run() {
            Mqv2r_3 KQgRr5e_0;
            while (true) {
                try {
                    synchronized (Mqv2r_3.class) {
                        KQgRr5e_0 = Mqv2r_3.KQgRr5e_0.KQgRr5e_0();
                        if (KQgRr5e_0 == Mqv2r_3.QxJ9d_1) {
                            Mqv2r_3.QxJ9d_1 = null;
                            return;
                        }
                        FSaI4_7.X6FRs_4 x6FRs_4 = FSaI4_7.X6FRs_4.KQgRr5e_0;
                    }
                    if (KQgRr5e_0 != null) {
                        KQgRr5e_0.mo180PGPF6hQ_2();
                    }
                } catch (InterruptedException unused) {
                }
            }
        }
    }

    /* renamed from: PGPF6hQ_2  reason: collision with other method in class */
    public void mo180PGPF6hQ_2() {
    }

    @Override // Nm6q_10.KVtv3R_25
    public final void QxJ9d_1() {
        if (!this.f339QxJ9d_1) {
            long QxJ9d_12 = QxJ9d_1();
            boolean m174KQgRr5e_0 = m174KQgRr5e_0();
            if (QxJ9d_12 != 0 || m174KQgRr5e_0) {
                this.f339QxJ9d_1 = true;
                KQgRr5e_0.KQgRr5e_0(this, QxJ9d_12, m174KQgRr5e_0);
                return;
            }
            return;
        }
        throw new IllegalStateException("Unbalanced enter/exit".toString());
    }

    @Override // Nm6q_10.KVtv3R_25
    public final long KQgRr5e_0(long j) {
        return this.PGPF6hQ_2 - j;
    }

    @Override // Nm6q_10.KVtv3R_25
    public final boolean QxJ9d_1() {
        if (this.f339QxJ9d_1) {
            this.f339QxJ9d_1 = false;
            return KQgRr5e_0.KQgRr5e_0(this);
        }
        return false;
    }

    public final H5YG8fq_22 KQgRr5e_0(H5YG8fq_22 h5YG8fq_22) {
        FSaI4_7.Xgtqvk_8.QxJ9d_1.IFxWkV_5.QxJ9d_1(h5YG8fq_22, "sink");
        return new PGPF6hQ_2(h5YG8fq_22);
    }

    public final G83uNX_24 KQgRr5e_0(G83uNX_24 g83uNX_24) {
        FSaI4_7.Xgtqvk_8.QxJ9d_1.IFxWkV_5.QxJ9d_1(g83uNX_24, "source");
        return new C0032Mqv2r_3(g83uNX_24);
    }

    public final void KQgRr5e_0(boolean z) {
        if (QxJ9d_1() && z) {
            throw QxJ9d_1(null);
        }
    }

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

    public final IOException KQgRr5e_0(IOException iOException) {
        FSaI4_7.Xgtqvk_8.QxJ9d_1.IFxWkV_5.QxJ9d_1(iOException, "cause");
        return !QxJ9d_1() ? iOException : QxJ9d_1(iOException);
    }
}
