|
| 1 | +/* |
| 2 | + * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
| 3 | + */ |
| 4 | + |
| 5 | +package kotlinx.coroutines.internal |
| 6 | + |
| 7 | +import kotlinx.coroutines.* |
| 8 | +import kotlin.coroutines.* |
| 9 | +import kotlin.jvm.* |
| 10 | + |
| 11 | +/** |
| 12 | + * The result of .limitedParallelism(x) call, dispatcher |
| 13 | + * that wraps the given dispatcher, but limits the parallelism level, while |
| 14 | + * trying to emulate fairness. |
| 15 | + */ |
| 16 | +internal class LimitedDispatcher( |
| 17 | + private val dispatcher: CoroutineDispatcher, |
| 18 | + private val parallelism: Int |
| 19 | +) : CoroutineDispatcher(), Runnable, Delay by (dispatcher as? Delay ?: DefaultDelay) { |
| 20 | + |
| 21 | + @Volatile |
| 22 | + private var runningWorkers = 0 |
| 23 | + |
| 24 | + private val queue = LockFreeTaskQueue<Runnable>(singleConsumer = false) |
| 25 | + |
| 26 | + @InternalCoroutinesApi |
| 27 | + override fun dispatchYield(context: CoroutineContext, block: Runnable) { |
| 28 | + dispatcher.dispatchYield(context, block) |
| 29 | + } |
| 30 | + |
| 31 | + override fun run() { |
| 32 | + var fairnessCounter = 0 |
| 33 | + while (true) { |
| 34 | + val task = queue.removeFirstOrNull() |
| 35 | + if (task != null) { |
| 36 | + task.run() |
| 37 | + // 16 is our out-of-thin-air constant to emulate fairness. Used in JS dispatchers as well |
| 38 | + if (++fairnessCounter >= 16 && dispatcher.isDispatchNeeded(EmptyCoroutineContext)) { |
| 39 | + // Do "yield" to let other views to execute their runnable as well |
| 40 | + // Note that we do not decrement 'runningWorkers' as we still committed to do our part of work |
| 41 | + dispatcher.dispatch(EmptyCoroutineContext, this) |
| 42 | + return |
| 43 | + } |
| 44 | + continue |
| 45 | + } |
| 46 | + |
| 47 | + @Suppress("CAST_NEVER_SUCCEEDS") |
| 48 | + synchronized(this as SynchronizedObject) { |
| 49 | + --runningWorkers |
| 50 | + if (queue.size == 0) return |
| 51 | + ++runningWorkers |
| 52 | + fairnessCounter = 0 |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + override fun dispatch(context: CoroutineContext, block: Runnable) { |
| 58 | + // Add task to queue so running workers will be able to see that |
| 59 | + queue.addLast(block) |
| 60 | + if (runningWorkers >= parallelism) { |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + /* |
| 65 | + * Protect against race when the worker is finished |
| 66 | + * right after our check |
| 67 | + */ |
| 68 | + @Suppress("CAST_NEVER_SUCCEEDS") |
| 69 | + synchronized(this as SynchronizedObject) { |
| 70 | + if (runningWorkers >= parallelism) return |
| 71 | + ++runningWorkers |
| 72 | + } |
| 73 | + if (dispatcher.isDispatchNeeded(EmptyCoroutineContext)) { |
| 74 | + dispatcher.dispatch(EmptyCoroutineContext, this) |
| 75 | + } else { |
| 76 | + run() |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +// Save a few bytecode ops |
| 82 | +internal fun Int.checkParallelism() = require(this >= 1) { "Expected positive parallelism level, but got $this" } |
0 commit comments