package o.a.h.g;

import java.util.Objects;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import o.a.d;
import o.a.h.e.a.g;

/* JADX INFO: loaded from: classes.dex */
public final class m extends o.a.d {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final m f6090b = new m();

    public static final class a implements Runnable {

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

        /* JADX INFO: renamed from: m, reason: collision with root package name */
        public final c f6092m;

        /* JADX INFO: renamed from: n, reason: collision with root package name */
        public final long f6093n;

        public a(Runnable runnable, c cVar, long j2) {
            this.f6091f = runnable;
            this.f6092m = cVar;
            this.f6093n = j2;
        }

        @Override // java.lang.Runnable
        public void run() {
            if (this.f6092m.f6101o) {
                return;
            }
            long jA = this.f6092m.a(TimeUnit.MILLISECONDS);
            long j2 = this.f6093n;
            if (j2 > jA) {
                try {
                    Thread.sleep(j2 - jA);
                } catch (InterruptedException e2) {
                    Thread.currentThread().interrupt();
                    g.h.a.i.P2(e2);
                    return;
                }
            }
            if (this.f6092m.f6101o) {
                return;
            }
            this.f6091f.run();
        }
    }

    public static final class b implements Comparable<b> {

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

        /* JADX INFO: renamed from: m, reason: collision with root package name */
        public final long f6095m;

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

        /* JADX INFO: renamed from: o, reason: collision with root package name */
        public volatile boolean f6097o;

        public b(Runnable runnable, Long l2, int i2) {
            this.f6094f = runnable;
            this.f6095m = l2.longValue();
            this.f6096n = i2;
        }

        @Override // java.lang.Comparable
        public int compareTo(b bVar) {
            b bVar2 = bVar;
            long j2 = this.f6095m;
            long j3 = bVar2.f6095m;
            int i2 = 1;
            int i3 = j2 < j3 ? -1 : j2 > j3 ? 1 : 0;
            if (i3 != 0) {
                return i3;
            }
            int i4 = this.f6096n;
            int i5 = bVar2.f6096n;
            if (i4 < i5) {
                i2 = -1;
            } else if (i4 <= i5) {
                i2 = 0;
            }
            return i2;
        }
    }

    public static final class c extends d.c implements o.a.e.b {

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        public final PriorityBlockingQueue<b> f6098f = new PriorityBlockingQueue<>();

        /* JADX INFO: renamed from: m, reason: collision with root package name */
        public final AtomicInteger f6099m = new AtomicInteger();

        /* JADX INFO: renamed from: n, reason: collision with root package name */
        public final AtomicInteger f6100n = new AtomicInteger();

        /* JADX INFO: renamed from: o, reason: collision with root package name */
        public volatile boolean f6101o;

        public final class a implements Runnable {

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

            public a(b bVar) {
                this.f6102f = bVar;
            }

            @Override // java.lang.Runnable
            public void run() {
                this.f6102f.f6097o = true;
                c.this.f6098f.remove(this.f6102f);
            }
        }

        @Override // o.a.e.b
        public void b() {
            this.f6101o = true;
        }

        @Override // o.a.d.c
        public o.a.e.b c(Runnable runnable) {
            return g(runnable, a(TimeUnit.MILLISECONDS));
        }

        @Override // o.a.d.c
        public o.a.e.b d(Runnable runnable, long j2, TimeUnit timeUnit) {
            long millis = timeUnit.toMillis(j2) + a(TimeUnit.MILLISECONDS);
            return g(new a(runnable, this, millis), millis);
        }

        public o.a.e.b g(Runnable runnable, long j2) {
            o.a.h.a.c cVar = o.a.h.a.c.INSTANCE;
            if (this.f6101o) {
                return cVar;
            }
            b bVar = new b(runnable, Long.valueOf(j2), this.f6100n.incrementAndGet());
            this.f6098f.add(bVar);
            if (this.f6099m.getAndIncrement() != 0) {
                return new o.a.e.c(new a(bVar));
            }
            int iAddAndGet = 1;
            while (!this.f6101o) {
                b bVarPoll = this.f6098f.poll();
                if (bVarPoll == null) {
                    iAddAndGet = this.f6099m.addAndGet(-iAddAndGet);
                    if (iAddAndGet == 0) {
                        return cVar;
                    }
                } else if (!bVarPoll.f6097o) {
                    bVarPoll.f6094f.run();
                }
            }
            this.f6098f.clear();
            return cVar;
        }
    }

    @Override // o.a.d
    public d.c a() {
        return new c();
    }

    @Override // o.a.d
    public o.a.e.b b(Runnable runnable) {
        ((g.b) runnable).run();
        return o.a.h.a.c.INSTANCE;
    }

    @Override // o.a.d
    public o.a.e.b c(Runnable runnable, long j2, TimeUnit timeUnit) {
        try {
            timeUnit.sleep(j2);
            Objects.requireNonNull(runnable, "run is null");
            runnable.run();
        } catch (InterruptedException e2) {
            Thread.currentThread().interrupt();
            g.h.a.i.P2(e2);
        }
        return o.a.h.a.c.INSTANCE;
    }
}
