package j0.l.a; import androidx.recyclerview.widget.RecyclerView; import j0.l.a.e; import j0.l.e.h; import j0.l.e.m.c; import j0.l.e.n.n; import j0.l.e.n.y; import java.util.Queue; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; import rx.Observable; import rx.Producer; import rx.Subscriber; import rx.Subscription; import rx.exceptions.MissingBackpressureException; import rx.functions.Action1; /* compiled from: OperatorPublish */ public final class h1 extends j0.m.b { public final Observable j; public final AtomicReference> k; /* compiled from: OperatorPublish */ public static final class a extends AtomicLong implements Producer, Subscription { private static final long serialVersionUID = -4453897557930727610L; public final Subscriber child; public final b parent; public a(b bVar, Subscriber subscriber) { this.parent = bVar; this.child = subscriber; lazySet(-4611686018427387904L); } public long a(long j) { long j2; long j3; if (j > 0) { do { j2 = get(); if (j2 == -4611686018427387904L) { throw new IllegalStateException("Produced without request"); } else if (j2 == Long.MIN_VALUE) { return Long.MIN_VALUE; } else { j3 = j2 - j; if (j3 < 0) { throw new IllegalStateException(c.d.b.a.a.A(c.d.b.a.a.S("More produced (", j, ") than requested ("), j2, ")")); } } } while (!compareAndSet(j2, j3)); return j3; } throw new IllegalArgumentException("Cant produce zero or less"); } @Override // rx.Subscription public boolean isUnsubscribed() { return get() == Long.MIN_VALUE; } @Override // rx.Producer public void j(long j) { long j2; long j3; int i = (j > 0 ? 1 : (j == 0 ? 0 : -1)); if (i >= 0) { do { j2 = get(); if (j2 != Long.MIN_VALUE) { if (j2 >= 0 && i == 0) { return; } if (j2 == -4611686018427387904L) { j3 = j; } else { j3 = j2 + j; if (j3 < 0) { j3 = RecyclerView.FOREVER_NS; } } } else { return; } } while (!compareAndSet(j2, j3)); this.parent.b(); } } @Override // rx.Subscription public void unsubscribe() { a[] aVarArr; a[] aVarArr2; if (get() != Long.MIN_VALUE && getAndSet(Long.MIN_VALUE) != Long.MIN_VALUE) { b bVar = this.parent; do { aVarArr = bVar.n.get(); if (aVarArr != b.i && aVarArr != b.j) { int length = aVarArr.length; int i = 0; while (true) { if (i >= length) { i = -1; break; } else if (aVarArr[i].equals(this)) { break; } else { i++; } } if (i < 0) { break; } else if (length == 1) { aVarArr2 = b.i; } else { a[] aVarArr3 = new a[(length - 1)]; System.arraycopy(aVarArr, 0, aVarArr3, 0, i); System.arraycopy(aVarArr, i + 1, aVarArr3, i, (length - i) - 1); aVarArr2 = aVarArr3; } } else { break; } } while (!bVar.n.compareAndSet(aVarArr, aVarArr2)); this.parent.b(); } } } /* compiled from: OperatorPublish */ public static final class b extends Subscriber implements Subscription { public static final a[] i = new a[0]; public static final a[] j = new a[0]; public final Queue k; public final AtomicReference> l; public volatile Object m; public final AtomicReference n; public final AtomicBoolean o; public boolean p; public boolean q; public b(AtomicReference> atomicReference) { this.k = y.b() ? new n<>(h.i) : new c<>(h.i); this.n = new AtomicReference<>(i); this.l = atomicReference; this.o = new AtomicBoolean(); } public boolean a(Object obj, boolean z2) { int i2 = 0; if (obj != null) { if (!e.c(obj)) { Throwable th = ((e.c) obj).e; this.l.compareAndSet(this, null); try { a[] andSet = this.n.getAndSet(j); int length = andSet.length; while (i2 < length) { andSet[i2].child.onError(th); i2++; } return true; } finally { unsubscribe(); } } else if (z2) { this.l.compareAndSet(this, null); try { a[] andSet2 = this.n.getAndSet(j); int length2 = andSet2.length; while (i2 < length2) { andSet2[i2].child.onCompleted(); i2++; } return true; } finally { unsubscribe(); } } } return false; } /* JADX WARNING: Code restructure failed: missing block: B:63:0x00c3, code lost: if (r4 == false) goto L_0x00d5; */ /* JADX WARNING: Code restructure failed: missing block: B:83:0x00dc, code lost: r0 = th; */ public void b() { boolean z2; Throwable th; long j2; synchronized (this) { if (this.p) { this.q = true; return; } else { this.p = true; this.q = false; } } while (true) { try { Object obj = this.m; boolean isEmpty = this.k.isEmpty(); if (!a(obj, isEmpty)) { if (!isEmpty) { a[] aVarArr = this.n.get(); int length = aVarArr.length; long j3 = RecyclerView.FOREVER_NS; int i2 = 0; for (a aVar : aVarArr) { long j4 = aVar.get(); if (j4 >= 0) { j3 = Math.min(j3, j4); } else if (j4 == Long.MIN_VALUE) { i2++; } } if (length != i2) { int i3 = 0; while (true) { j2 = (long) i3; if (j2 >= j3) { break; } Object obj2 = this.m; Object poll = this.k.poll(); boolean z3 = poll == null; if (!a(obj2, z3)) { if (z3) { isEmpty = z3; break; } Object b = e.b(poll); for (a aVar2 : aVarArr) { if (aVar2.get() > 0) { try { aVar2.child.onNext(b); aVar2.a(1); } catch (Throwable th2) { aVar2.unsubscribe(); c.q.a.k.a.b0(th2, aVar2.child, b); } } } i3++; isEmpty = z3; } else { return; } } if (i3 > 0) { request(j2); } if (j3 != 0) { } } else if (!a(this.m, this.k.poll() == null)) { request(1); } else { return; } } synchronized (this) { try { if (!this.q) { this.p = false; try { return; } catch (Throwable th3) { th = th3; z2 = true; while (true) { try { break; } catch (Throwable th4) { th = th4; } } throw th; } } else { this.q = false; } } catch (Throwable th5) { th = th5; z2 = false; while (true) { break; } throw th; } } } else { return; } } catch (Throwable th6) { Throwable th7 = th6; z2 = false; if (!z2) { synchronized (this) { this.p = false; } } throw th7; } } } @Override // j0.g public void onCompleted() { if (this.m == null) { this.m = e.a; b(); } } @Override // j0.g public void onError(Throwable th) { if (this.m == null) { this.m = new e.c(th); b(); } } @Override // j0.g public void onNext(T t) { Queue queue = this.k; if (t == null) { t = (T) e.b; } if (!queue.offer(t)) { MissingBackpressureException missingBackpressureException = new MissingBackpressureException(); if (this.m == null) { this.m = new e.c(missingBackpressureException); b(); return; } return; } b(); } @Override // rx.Subscriber public void onStart() { request((long) h.i); } } public h1(Observable.a aVar, Observable observable, AtomicReference> atomicReference) { super(aVar); this.j = observable; this.k = atomicReference; } @Override // j0.m.b public void j0(Action1 action1) { b bVar; while (true) { bVar = this.k.get(); if (bVar != null && !bVar.isUnsubscribed()) { break; } b bVar2 = new b<>(this.k); bVar2.add(new j0.r.a(new i1(bVar2))); if (this.k.compareAndSet(bVar, bVar2)) { bVar = bVar2; break; } } boolean z2 = true; if (bVar.o.get() || !bVar.o.compareAndSet(false, true)) { z2 = false; } ((z) action1).call(bVar); if (z2) { this.j.h0(bVar); } } }