• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package kotlinx.coroutines.scheduling
2 
3 import kotlinx.coroutines.*
4 import kotlinx.coroutines.internal.*
5 import java.util.concurrent.*
6 
7 
8 /**
9  * The name of the default scheduler. The names of the worker threads of [Dispatchers.Default] have it as their prefix.
10  */
11 @JvmField
12 internal val DEFAULT_SCHEDULER_NAME = systemProp(
13     "kotlinx.coroutines.scheduler.default.name", "DefaultDispatcher"
14 )
15 
16 // 100us as default
17 @JvmField
18 internal val WORK_STEALING_TIME_RESOLUTION_NS = systemProp(
19     "kotlinx.coroutines.scheduler.resolution.ns", 100000L
20 )
21 
22 /**
23  * The maximum number of threads allocated for CPU-bound tasks at the default set of dispatchers.
24  *
25  * NOTE: we coerce default to at least two threads to give us chances that multi-threading problems
26  * get reproduced even on a single-core machine, but support explicit setting of 1 thread scheduler if needed
27  */
28 @JvmField
29 internal val CORE_POOL_SIZE = systemProp(
30     "kotlinx.coroutines.scheduler.core.pool.size",
31     AVAILABLE_PROCESSORS.coerceAtLeast(2),
32     minValue = CoroutineScheduler.MIN_SUPPORTED_POOL_SIZE
33 )
34 
35 /** The maximum number of threads allocated for blocking tasks at the default set of dispatchers. */
36 @JvmField
37 internal val MAX_POOL_SIZE = systemProp(
38     "kotlinx.coroutines.scheduler.max.pool.size",
39     CoroutineScheduler.MAX_SUPPORTED_POOL_SIZE,
40     maxValue = CoroutineScheduler.MAX_SUPPORTED_POOL_SIZE
41 )
42 
43 @JvmField
44 internal val IDLE_WORKER_KEEP_ALIVE_NS = TimeUnit.SECONDS.toNanos(
45     systemProp("kotlinx.coroutines.scheduler.keep.alive.sec", 60L)
46 )
47 
48 @JvmField
49 internal var schedulerTimeSource: SchedulerTimeSource = NanoTimeSource
50 
51 /**
52  * Concurrency context of a task.
53  *
54  * Currently, it only signifies whether the task is blocking or non-blocking.
55  */
56 internal typealias TaskContext = Boolean
57 
58 /**
59  * This would be [TaskContext.toString] if [TaskContext] was a proper class.
60  */
taskContextStringnull61 private fun taskContextString(taskContext: TaskContext): String = if (taskContext) "Blocking" else "Non-blocking"
62 
63 internal const val NonBlockingContext: TaskContext = false
64 
65 internal const val BlockingContext: TaskContext = true
66 
67 /**
68  * A scheduler task.
69  */
70 internal abstract class Task(
71     @JvmField var submissionTime: Long,
72     @JvmField var taskContext: TaskContext
73 ) : Runnable {
74     internal constructor() : this(0, NonBlockingContext)
75 }
76 
77 internal inline val Task.isBlocking get() = taskContext
78 
asTasknull79 internal fun Runnable.asTask(submissionTime: Long, taskContext: TaskContext): Task =
80     TaskImpl(this, submissionTime, taskContext)
81 
82 // Non-reusable Task implementation to wrap Runnable instances that do not otherwise implement task
83 private class TaskImpl(
84     @JvmField val block: Runnable,
85     submissionTime: Long,
86     taskContext: TaskContext
87 ) : Task(submissionTime, taskContext) {
88     override fun run() {
89         block.run()
90     }
91 
92     override fun toString(): String =
93         "Task[${block.classSimpleName}@${block.hexAddress}, $submissionTime, ${taskContextString(taskContext)}]"
94 }
95 
96 // Open for tests
97 internal class GlobalQueue : LockFreeTaskQueue<Task>(singleConsumer = false)
98 
99 // Was previously TimeSource, renamed due to KT-42625 and KT-23727
100 internal abstract class SchedulerTimeSource {
nanoTimenull101     abstract fun nanoTime(): Long
102 }
103 
104 internal object NanoTimeSource : SchedulerTimeSource() {
105     override fun nanoTime() = System.nanoTime()
106 }
107