diff options
Diffstat (limited to 'kotlinx-coroutines-core/jvm/src/scheduling/CoroutineScheduler.kt')
-rw-r--r-- | kotlinx-coroutines-core/jvm/src/scheduling/CoroutineScheduler.kt | 30 |
1 files changed, 9 insertions, 21 deletions
diff --git a/kotlinx-coroutines-core/jvm/src/scheduling/CoroutineScheduler.kt b/kotlinx-coroutines-core/jvm/src/scheduling/CoroutineScheduler.kt index 84d9d9f8..62cf80f7 100644 --- a/kotlinx-coroutines-core/jvm/src/scheduling/CoroutineScheduler.kt +++ b/kotlinx-coroutines-core/jvm/src/scheduling/CoroutineScheduler.kt @@ -1,5 +1,5 @@ /* - * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. */ package kotlinx.coroutines.scheduling @@ -146,7 +146,7 @@ internal class CoroutineScheduler( * * Note, [newIndex] can be zero for the worker that is being terminated (removed from [workers]). */ - fun parkedWorkersStackTopUpdate(worker: Worker, oldIndex: Int, newIndex: Int) { + internal fun parkedWorkersStackTopUpdate(worker: Worker, oldIndex: Int, newIndex: Int) { parkedWorkersStack.loop { top -> val index = (top and PARKED_INDEX_MASK).toInt() val updVersion = (top + PARKED_VERSION_INC) and PARKED_VERSION_MASK @@ -169,12 +169,12 @@ internal class CoroutineScheduler( * It does nothing is this worker is already physically linked to the stack. * This method is invoked only from the worker thread itself. * This invocation always precedes [LockSupport.parkNanos]. - * See [Worker.tryPark]. + * See [Worker.doPark]. * * Returns `true` if worker was added to the stack by this invocation, `false` if it was already * registered in the stack. */ - fun parkedWorkersStackPush(worker: Worker): Boolean { + internal fun parkedWorkersStackPush(worker: Worker): Boolean { if (worker.nextParkedWorker !== NOT_IN_STACK) return false // already in stack, bail out /* * The below loop can be entered only if this worker was not in the stack and, since no other thread @@ -403,7 +403,7 @@ internal class CoroutineScheduler( } } - fun createTask(block: Runnable, taskContext: TaskContext): Task { + internal fun createTask(block: Runnable, taskContext: TaskContext): Task { val nanoTime = schedulerTimeSource.nanoTime() if (block is Task) { block.submissionTime = nanoTime @@ -422,7 +422,7 @@ internal class CoroutineScheduler( tryUnpark() // Try unpark again in case there was race between permit release and parking } - fun signalCpuWork() { + internal fun signalCpuWork() { if (tryUnpark()) return if (tryCreateWorker()) return tryUnpark() @@ -654,7 +654,7 @@ internal class CoroutineScheduler( * Releases CPU token if worker has any and changes state to [newState]. * Returns `true` if CPU permit was returned to the pool */ - fun tryReleaseCpu(newState: WorkerState): Boolean { + internal fun tryReleaseCpu(newState: WorkerState): Boolean { val previousState = state val hadCpu = previousState == WorkerState.CPU_ACQUIRED if (hadCpu) releaseCpuPermit() @@ -721,19 +721,7 @@ internal class CoroutineScheduler( } assert { localQueue.size == 0 } workerCtl.value = PARKED // Update value once - /* - * inStack() prevents spurious wakeups, while workerCtl.value == PARKED - * prevents the following race: - * - * - T2 scans the queue, adds itself to the stack, goes to rescan - * - T2 suspends in 'workerCtl.value = PARKED' line - * - T1 pops T2 from the stack, claims workerCtl, suspends - * - T2 fails 'while (inStack())' check, goes to full rescan - * - T2 adds itself to the stack, parks - * - T1 unparks T2, bails out with success - * - T2 unparks and loops in 'while (inStack())' - */ - while (inStack() && workerCtl.value == PARKED) { // Prevent spurious wakeups + while (inStack()) { // Prevent spurious wakeups if (isTerminated || state == WorkerState.TERMINATED) break tryReleaseCpu(WorkerState.PARKING) interrupted() // Cleanup interruptions @@ -774,7 +762,7 @@ internal class CoroutineScheduler( * Marsaglia xorshift RNG with period 2^32-1 for work stealing purposes. * ThreadLocalRandom cannot be used to support Android and ThreadLocal<Random> is up to 15% slower on Ktor benchmarks */ - fun nextInt(upperBound: Int): Int { + internal fun nextInt(upperBound: Int): Int { var r = rngState r = r xor (r shl 13) r = r xor (r shr 17) |