package o; import java.util.Objects; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; /* loaded from: classes6.dex */ public final class fUQ extends AbstractC13163fSk { private boolean a; private fSS> b; private AbstractC13163fSk c; public fUQ(AbstractC13163fSk abstractC13163fSk, fSS> fss, boolean z) { this.c = abstractC13163fSk; this.b = fss; this.a = z; } @Override // o.AbstractC13163fSk public final void subscribeActual(InterfaceC13174fSv interfaceC13174fSv) { if (fUP.a(this.c, this.b, interfaceC13174fSv)) { return; } this.c.subscribe(new RVV(interfaceC13174fSv, this.b, this.a)); } /* loaded from: classes6.dex */ static final class RVV extends AtomicInteger implements InterfaceC13174fSv, fSJ { private static LWm e = new LWm<>(null); fSJ b; final boolean c; private volatile boolean f; private InterfaceC13174fSv g; private fSS> h; private volatile boolean i; final C13355fYI d = new C13355fYI(); final AtomicReference> a = new AtomicReference<>(); RVV(InterfaceC13174fSv interfaceC13174fSv, fSS> fss, boolean z) { this.g = interfaceC13174fSv; this.h = fss; this.c = z; } @Override // o.InterfaceC13174fSv public final void onSubscribe(fSJ fsj) { if (EnumC13192fTf.d(this.b, fsj)) { this.b = fsj; this.g.onSubscribe(this); } } @Override // o.InterfaceC13174fSv public final void onNext(T t) { LWm lWm = this.a.get(); if (lWm != null) { EnumC13192fTf.e(lWm); } try { fSD fsd = (fSD) Objects.requireNonNull(this.h.a(t), "The mapper returned a null SingleSource"); LWm lWm2 = new LWm<>(this); while (true) { LWm lWm3 = this.a.get(); if (lWm3 != e) { AtomicReference> atomicReference = this.a; while (!atomicReference.compareAndSet(lWm3, lWm2)) { if (atomicReference.get() != lWm3) { break; } } fsd.c(lWm2); return; } return; } } catch (Throwable th) { C13147fSI.c(th); this.b.dispose(); this.a.getAndSet(e); onError(th); } } @Override // o.InterfaceC13174fSv public final void onComplete() { this.f = true; e(); } final void c() { AtomicReference> atomicReference = this.a; LWm lWm = e; LWm lWm2 = (LWm) atomicReference.getAndSet(lWm); if (lWm2 == null || lWm2 == lWm) { return; } EnumC13192fTf.e(lWm2); } @Override // o.fSJ public final void dispose() { this.i = true; this.b.dispose(); AtomicReference> atomicReference = this.a; LWm lWm = e; LWm lWm2 = (LWm) atomicReference.getAndSet(lWm); if (lWm2 != null && lWm2 != lWm) { EnumC13192fTf.e(lWm2); } Throwable b = fYR.b(this.d); if (b == null || b == fYR.b) { return; } C13418fZu.c(b); } final void e() { if (getAndIncrement() != 0) { return; } InterfaceC13174fSv interfaceC13174fSv = this.g; C13355fYI c13355fYI = this.d; AtomicReference> atomicReference = this.a; int i = 1; while (!this.i) { if (c13355fYI.get() == null || this.c) { boolean z = this.f; LWm lWm = atomicReference.get(); boolean z2 = lWm == null; if (!z || !z2) { if (z2 || lWm.b == null) { i = addAndGet(-i); if (i == 0) { return; } } else { while (!atomicReference.compareAndSet(lWm, null) && atomicReference.get() == lWm) { } interfaceC13174fSv.onNext(lWm.b); } } else { Throwable b = fYR.b(c13355fYI); if (b == null) { interfaceC13174fSv.onComplete(); return; } else { if (b != fYR.b) { interfaceC13174fSv.onError(b); return; } return; } } } else { Throwable b2 = fYR.b(c13355fYI); if (b2 == null) { interfaceC13174fSv.onComplete(); return; } else { if (b2 != fYR.b) { interfaceC13174fSv.onError(b2); return; } return; } } } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public static final class LWm extends AtomicReference implements InterfaceC13176fSx { volatile R b; private RVV e; LWm(RVV rvv) { this.e = rvv; } @Override // o.InterfaceC13176fSx public final void onSubscribe(fSJ fsj) { EnumC13192fTf.a(this, fsj); } @Override // o.InterfaceC13176fSx public final void e(R r) { this.b = r; this.e.e(); } @Override // o.InterfaceC13176fSx public final void onError(Throwable th) { RVV rvv = this.e; AtomicReference> atomicReference = rvv.a; while (!atomicReference.compareAndSet(this, null)) { if (atomicReference.get() != this) { C13418fZu.c(th); return; } } if (fYR.c(rvv.d, th)) { if (!rvv.c) { rvv.b.dispose(); rvv.c(); } rvv.e(); return; } C13418fZu.c(th); } } @Override // o.InterfaceC13174fSv public final void onError(Throwable th) { if (fYR.c(this.d, th)) { if (!this.c) { c(); } this.f = true; e(); return; } C13418fZu.c(th); } @Override // o.fSJ public final boolean isDisposed() { return this.i; } } }