package f.a.w.e.d;

import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

/* JADX INFO: compiled from: ObservableRefCount.java */
/* JADX INFO: loaded from: classes.dex */
public final class y<T> extends f.a.i<T> {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final f.a.x.a<T> f3901b;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final int f3902e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final TimeUnit f3903f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public a f3904g;

    /* JADX INFO: compiled from: ObservableRefCount.java */
    public static final class a extends AtomicReference<f.a.t.b> implements Runnable, f.a.v.c<f.a.t.b> {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public final y<?> f3905b;

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        public long f3906e;

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        public boolean f3907f;

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        public boolean f3908g;

        public a(y<?> yVar) {
            this.f3905b = yVar;
        }

        @Override // f.a.v.c
        public void accept(f.a.t.b bVar) throws Exception {
            f.a.t.b bVar2 = bVar;
            f.a.w.a.b.replace(this, bVar2);
            synchronized (this.f3905b) {
                if (this.f3908g) {
                    ((f.a.w.a.e) this.f3905b.f3901b).e(bVar2);
                }
            }
        }

        @Override // java.lang.Runnable
        public void run() {
            this.f3905b.F(this);
        }
    }

    /* JADX INFO: compiled from: ObservableRefCount.java */
    public static final class b<T> extends AtomicBoolean implements f.a.n<T>, f.a.t.b {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public final f.a.n<? super T> f3909b;

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        public final y<T> f3910e;

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        public final a f3911f;

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        public f.a.t.b f3912g;

        public b(f.a.n<? super T> nVar, y<T> yVar, a aVar) {
            this.f3909b = nVar;
            this.f3910e = yVar;
            this.f3911f = aVar;
        }

        @Override // f.a.n
        public void a(Throwable th) {
            if (!compareAndSet(false, true)) {
                f.a.y.a.C0(th);
            } else {
                this.f3910e.E(this.f3911f);
                this.f3909b.a(th);
            }
        }

        @Override // f.a.n
        public void b(f.a.t.b bVar) {
            if (f.a.w.a.b.validate(this.f3912g, bVar)) {
                this.f3912g = bVar;
                this.f3909b.b(this);
            }
        }

        @Override // f.a.n
        public void c(T t) {
            this.f3909b.c(t);
        }

        @Override // f.a.t.b
        public void dispose() {
            this.f3912g.dispose();
            if (compareAndSet(false, true)) {
                y<T> yVar = this.f3910e;
                a aVar = this.f3911f;
                synchronized (yVar) {
                    a aVar2 = yVar.f3904g;
                    if (aVar2 != null && aVar2 == aVar) {
                        long j2 = aVar.f3906e - 1;
                        aVar.f3906e = j2;
                        if (j2 == 0 && aVar.f3907f) {
                            yVar.F(aVar);
                        }
                    }
                }
            }
        }

        @Override // f.a.n
        public void onComplete() {
            if (compareAndSet(false, true)) {
                this.f3910e.E(this.f3911f);
                this.f3909b.onComplete();
            }
        }
    }

    public y(f.a.x.a<T> aVar) {
        TimeUnit timeUnit = TimeUnit.NANOSECONDS;
        this.f3901b = aVar;
        this.f3902e = 1;
        this.f3903f = timeUnit;
    }

    public void D(a aVar) {
        f.a.x.a<T> aVar2 = this.f3901b;
        if (aVar2 instanceof f.a.t.b) {
            ((f.a.t.b) aVar2).dispose();
        } else if (aVar2 instanceof f.a.w.a.e) {
            ((f.a.w.a.e) aVar2).e(aVar.get());
        }
    }

    public void E(a aVar) {
        synchronized (this) {
            if (this.f3901b instanceof x) {
                a aVar2 = this.f3904g;
                if (aVar2 != null && aVar2 == aVar) {
                    this.f3904g = null;
                    Objects.requireNonNull(aVar);
                }
                long j2 = aVar.f3906e - 1;
                aVar.f3906e = j2;
                if (j2 == 0) {
                    D(aVar);
                }
            } else {
                a aVar3 = this.f3904g;
                if (aVar3 != null && aVar3 == aVar) {
                    Objects.requireNonNull(aVar);
                    long j3 = aVar.f3906e - 1;
                    aVar.f3906e = j3;
                    if (j3 == 0) {
                        this.f3904g = null;
                        D(aVar);
                    }
                }
            }
        }
    }

    public void F(a aVar) {
        synchronized (this) {
            if (aVar.f3906e == 0 && aVar == this.f3904g) {
                this.f3904g = null;
                f.a.t.b bVar = aVar.get();
                f.a.w.a.b.dispose(aVar);
                f.a.x.a<T> aVar2 = this.f3901b;
                if (aVar2 instanceof f.a.t.b) {
                    ((f.a.t.b) aVar2).dispose();
                } else if (aVar2 instanceof f.a.w.a.e) {
                    if (bVar == null) {
                        aVar.f3908g = true;
                    } else {
                        ((f.a.w.a.e) aVar2).e(bVar);
                    }
                }
            }
        }
    }

    @Override // f.a.i
    public void v(f.a.n<? super T> nVar) {
        a aVar;
        boolean z;
        synchronized (this) {
            aVar = this.f3904g;
            if (aVar == null) {
                aVar = new a(this);
                this.f3904g = aVar;
            }
            long j2 = aVar.f3906e;
            int i2 = (j2 > 0L ? 1 : (j2 == 0L ? 0 : -1));
            long j3 = j2 + 1;
            aVar.f3906e = j3;
            z = true;
            if (aVar.f3907f || j3 != this.f3902e) {
                z = false;
            } else {
                aVar.f3907f = true;
            }
        }
        this.f3901b.d(new b(nVar, this, aVar));
        if (z) {
            this.f3901b.D(aVar);
        }
    }
}
