package o; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; /* loaded from: classes6.dex */ public final class fWI extends fYZ { private AtomicReference> b = new AtomicReference<>(); private InterfaceC13169fSq e; public fWI(InterfaceC13169fSq interfaceC13169fSq) { this.e = interfaceC13169fSq; } @Override // o.fYZ public final void d(fSQ fsq) { LWm lWm; loop0: while (true) { lWm = this.b.get(); if (lWm != null && !lWm.isDisposed()) { break; } LWm lWm2 = new LWm<>(this.b); AtomicReference> atomicReference = this.b; while (!atomicReference.compareAndSet(lWm, lWm2)) { if (atomicReference.get() != lWm) { break; } } lWm = lWm2; break loop0; } boolean z = false; if (!lWm.a.get() && lWm.a.compareAndSet(false, true)) { z = true; } try { fsq.c(lWm); if (z) { this.e.subscribe(lWm); } } catch (Throwable th) { C13147fSI.c(th); throw fYR.c(th); } } @Override // o.AbstractC13163fSk public final void subscribeActual(InterfaceC13174fSv interfaceC13174fSv) { LWm lWm; Sts[] stsArr; Sts[] stsArr2; loop0: while (true) { lWm = this.b.get(); if (lWm != null) { break; } LWm lWm2 = new LWm<>(this.b); AtomicReference> atomicReference = this.b; while (!atomicReference.compareAndSet(lWm, lWm2)) { if (atomicReference.get() != lWm) { break; } } lWm = lWm2; break loop0; } Sts sts = new Sts<>(interfaceC13174fSv, lWm); interfaceC13174fSv.onSubscribe(sts); do { stsArr = lWm.get(); if (stsArr == LWm.e) { Throwable th = lWm.b; if (th != null) { interfaceC13174fSv.onError(th); return; } else { interfaceC13174fSv.onComplete(); return; } } int length = stsArr.length; stsArr2 = new Sts[length + 1]; System.arraycopy(stsArr, 0, stsArr2, 0, length); stsArr2[length] = sts; } while (!lWm.compareAndSet(stsArr, stsArr2)); if (sts.isDisposed()) { lWm.e(sts); } } @Override // o.fYZ public final void d() { LWm lWm = this.b.get(); if (lWm == null || !lWm.isDisposed()) { return; } AtomicReference> atomicReference = this.b; while (!atomicReference.compareAndSet(lWm, null) && atomicReference.get() == lWm) { } } /* loaded from: classes6.dex */ static final class LWm extends AtomicReference[]> implements InterfaceC13174fSv, fSJ { private static Sts[] c = new Sts[0]; static final Sts[] e = new Sts[0]; Throwable b; private AtomicReference> d; final AtomicBoolean a = new AtomicBoolean(); private AtomicReference h = new AtomicReference<>(); LWm(AtomicReference> atomicReference) { this.d = atomicReference; lazySet(c); } @Override // o.fSJ public final void dispose() { getAndSet(e); AtomicReference> atomicReference = this.d; while (!atomicReference.compareAndSet(this, null) && atomicReference.get() == this) { } EnumC13192fTf.e(this.h); } @Override // o.fSJ public final boolean isDisposed() { return get() == e; } @Override // o.InterfaceC13174fSv public final void onSubscribe(fSJ fsj) { EnumC13192fTf.a(this.h, fsj); } @Override // o.InterfaceC13174fSv public final void onNext(T t) { for (Sts sts : get()) { sts.b.onNext(t); } } @Override // o.InterfaceC13174fSv public final void onError(Throwable th) { if (this.h.get() != EnumC13192fTf.DISPOSED) { this.b = th; this.h.lazySet(EnumC13192fTf.DISPOSED); for (Sts sts : getAndSet(e)) { sts.b.onError(th); } return; } C13418fZu.c(th); } @Override // o.InterfaceC13174fSv public final void onComplete() { this.h.lazySet(EnumC13192fTf.DISPOSED); for (Sts sts : getAndSet(e)) { sts.b.onComplete(); } } public final void e(Sts sts) { Sts[] stsArr; Sts[] stsArr2; do { stsArr = get(); int length = stsArr.length; if (length == 0) { return; } int i = 0; while (true) { if (i >= length) { i = -1; break; } else if (stsArr[i] == sts) { break; } else { i++; } } if (i < 0) { return; } stsArr2 = c; if (length != 1) { stsArr2 = new Sts[length - 1]; System.arraycopy(stsArr, 0, stsArr2, 0, i); System.arraycopy(stsArr, i + 1, stsArr2, i, (length - i) - 1); } } while (!compareAndSet(stsArr, stsArr2)); } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public static final class Sts extends AtomicReference> implements fSJ { final InterfaceC13174fSv b; Sts(InterfaceC13174fSv interfaceC13174fSv, LWm lWm) { this.b = interfaceC13174fSv; lazySet(lWm); } @Override // o.fSJ public final void dispose() { LWm andSet = getAndSet(null); if (andSet != null) { andSet.e(this); } } @Override // o.fSJ public final boolean isDisposed() { return get() == null; } } }