- 28,932 files - Full Java source code - Smali files - Resources Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
129 lines
5.4 KiB
Java
129 lines
5.4 KiB
Java
package kotlinx.coroutines.internal;
|
|
|
|
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
|
|
import kotlin.coroutines.CoroutineContext;
|
|
import kotlin.coroutines.EmptyCoroutineContext;
|
|
import kotlinx.coroutines.CancellableContinuation;
|
|
import kotlinx.coroutines.CoroutineDispatcher;
|
|
import kotlinx.coroutines.CoroutineExceptionHandlerKt;
|
|
import kotlinx.coroutines.DefaultExecutorKt;
|
|
import kotlinx.coroutines.Delay;
|
|
import kotlinx.coroutines.DisposableHandle;
|
|
|
|
/* loaded from: classes5.dex */
|
|
public final class LimitedDispatcher extends CoroutineDispatcher implements Delay {
|
|
public static final AtomicIntegerFieldUpdater runningWorkers$FU = AtomicIntegerFieldUpdater.newUpdater(LimitedDispatcher.class, "runningWorkers");
|
|
public final /* synthetic */ Delay $$delegate_0;
|
|
public final CoroutineDispatcher dispatcher;
|
|
public final int parallelism;
|
|
public final LockFreeTaskQueue queue;
|
|
private volatile int runningWorkers;
|
|
public final Object workerAllocationLock;
|
|
|
|
@Override // kotlinx.coroutines.Delay
|
|
public DisposableHandle invokeOnTimeout(long j, Runnable runnable, CoroutineContext coroutineContext) {
|
|
return this.$$delegate_0.invokeOnTimeout(j, runnable, coroutineContext);
|
|
}
|
|
|
|
@Override // kotlinx.coroutines.Delay
|
|
/* renamed from: scheduleResumeAfterDelay */
|
|
public void mo4149scheduleResumeAfterDelay(long j, CancellableContinuation cancellableContinuation) {
|
|
this.$$delegate_0.mo4149scheduleResumeAfterDelay(j, cancellableContinuation);
|
|
}
|
|
|
|
public final boolean tryAllocateWorker() {
|
|
synchronized (this.workerAllocationLock) {
|
|
AtomicIntegerFieldUpdater atomicIntegerFieldUpdater = runningWorkers$FU;
|
|
if (atomicIntegerFieldUpdater.get(this) >= this.parallelism) {
|
|
return false;
|
|
}
|
|
atomicIntegerFieldUpdater.incrementAndGet(this);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
/* JADX WARN: Multi-variable type inference failed */
|
|
public LimitedDispatcher(CoroutineDispatcher coroutineDispatcher, int i) {
|
|
this.dispatcher = coroutineDispatcher;
|
|
this.parallelism = i;
|
|
Delay delay = coroutineDispatcher instanceof Delay ? (Delay) coroutineDispatcher : null;
|
|
this.$$delegate_0 = delay == null ? DefaultExecutorKt.getDefaultDelay() : delay;
|
|
this.queue = new LockFreeTaskQueue(false);
|
|
this.workerAllocationLock = new Object();
|
|
}
|
|
|
|
@Override // kotlinx.coroutines.CoroutineDispatcher
|
|
public CoroutineDispatcher limitedParallelism(int i) {
|
|
LimitedDispatcherKt.checkParallelism(i);
|
|
return i >= this.parallelism ? this : super.limitedParallelism(i);
|
|
}
|
|
|
|
@Override // kotlinx.coroutines.CoroutineDispatcher
|
|
/* renamed from: dispatch */
|
|
public void mo4148dispatch(CoroutineContext coroutineContext, Runnable runnable) {
|
|
Runnable obtainTaskOrDeallocateWorker;
|
|
this.queue.addLast(runnable);
|
|
if (runningWorkers$FU.get(this) >= this.parallelism || !tryAllocateWorker() || (obtainTaskOrDeallocateWorker = obtainTaskOrDeallocateWorker()) == null) {
|
|
return;
|
|
}
|
|
this.dispatcher.mo4148dispatch(this, new Worker(obtainTaskOrDeallocateWorker));
|
|
}
|
|
|
|
@Override // kotlinx.coroutines.CoroutineDispatcher
|
|
public void dispatchYield(CoroutineContext coroutineContext, Runnable runnable) {
|
|
Runnable obtainTaskOrDeallocateWorker;
|
|
this.queue.addLast(runnable);
|
|
if (runningWorkers$FU.get(this) >= this.parallelism || !tryAllocateWorker() || (obtainTaskOrDeallocateWorker = obtainTaskOrDeallocateWorker()) == null) {
|
|
return;
|
|
}
|
|
this.dispatcher.dispatchYield(this, new Worker(obtainTaskOrDeallocateWorker));
|
|
}
|
|
|
|
public final Runnable obtainTaskOrDeallocateWorker() {
|
|
while (true) {
|
|
Runnable runnable = (Runnable) this.queue.removeFirstOrNull();
|
|
if (runnable != null) {
|
|
return runnable;
|
|
}
|
|
synchronized (this.workerAllocationLock) {
|
|
AtomicIntegerFieldUpdater atomicIntegerFieldUpdater = runningWorkers$FU;
|
|
atomicIntegerFieldUpdater.decrementAndGet(this);
|
|
if (this.queue.getSize() == 0) {
|
|
return null;
|
|
}
|
|
atomicIntegerFieldUpdater.incrementAndGet(this);
|
|
}
|
|
}
|
|
}
|
|
|
|
public final class Worker implements Runnable {
|
|
public Runnable currentTask;
|
|
|
|
public Worker(Runnable runnable) {
|
|
this.currentTask = runnable;
|
|
}
|
|
|
|
@Override // java.lang.Runnable
|
|
public void run() {
|
|
int i = 0;
|
|
while (true) {
|
|
try {
|
|
this.currentTask.run();
|
|
} catch (Throwable th) {
|
|
CoroutineExceptionHandlerKt.handleCoroutineException(EmptyCoroutineContext.INSTANCE, th);
|
|
}
|
|
Runnable obtainTaskOrDeallocateWorker = LimitedDispatcher.this.obtainTaskOrDeallocateWorker();
|
|
if (obtainTaskOrDeallocateWorker == null) {
|
|
return;
|
|
}
|
|
this.currentTask = obtainTaskOrDeallocateWorker;
|
|
i++;
|
|
if (i >= 16 && LimitedDispatcher.this.dispatcher.isDispatchNeeded(LimitedDispatcher.this)) {
|
|
LimitedDispatcher.this.dispatcher.mo4148dispatch(LimitedDispatcher.this, this);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|