package kotlinx.coroutines; import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.LockSupport; import kotlin.coroutines.CoroutineContext; import kotlin.jvm.internal.Intrinsics; import kotlin.ranges.RangesKt___RangesKt; import kotlinx.coroutines.EventLoopImplBase; /* loaded from: classes5.dex */ public final class DefaultExecutor extends EventLoopImplBase implements Runnable { public static final DefaultExecutor INSTANCE; public static final long KEEP_ALIVE_NANOS; private static volatile Thread _thread; private static volatile int debugStatus; public final boolean isShutDown() { return debugStatus == 4; } public final boolean isShutdownRequested() { int i = debugStatus; return i == 2 || i == 3; } static { Long l; DefaultExecutor defaultExecutor = new DefaultExecutor(); INSTANCE = defaultExecutor; EventLoop.incrementUseCount$default(defaultExecutor, false, 1, null); TimeUnit timeUnit = TimeUnit.MILLISECONDS; try { l = Long.getLong("kotlinx.coroutines.DefaultExecutor.keepAlive", 1000L); } catch (SecurityException unused) { l = 1000L; } KEEP_ALIVE_NANOS = timeUnit.toNanos(l.longValue()); } @Override // kotlinx.coroutines.EventLoopImplPlatform public Thread getThread() { Thread thread = _thread; return thread == null ? createThreadSync() : thread; } @Override // kotlinx.coroutines.EventLoopImplBase public void enqueue(Runnable runnable) { if (isShutDown()) { shutdownError(); } super.enqueue(runnable); } @Override // kotlinx.coroutines.EventLoopImplPlatform public void reschedule(long j, EventLoopImplBase.DelayedTask delayedTask) { shutdownError(); } public final void shutdownError() { 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.EventLoopImplBase, kotlinx.coroutines.EventLoop public void shutdown() { debugStatus = 4; super.shutdown(); } @Override // kotlinx.coroutines.EventLoopImplBase, kotlinx.coroutines.Delay public DisposableHandle invokeOnTimeout(long j, Runnable runnable, CoroutineContext coroutineContext) { return scheduleInvokeOnTimeout(j, runnable); } @Override // java.lang.Runnable public void run() { boolean isEmpty; ThreadLocalEventLoop.INSTANCE.setEventLoop$kotlinx_coroutines_core(this); AbstractTimeSourceKt.getTimeSource(); try { if (!notifyStartup()) { if (isEmpty) { return; } else { return; } } long j = Long.MAX_VALUE; while (true) { Thread.interrupted(); long processNextEvent = processNextEvent(); if (processNextEvent == Long.MAX_VALUE) { AbstractTimeSourceKt.getTimeSource(); long nanoTime = System.nanoTime(); if (j == Long.MAX_VALUE) { j = KEEP_ALIVE_NANOS + nanoTime; } long j2 = j - nanoTime; if (j2 <= 0) { _thread = null; acknowledgeShutdownIfNeeded(); AbstractTimeSourceKt.getTimeSource(); if (isEmpty()) { return; } getThread(); return; } processNextEvent = RangesKt___RangesKt.coerceAtMost(processNextEvent, j2); } else { j = Long.MAX_VALUE; } if (processNextEvent > 0) { if (isShutdownRequested()) { _thread = null; acknowledgeShutdownIfNeeded(); AbstractTimeSourceKt.getTimeSource(); if (isEmpty()) { return; } getThread(); return; } AbstractTimeSourceKt.getTimeSource(); LockSupport.parkNanos(this, processNextEvent); } } } finally { _thread = null; acknowledgeShutdownIfNeeded(); AbstractTimeSourceKt.getTimeSource(); if (!isEmpty()) { getThread(); } } } public final synchronized Thread createThreadSync() { Thread thread; thread = _thread; if (thread == null) { thread = new Thread(this, "kotlinx.coroutines.DefaultExecutor"); _thread = thread; thread.setDaemon(true); thread.start(); } return thread; } public final synchronized boolean notifyStartup() { if (isShutdownRequested()) { return false; } debugStatus = 1; Intrinsics.checkNotNull(this, "null cannot be cast to non-null type java.lang.Object"); notifyAll(); return true; } public final synchronized void acknowledgeShutdownIfNeeded() { if (isShutdownRequested()) { debugStatus = 3; resetAll(); Intrinsics.checkNotNull(this, "null cannot be cast to non-null type java.lang.Object"); notifyAll(); } } }