package kotlinx.coroutines;

import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;
import org.jetbrains.annotations.Nullable;

/* JADX INFO: loaded from: classes.dex */
public final class z extends p0 implements Runnable {

    @Nullable
    private static volatile Thread _thread;
    private static volatile int debugStatus;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public static final z f29593j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public static final long f29594k;

    static {
        Long l10;
        z zVar = new z();
        f29593j = zVar;
        zVar.Q(false);
        TimeUnit timeUnit = TimeUnit.MILLISECONDS;
        try {
            l10 = Long.getLong("kotlinx.coroutines.DefaultExecutor.keepAlive", 1000L);
        } catch (SecurityException unused) {
            l10 = 1000L;
        }
        f29594k = timeUnit.toNanos(l10.longValue());
    }

    @Override // kotlinx.coroutines.q0
    public final Thread P() {
        Thread thread = _thread;
        if (thread == null) {
            synchronized (this) {
                thread = _thread;
                if (thread == null) {
                    thread = new Thread(this, "kotlinx.coroutines.DefaultExecutor");
                    _thread = thread;
                    thread.setContextClassLoader(z.class.getClassLoader());
                    thread.setDaemon(true);
                    thread.start();
                }
            }
        }
        return thread;
    }

    @Override // kotlinx.coroutines.q0
    public final void T(long j10, n0 n0Var) {
        throw new RejectedExecutionException("DefaultExecutor was shut down. This error indicates that Dispatchers.shutdown() was invoked prior to completion of exiting coroutines, leaving coroutines in incomplete state. Please refer to Dispatchers.shutdown documentation for more details");
    }

    @Override // kotlinx.coroutines.p0
    public final void U(Runnable runnable) {
        if (debugStatus == 4) {
            throw new RejectedExecutionException("DefaultExecutor was shut down. This error indicates that Dispatchers.shutdown() was invoked prior to completion of exiting coroutines, leaving coroutines in incomplete state. Please refer to Dispatchers.shutdown documentation for more details");
        }
        super.U(runnable);
    }

    public final synchronized void Y() {
        int i10 = debugStatus;
        if (i10 == 2 || i10 == 3) {
            debugStatus = 3;
            p0.f29519g.set(this, null);
            p0.f29520h.set(this, null);
            notifyAll();
        }
    }

    @Override // kotlinx.coroutines.p0, kotlinx.coroutines.d0
    public final j0 k(long j10, r1 r1Var, kotlin.coroutines.h hVar) {
        long j11 = j10 > 0 ? j10 >= 9223372036854L ? Long.MAX_VALUE : 1000000 * j10 : 0L;
        if (j11 >= 4611686018427387903L) {
            return k1.f29507a;
        }
        long jNanoTime = System.nanoTime();
        m0 m0Var = new m0(r1Var, j11 + jNanoTime);
        X(jNanoTime, m0Var);
        return m0Var;
    }

    /* JADX WARN: Code restructure failed: missing block: B:41:0x0072, code lost:
    
        return;
     */
    /* JADX WARN: Code restructure failed: missing block: B:66:?, code lost:
    
        return;
     */
    @Override // java.lang.Runnable
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void run() {
        q1.f29528a.set(this);
        try {
            synchronized (this) {
                int i10 = debugStatus;
                if (i10 != 2 && i10 != 3) {
                    debugStatus = 1;
                    notifyAll();
                    long j10 = Long.MAX_VALUE;
                    while (true) {
                        Thread.interrupted();
                        long jR = R();
                        if (jR == Long.MAX_VALUE) {
                            long jNanoTime = System.nanoTime();
                            if (j10 == Long.MAX_VALUE) {
                                j10 = f29594k + jNanoTime;
                            }
                            long j11 = j10 - jNanoTime;
                            if (j11 <= 0) {
                                _thread = null;
                                Y();
                                if (W()) {
                                    return;
                                }
                                P();
                                return;
                            }
                            if (jR > j11) {
                                jR = j11;
                            }
                        } else {
                            j10 = Long.MAX_VALUE;
                        }
                        if (jR > 0) {
                            int i11 = debugStatus;
                            if (i11 == 2 || i11 == 3) {
                                break;
                            } else {
                                LockSupport.parkNanos(this, jR);
                            }
                        }
                    }
                }
                _thread = null;
                Y();
                if (W()) {
                    return;
                }
                P();
            }
        } finally {
            _thread = null;
            Y();
            if (!W()) {
                P();
            }
        }
    }

    @Override // kotlinx.coroutines.p0, kotlinx.coroutines.q0
    public final void shutdown() {
        debugStatus = 4;
        super.shutdown();
    }
}
