package x9; import java.io.IOException; import java.io.InterruptedIOException; import java.util.concurrent.TimeUnit; /* loaded from: classes2.dex */ public class a extends w { /* renamed from: h, reason: collision with root package name */ private static final long f15454h; /* renamed from: i, reason: collision with root package name */ private static final long f15455i; /* renamed from: j, reason: collision with root package name */ static a f15456j; /* renamed from: e, reason: collision with root package name */ private boolean f15457e; /* renamed from: f, reason: collision with root package name */ private a f15458f; /* renamed from: g, reason: collision with root package name */ private long f15459g; /* renamed from: x9.a$a, reason: collision with other inner class name */ class C0264a implements u { /* renamed from: a, reason: collision with root package name */ final /* synthetic */ u f15460a; C0264a(u uVar) { this.f15460a = uVar; } @Override // x9.u public w b() { return a.this; } @Override // x9.u, java.io.Closeable, java.lang.AutoCloseable public void close() { a.this.k(); try { try { this.f15460a.close(); a.this.m(true); } catch (IOException e10) { throw a.this.l(e10); } } catch (Throwable th) { a.this.m(false); throw th; } } @Override // x9.u, java.io.Flushable public void flush() { a.this.k(); try { try { this.f15460a.flush(); a.this.m(true); } catch (IOException e10) { throw a.this.l(e10); } } catch (Throwable th) { a.this.m(false); throw th; } } @Override // x9.u public void t0(x9.c cVar, long j10) { x.b(cVar.f15468b, 0L, j10); while (true) { long j11 = 0; if (j10 <= 0) { return; } r rVar = cVar.f15467a; while (true) { if (j11 >= 65536) { break; } j11 += rVar.f15510c - rVar.f15509b; if (j11 >= j10) { j11 = j10; break; } rVar = rVar.f15513f; } a.this.k(); try { try { this.f15460a.t0(cVar, j11); j10 -= j11; a.this.m(true); } catch (IOException e10) { throw a.this.l(e10); } } catch (Throwable th) { a.this.m(false); throw th; } } } public String toString() { return "AsyncTimeout.sink(" + this.f15460a + ")"; } } class b implements v { /* renamed from: a, reason: collision with root package name */ final /* synthetic */ v f15462a; b(v vVar) { this.f15462a = vVar; } @Override // x9.v public w b() { return a.this; } @Override // x9.v, java.io.Closeable, java.lang.AutoCloseable public void close() { a.this.k(); try { try { this.f15462a.close(); a.this.m(true); } catch (IOException e10) { throw a.this.l(e10); } } catch (Throwable th) { a.this.m(false); throw th; } } @Override // x9.v public long s0(x9.c cVar, long j10) { a.this.k(); try { try { long s02 = this.f15462a.s0(cVar, j10); a.this.m(true); return s02; } catch (IOException e10) { throw a.this.l(e10); } } catch (Throwable th) { a.this.m(false); throw th; } } public String toString() { return "AsyncTimeout.source(" + this.f15462a + ")"; } } private static final class c extends Thread { c() { super("Okio Watchdog"); setDaemon(true); } /* JADX WARN: Code restructure failed: missing block: B:19:0x0015, code lost: r1.t(); */ @Override // java.lang.Thread, java.lang.Runnable /* Code decompiled incorrectly, please refer to instructions dump. To view partially-correct code enable 'Show inconsistent code' option in preferences */ public void run() { /* r3 = this; L0: java.lang.Class r0 = x9.a.class monitor-enter(r0) // Catch: java.lang.InterruptedException -> L0 x9.a r1 = x9.a.i() // Catch: java.lang.Throwable -> L19 if (r1 != 0) goto Lb monitor-exit(r0) // Catch: java.lang.Throwable -> L19 goto L0 Lb: x9.a r2 = x9.a.f15456j // Catch: java.lang.Throwable -> L19 if (r1 != r2) goto L14 r1 = 0 x9.a.f15456j = r1 // Catch: java.lang.Throwable -> L19 monitor-exit(r0) // Catch: java.lang.Throwable -> L19 return L14: monitor-exit(r0) // Catch: java.lang.Throwable -> L19 r1.t() // Catch: java.lang.InterruptedException -> L0 goto L0 L19: r1 = move-exception monitor-exit(r0) // Catch: java.lang.Throwable -> L19 throw r1 // Catch: java.lang.InterruptedException -> L0 */ throw new UnsupportedOperationException("Method not decompiled: x9.a.c.run():void"); } } static { long millis = TimeUnit.SECONDS.toMillis(60L); f15454h = millis; f15455i = TimeUnit.MILLISECONDS.toNanos(millis); } static a i() { a aVar = f15456j.f15458f; if (aVar == null) { long nanoTime = System.nanoTime(); a.class.wait(f15454h); if (f15456j.f15458f != null || System.nanoTime() - nanoTime < f15455i) { return null; } return f15456j; } long p10 = aVar.p(System.nanoTime()); if (p10 > 0) { long j10 = p10 / 1000000; a.class.wait(j10, (int) (p10 - (1000000 * j10))); return null; } f15456j.f15458f = aVar.f15458f; aVar.f15458f = null; return aVar; } private static synchronized boolean j(a aVar) { synchronized (a.class) { a aVar2 = f15456j; while (aVar2 != null) { a aVar3 = aVar2.f15458f; if (aVar3 == aVar) { aVar2.f15458f = aVar.f15458f; aVar.f15458f = null; return false; } aVar2 = aVar3; } return true; } } private long p(long j10) { return this.f15459g - j10; } private static synchronized void q(a aVar, long j10, boolean z10) { synchronized (a.class) { if (f15456j == null) { f15456j = new a(); new c().start(); } long nanoTime = System.nanoTime(); if (j10 != 0 && z10) { aVar.f15459g = Math.min(j10, aVar.c() - nanoTime) + nanoTime; } else if (j10 != 0) { aVar.f15459g = j10 + nanoTime; } else { if (!z10) { throw new AssertionError(); } aVar.f15459g = aVar.c(); } long p10 = aVar.p(nanoTime); a aVar2 = f15456j; while (true) { a aVar3 = aVar2.f15458f; if (aVar3 == null || p10 < aVar3.p(nanoTime)) { break; } else { aVar2 = aVar2.f15458f; } } aVar.f15458f = aVar2.f15458f; aVar2.f15458f = aVar; if (aVar2 == f15456j) { a.class.notify(); } } } public final void k() { if (this.f15457e) { throw new IllegalStateException("Unbalanced enter/exit"); } long h10 = h(); boolean e10 = e(); if (h10 != 0 || e10) { this.f15457e = true; q(this, h10, e10); } } final IOException l(IOException iOException) { return !n() ? iOException : o(iOException); } final void m(boolean z10) { if (n() && z10) { throw o(null); } } public final boolean n() { if (!this.f15457e) { return false; } this.f15457e = false; return j(this); } protected IOException o(IOException iOException) { InterruptedIOException interruptedIOException = new InterruptedIOException("timeout"); if (iOException != null) { interruptedIOException.initCause(iOException); } return interruptedIOException; } public final u r(u uVar) { return new C0264a(uVar); } public final v s(v vVar) { return new b(vVar); } protected void t() { } }