package p447.p448.p451;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import kotlin.jvm.internal.DefaultConstructorMarker;
import p138.p139.p143.p144.C2803;
import p138.p176.p177.p242.p246.p251.C4303;
import p402.InterfaceC6540;
import p402.p411.p413.C6635;
import p447.p448.C6943;
import p447.p448.ThreadFactoryC6942;

/* JADX INFO: renamed from: ސ.ޣ.ރ.ށ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes.dex */
@InterfaceC6540
public final class C6953 {

    /* JADX INFO: renamed from: ޅ, reason: contains not printable characters */
    public static final C6953 f20216;

    /* JADX INFO: renamed from: ކ, reason: contains not printable characters */
    public static final Logger f20217;

    /* JADX INFO: renamed from: އ, reason: contains not printable characters */
    public static final C6955 f20218 = new C6955(null);

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public int f20219;

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public boolean f20220;

    /* JADX INFO: renamed from: ހ, reason: contains not printable characters */
    public long f20221;

    /* JADX INFO: renamed from: ށ, reason: contains not printable characters */
    public final List<C6952> f20222;

    /* JADX INFO: renamed from: ނ, reason: contains not printable characters */
    public final List<C6952> f20223;

    /* JADX INFO: renamed from: ރ, reason: contains not printable characters */
    public final Runnable f20224;

    /* JADX INFO: renamed from: ބ, reason: contains not printable characters */
    public final InterfaceC6954 f20225;

    /* JADX INFO: renamed from: ސ.ޣ.ރ.ށ$֏, reason: contains not printable characters */
    public interface InterfaceC6954 {
        void execute(Runnable runnable);

        /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
        long mo10195();

        /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
        void mo10196(C6953 c6953);

        /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
        void mo10197(C6953 c6953, long j);
    }

    /* JADX INFO: renamed from: ސ.ޣ.ރ.ށ$ؠ, reason: contains not printable characters */
    public static final class C6955 {
        public C6955() {
        }

        public /* synthetic */ C6955(DefaultConstructorMarker defaultConstructorMarker) {
        }
    }

    /* JADX INFO: renamed from: ސ.ޣ.ރ.ށ$ހ, reason: contains not printable characters */
    public static final class C6956 implements InterfaceC6954 {

        /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
        public final ThreadPoolExecutor f20226;

        public C6956(ThreadFactory threadFactory) {
            if (threadFactory != null) {
                this.f20226 = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue(), threadFactory);
            } else {
                C6635.m9824("threadFactory");
                throw null;
            }
        }

        @Override // p447.p448.p451.C6953.InterfaceC6954
        public void execute(Runnable runnable) {
            if (runnable != null) {
                this.f20226.execute(runnable);
            } else {
                C6635.m9824("runnable");
                throw null;
            }
        }

        @Override // p447.p448.p451.C6953.InterfaceC6954
        /* JADX INFO: renamed from: ֏ */
        public long mo10195() {
            return System.nanoTime();
        }

        @Override // p447.p448.p451.C6953.InterfaceC6954
        /* JADX INFO: renamed from: ֏ */
        public void mo10196(C6953 c6953) {
            if (c6953 != null) {
                c6953.notify();
            } else {
                C6635.m9824("taskRunner");
                throw null;
            }
        }

        @Override // p447.p448.p451.C6953.InterfaceC6954
        /* JADX INFO: renamed from: ֏ */
        public void mo10197(C6953 c6953, long j) throws InterruptedException {
            if (c6953 == null) {
                C6635.m9824("taskRunner");
                throw null;
            }
            long j2 = j / 1000000;
            long j3 = j - (1000000 * j2);
            if (j2 > 0 || j > 0) {
                c6953.wait(j2, (int) j3);
            }
        }
    }

    /* JADX INFO: renamed from: ސ.ޣ.ރ.ށ$ށ, reason: contains not printable characters */
    public static final class RunnableC6957 implements Runnable {
        public RunnableC6957() {
        }

        @Override // java.lang.Runnable
        public void run() {
            AbstractC6950 abstractC6950M10189;
            while (true) {
                synchronized (C6953.this) {
                    abstractC6950M10189 = C6953.this.m10189();
                }
                if (abstractC6950M10189 == null) {
                    return;
                }
                C6952 c6952 = abstractC6950M10189.f20205;
                if (c6952 == null) {
                    C6635.m9822();
                    throw null;
                }
                long jMo10195 = -1;
                if (C6953.f20218 == null) {
                    throw null;
                }
                boolean zIsLoggable = C6953.f20217.isLoggable(Level.FINE);
                if (zIsLoggable) {
                    jMo10195 = c6952.f20214.f20225.mo10195();
                    C4303.m7115(abstractC6950M10189, c6952, "starting");
                }
                try {
                    C6953.this.m10190(abstractC6950M10189);
                    if (zIsLoggable) {
                        long jMo101952 = c6952.f20214.f20225.mo10195() - jMo10195;
                        StringBuilder sbM4889 = C2803.m4889("finished run in ");
                        sbM4889.append(C4303.m7153(jMo101952));
                        C4303.m7115(abstractC6950M10189, c6952, sbM4889.toString());
                    }
                } finally {
                }
            }
        }
    }

    static {
        String strM4884 = C2803.m4884(new StringBuilder(), C6943.f20197, " TaskRunner");
        if (strM4884 == null) {
            C6635.m9824("name");
            throw null;
        }
        f20216 = new C6953(new C6956(new ThreadFactoryC6942(strM4884, true)));
        Logger logger = Logger.getLogger(C6953.class.getName());
        C6635.m9823((Object) logger, "Logger.getLogger(TaskRunner::class.java.name)");
        f20217 = logger;
    }

    public C6953(InterfaceC6954 interfaceC6954) {
        if (interfaceC6954 == null) {
            C6635.m9824("backend");
            throw null;
        }
        this.f20225 = interfaceC6954;
        this.f20219 = 10000;
        this.f20222 = new ArrayList();
        this.f20223 = new ArrayList();
        this.f20224 = new RunnableC6957();
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final AbstractC6950 m10189() {
        boolean z;
        if (C6943.f20196 && !Thread.holdsLock(this)) {
            StringBuilder sbM4889 = C2803.m4889("Thread ");
            Thread threadCurrentThread = Thread.currentThread();
            C6635.m9823((Object) threadCurrentThread, "Thread.currentThread()");
            sbM4889.append(threadCurrentThread.getName());
            sbM4889.append(" MUST hold lock on ");
            sbM4889.append(this);
            throw new AssertionError(sbM4889.toString());
        }
        while (true) {
            AbstractC6950 abstractC6950 = null;
            if (this.f20223.isEmpty()) {
                return null;
            }
            long jMo10195 = this.f20225.mo10195();
            long jMin = Long.MAX_VALUE;
            Iterator<C6952> it = this.f20223.iterator();
            while (true) {
                if (!it.hasNext()) {
                    z = false;
                    break;
                }
                AbstractC6950 abstractC69502 = it.next().f20212.get(0);
                long jMax = Math.max(0L, abstractC69502.f20206 - jMo10195);
                if (jMax > 0) {
                    jMin = Math.min(jMax, jMin);
                } else {
                    if (abstractC6950 != null) {
                        z = true;
                        break;
                    }
                    abstractC6950 = abstractC69502;
                }
            }
            if (abstractC6950 != null) {
                if (C6943.f20196 && !Thread.holdsLock(this)) {
                    StringBuilder sbM48892 = C2803.m4889("Thread ");
                    Thread threadCurrentThread2 = Thread.currentThread();
                    C6635.m9823((Object) threadCurrentThread2, "Thread.currentThread()");
                    sbM48892.append(threadCurrentThread2.getName());
                    sbM48892.append(" MUST hold lock on ");
                    sbM48892.append(this);
                    throw new AssertionError(sbM48892.toString());
                }
                abstractC6950.f20206 = -1L;
                C6952 c6952 = abstractC6950.f20205;
                if (c6952 == null) {
                    C6635.m9822();
                    throw null;
                }
                c6952.f20212.remove(abstractC6950);
                this.f20223.remove(c6952);
                c6952.f20211 = abstractC6950;
                this.f20222.add(c6952);
                if (z || (!this.f20220 && (!this.f20223.isEmpty()))) {
                    this.f20225.execute(this.f20224);
                }
                return abstractC6950;
            }
            if (this.f20220) {
                if (jMin < this.f20221 - jMo10195) {
                    this.f20225.mo10196(this);
                }
                return null;
            }
            this.f20220 = true;
            this.f20221 = jMo10195 + jMin;
            try {
                try {
                    this.f20225.mo10197(this, jMin);
                } catch (InterruptedException unused) {
                    m10193();
                }
            } finally {
                this.f20220 = false;
            }
        }
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final void m10190(AbstractC6950 abstractC6950) {
        if (C6943.f20196 && Thread.holdsLock(this)) {
            StringBuilder sbM4889 = C2803.m4889("Thread ");
            Thread threadCurrentThread = Thread.currentThread();
            C6635.m9823((Object) threadCurrentThread, "Thread.currentThread()");
            sbM4889.append(threadCurrentThread.getName());
            sbM4889.append(" MUST NOT hold lock on ");
            sbM4889.append(this);
            throw new AssertionError(sbM4889.toString());
        }
        Thread threadCurrentThread2 = Thread.currentThread();
        C6635.m9823((Object) threadCurrentThread2, "currentThread");
        String name = threadCurrentThread2.getName();
        threadCurrentThread2.setName(abstractC6950.f20207);
        try {
            long jMo10183 = abstractC6950.mo10183();
            synchronized (this) {
                m10191(abstractC6950, jMo10183);
            }
            threadCurrentThread2.setName(name);
        } catch (Throwable th) {
            synchronized (this) {
                m10191(abstractC6950, -1L);
                threadCurrentThread2.setName(name);
                throw th;
            }
        }
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final void m10191(AbstractC6950 abstractC6950, long j) {
        if (C6943.f20196 && !Thread.holdsLock(this)) {
            StringBuilder sbM4889 = C2803.m4889("Thread ");
            Thread threadCurrentThread = Thread.currentThread();
            C6635.m9823((Object) threadCurrentThread, "Thread.currentThread()");
            sbM4889.append(threadCurrentThread.getName());
            sbM4889.append(" MUST hold lock on ");
            sbM4889.append(this);
            throw new AssertionError(sbM4889.toString());
        }
        C6952 c6952 = abstractC6950.f20205;
        if (c6952 == null) {
            C6635.m9822();
            throw null;
        }
        if (!(c6952.f20211 == abstractC6950)) {
            throw new IllegalStateException("Check failed.".toString());
        }
        boolean z = c6952.f20213;
        c6952.f20213 = false;
        c6952.f20211 = null;
        this.f20222.remove(c6952);
        if (j != -1 && !z && !c6952.f20210) {
            c6952.m10186(abstractC6950, j, true);
        }
        if (!c6952.f20212.isEmpty()) {
            this.f20223.add(c6952);
        }
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final void m10192(C6952 c6952) {
        if (c6952 == null) {
            C6635.m9824("taskQueue");
            throw null;
        }
        if (C6943.f20196 && !Thread.holdsLock(this)) {
            StringBuilder sbM4889 = C2803.m4889("Thread ");
            Thread threadCurrentThread = Thread.currentThread();
            C6635.m9823((Object) threadCurrentThread, "Thread.currentThread()");
            sbM4889.append(threadCurrentThread.getName());
            sbM4889.append(" MUST hold lock on ");
            sbM4889.append(this);
            throw new AssertionError(sbM4889.toString());
        }
        if (c6952.f20211 == null) {
            if (!c6952.f20212.isEmpty()) {
                List<C6952> list = this.f20223;
                if (list == null) {
                    C6635.m9824("$this$addIfAbsent");
                    throw null;
                }
                if (!list.contains(c6952)) {
                    list.add(c6952);
                }
            } else {
                this.f20223.remove(c6952);
            }
        }
        if (this.f20220) {
            this.f20225.mo10196(this);
        } else {
            this.f20225.execute(this.f20224);
        }
    }

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public final void m10193() {
        for (int size = this.f20222.size() - 1; size >= 0; size--) {
            this.f20222.get(size).m10187();
        }
        for (int size2 = this.f20223.size() - 1; size2 >= 0; size2--) {
            C6952 c6952 = this.f20223.get(size2);
            c6952.m10187();
            if (c6952.f20212.isEmpty()) {
                this.f20223.remove(size2);
            }
        }
    }

    /* JADX INFO: renamed from: ހ, reason: contains not printable characters */
    public final C6952 m10194() {
        int i;
        synchronized (this) {
            i = this.f20219;
            this.f20219 = i + 1;
        }
        StringBuilder sb = new StringBuilder();
        sb.append('Q');
        sb.append(i);
        return new C6952(this, sb.toString());
    }
}
