package n7;

import M4.k;
import P6.g;
import d5.AbstractC0844a;
import java.util.ArrayList;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import l6.C1139a;

/* JADX INFO: loaded from: classes.dex */
public final class d {

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public static final d f15542h;
    public static final Logger i;

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

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

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public int f15544b = 10000;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final ArrayList f15547e = new ArrayList();

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final ArrayList f15548f = new ArrayList();

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final k f15549g = new k(28, this);

    static {
        String str = l7.b.f14418g + " TaskRunner";
        g.e(str, "name");
        l7.a aVar = new l7.a(str, true);
        C1139a c1139a = new C1139a();
        c1139a.f14409q = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue(), aVar);
        f15542h = new d(c1139a);
        Logger logger = Logger.getLogger(d.class.getName());
        g.d(logger, "getLogger(TaskRunner::class.java.name)");
        i = logger;
    }

    public d(C1139a c1139a) {
        this.f15543a = c1139a;
    }

    public static final void a(d dVar, a aVar) {
        dVar.getClass();
        byte[] bArr = l7.b.f14412a;
        Thread threadCurrentThread = Thread.currentThread();
        String name = threadCurrentThread.getName();
        threadCurrentThread.setName(aVar.f15530a);
        try {
            long jA = aVar.a();
            synchronized (dVar) {
                dVar.b(aVar, jA);
            }
            threadCurrentThread.setName(name);
        } catch (Throwable th) {
            synchronized (dVar) {
                dVar.b(aVar, -1L);
                threadCurrentThread.setName(name);
                throw th;
            }
        }
    }

    public final void b(a aVar, long j8) {
        byte[] bArr = l7.b.f14412a;
        c cVar = aVar.f15532c;
        g.b(cVar);
        if (cVar.f15539d != aVar) {
            throw new IllegalStateException("Check failed.");
        }
        boolean z8 = cVar.f15541f;
        cVar.f15541f = false;
        cVar.f15539d = null;
        this.f15547e.remove(cVar);
        if (j8 != -1 && !z8 && !cVar.f15538c) {
            cVar.d(aVar, j8, true);
        }
        if (cVar.f15540e.isEmpty()) {
            return;
        }
        this.f15548f.add(cVar);
    }

    public final a c() {
        ArrayList arrayList;
        a aVar;
        boolean z8;
        byte[] bArr = l7.b.f14412a;
        while (true) {
            ArrayList arrayList2 = this.f15548f;
            if (arrayList2.isEmpty()) {
                return null;
            }
            C1139a c1139a = this.f15543a;
            long jNanoTime = System.nanoTime();
            int size = arrayList2.size();
            long jMin = Long.MAX_VALUE;
            int i5 = 0;
            a aVar2 = null;
            while (true) {
                if (i5 >= size) {
                    arrayList = arrayList2;
                    aVar = null;
                    z8 = false;
                    break;
                }
                Object obj = arrayList2.get(i5);
                i5++;
                aVar = null;
                a aVar3 = (a) ((c) obj).f15540e.get(0);
                arrayList = arrayList2;
                long jMax = Math.max(0L, aVar3.f15533d - jNanoTime);
                if (jMax > 0) {
                    jMin = Math.min(jMax, jMin);
                } else {
                    if (aVar2 != null) {
                        z8 = true;
                        break;
                    }
                    aVar2 = aVar3;
                }
                arrayList2 = arrayList;
            }
            ArrayList arrayList3 = this.f15547e;
            if (aVar2 != null) {
                byte[] bArr2 = l7.b.f14412a;
                aVar2.f15533d = -1L;
                c cVar = aVar2.f15532c;
                g.b(cVar);
                cVar.f15540e.remove(aVar2);
                ArrayList arrayList4 = arrayList;
                arrayList4.remove(cVar);
                cVar.f15539d = aVar2;
                arrayList3.add(cVar);
                if (z8 || (!this.f15545c && !arrayList4.isEmpty())) {
                    k kVar = this.f15549g;
                    g.e(kVar, "runnable");
                    ((ThreadPoolExecutor) c1139a.f14409q).execute(kVar);
                }
                return aVar2;
            }
            ArrayList arrayList5 = arrayList;
            if (this.f15545c) {
                if (jMin >= this.f15546d - jNanoTime) {
                    return aVar;
                }
                notify();
                return aVar;
            }
            this.f15545c = true;
            this.f15546d = jNanoTime + jMin;
            try {
                try {
                    long j8 = jMin / 1000000;
                    Long.signum(j8);
                    long j9 = jMin - (1000000 * j8);
                    if (j8 > 0 || jMin > 0) {
                        wait(j8, (int) j9);
                    }
                } catch (InterruptedException unused) {
                    for (int size2 = arrayList3.size() - 1; -1 < size2; size2--) {
                        ((c) arrayList3.get(size2)).b();
                    }
                    int size3 = arrayList5.size() - 1;
                    for (int i8 = -1; i8 < size3; i8 = -1) {
                        c cVar2 = (c) arrayList5.get(size3);
                        cVar2.b();
                        if (cVar2.f15540e.isEmpty()) {
                            arrayList5.remove(size3);
                        }
                        size3--;
                    }
                }
            } finally {
                this.f15545c = false;
            }
        }
    }

    public final void d(c cVar) {
        g.e(cVar, "taskQueue");
        byte[] bArr = l7.b.f14412a;
        if (cVar.f15539d == null) {
            boolean zIsEmpty = cVar.f15540e.isEmpty();
            ArrayList arrayList = this.f15548f;
            if (zIsEmpty) {
                arrayList.remove(cVar);
            } else {
                g.e(arrayList, "<this>");
                if (!arrayList.contains(cVar)) {
                    arrayList.add(cVar);
                }
            }
        }
        boolean z8 = this.f15545c;
        C1139a c1139a = this.f15543a;
        if (z8) {
            notify();
            return;
        }
        k kVar = this.f15549g;
        g.e(kVar, "runnable");
        ((ThreadPoolExecutor) c1139a.f14409q).execute(kVar);
    }

    public final c e() {
        int i5;
        synchronized (this) {
            i5 = this.f15544b;
            this.f15544b = i5 + 1;
        }
        return new c(this, AbstractC0844a.j("Q", i5));
    }
}
