Roman Elizarov | f16fd27 | 2017-02-07 11:26:00 +0300 | [diff] [blame] | 1 | /* |
Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame^] | 2 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Roman Elizarov | f16fd27 | 2017-02-07 11:26:00 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 5 | package kotlinx.coroutines.experimental |
| 6 | |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 7 | import kotlinx.coroutines.experimental.timeunit.TimeUnit |
Roman Elizarov | 1a016bd | 2017-07-12 11:41:34 +0300 | [diff] [blame] | 8 | import java.util.concurrent.* |
Vsevolod Tolstopyatov | 87f2faa | 2018-04-30 22:53:02 +0300 | [diff] [blame] | 9 | import java.util.concurrent.atomic.* |
| 10 | import kotlin.coroutines.experimental.* |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 11 | |
| 12 | /** |
Roman Elizarov | ed7b864 | 2017-01-19 11:22:28 +0300 | [diff] [blame] | 13 | * Represents common pool of shared threads as coroutine dispatcher for compute-intensive tasks. |
Nikolay Metchev | c1c380c | 2018-02-11 13:13:08 +0000 | [diff] [blame] | 14 | * |
| 15 | * If there isn't a SecurityManager present it uses [java.util.concurrent.ForkJoinPool] when available, which implements |
| 16 | * efficient work-stealing algorithm for its queues, so every coroutine resumption is dispatched as a separate task even |
| 17 | * when it already executes inside the pool. When available, it wraps `ForkJoinPool.commonPool` and provides a similar |
| 18 | * shared pool where not. |
| 19 | * |
| 20 | * If there is a SecurityManager present (as would be if running inside a Java Web Start context) then a plain thread |
| 21 | * pool is created. This is to work around the fact that ForkJoinPool creates threads that cannot perform |
| 22 | * privileged actions. |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 23 | */ |
Roman Elizarov | 67891d8 | 2017-01-23 16:47:20 +0300 | [diff] [blame] | 24 | object CommonPool : CoroutineDispatcher() { |
Vsevolod Tolstopyatov | 6a0ce76 | 2018-05-22 19:47:50 +0300 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * Name of the property that controls default parallelism level of [CommonPool]. |
| 28 | * If the property is not specified, `Runtime.getRuntime().availableProcessors() - 1` will be used instead (or `1` for single-core JVM). |
| 29 | * Note that until Java 10, if an application is run within a container, |
paolop | e06b6ca | 2018-06-09 09:48:44 +0000 | [diff] [blame] | 30 | * `Runtime.getRuntime().availableProcessors()` is not aware of container constraints and will return the real number of cores. |
Vsevolod Tolstopyatov | 6a0ce76 | 2018-05-22 19:47:50 +0300 | [diff] [blame] | 31 | */ |
| 32 | public const val DEFAULT_PARALLELISM_PROPERTY_NAME = "kotlinx.coroutines.default.parallelism" |
| 33 | |
| 34 | private val parallelism = run<Int> { |
| 35 | val property = Try { System.getProperty(DEFAULT_PARALLELISM_PROPERTY_NAME) } |
| 36 | if (property == null) { |
| 37 | (Runtime.getRuntime().availableProcessors() - 1).coerceAtLeast(1) |
| 38 | } else { |
| 39 | val parallelism = property.toIntOrNull() |
| 40 | if (parallelism == null || parallelism < 1) { |
| 41 | error("Expected positive number in $DEFAULT_PARALLELISM_PROPERTY_NAME, but has $property") |
| 42 | } |
| 43 | parallelism |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | // For debug and tests |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 48 | private var usePrivatePool = false |
| 49 | |
| 50 | @Volatile |
Vsevolod Tolstopyatov | 6a0ce76 | 2018-05-22 19:47:50 +0300 | [diff] [blame] | 51 | private var pool: Executor? = null |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 52 | |
| 53 | private inline fun <T> Try(block: () -> T) = try { block() } catch (e: Throwable) { null } |
| 54 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 55 | private fun createPool(): ExecutorService { |
Nikolay Metchev | c1c380c | 2018-02-11 13:13:08 +0000 | [diff] [blame] | 56 | if (System.getSecurityManager() != null) return createPlainPool() |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 57 | val fjpClass = Try { Class.forName("java.util.concurrent.ForkJoinPool") } |
| 58 | ?: return createPlainPool() |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 59 | if (!usePrivatePool) { |
| 60 | Try { fjpClass.getMethod("commonPool")?.invoke(null) as? ExecutorService } |
| 61 | ?.let { return it } |
| 62 | } |
Vsevolod Tolstopyatov | 6a0ce76 | 2018-05-22 19:47:50 +0300 | [diff] [blame] | 63 | Try { fjpClass.getConstructor(Int::class.java).newInstance(parallelism) as? ExecutorService } |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 64 | ?. let { return it } |
| 65 | return createPlainPool() |
| 66 | } |
| 67 | |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 68 | private fun createPlainPool(): ExecutorService { |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 69 | val threadId = AtomicInteger() |
Vsevolod Tolstopyatov | 6a0ce76 | 2018-05-22 19:47:50 +0300 | [diff] [blame] | 70 | return Executors.newFixedThreadPool(parallelism) { |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 71 | Thread(it, "CommonPool-worker-${threadId.incrementAndGet()}").apply { isDaemon = true } |
| 72 | } |
| 73 | } |
| 74 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 75 | @Synchronized |
Roman Elizarov | 1a016bd | 2017-07-12 11:41:34 +0300 | [diff] [blame] | 76 | private fun getOrCreatePoolSync(): Executor = |
Vsevolod Tolstopyatov | 6a0ce76 | 2018-05-22 19:47:50 +0300 | [diff] [blame] | 77 | pool ?: createPool().also { pool = it } |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 78 | |
| 79 | override fun dispatch(context: CoroutineContext, block: Runnable) = |
Vsevolod Tolstopyatov | 6a0ce76 | 2018-05-22 19:47:50 +0300 | [diff] [blame] | 80 | try { (pool ?: getOrCreatePoolSync()).execute(timeSource.trackTask(block)) } |
Roman Elizarov | 35d2c34 | 2017-07-20 14:54:39 +0300 | [diff] [blame] | 81 | catch (e: RejectedExecutionException) { |
| 82 | timeSource.unTrackTask() |
| 83 | DefaultExecutor.execute(block) |
| 84 | } |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 85 | |
| 86 | // used for tests |
| 87 | @Synchronized |
| 88 | internal fun usePrivatePool() { |
Roman Elizarov | 1a016bd | 2017-07-12 11:41:34 +0300 | [diff] [blame] | 89 | shutdown(0) |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 90 | usePrivatePool = true |
Vsevolod Tolstopyatov | 6a0ce76 | 2018-05-22 19:47:50 +0300 | [diff] [blame] | 91 | pool = null |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | // used for tests |
| 95 | @Synchronized |
Roman Elizarov | 1a016bd | 2017-07-12 11:41:34 +0300 | [diff] [blame] | 96 | internal fun shutdown(timeout: Long) { |
Vsevolod Tolstopyatov | 6a0ce76 | 2018-05-22 19:47:50 +0300 | [diff] [blame] | 97 | (pool as? ExecutorService)?.apply { |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 98 | shutdown() |
| 99 | if (timeout > 0) |
| 100 | awaitTermination(timeout, TimeUnit.MILLISECONDS) |
Roman Elizarov | 35d2c34 | 2017-07-20 14:54:39 +0300 | [diff] [blame] | 101 | shutdownNow().forEach { DefaultExecutor.execute(it) } |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 102 | } |
Vsevolod Tolstopyatov | 6a0ce76 | 2018-05-22 19:47:50 +0300 | [diff] [blame] | 103 | pool = Executor { throw RejectedExecutionException("CommonPool was shutdown") } |
Roman Elizarov | 1a016bd | 2017-07-12 11:41:34 +0300 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | // used for tests |
| 107 | @Synchronized |
| 108 | internal fun restore() { |
| 109 | shutdown(0) |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 110 | usePrivatePool = false |
Vsevolod Tolstopyatov | 6a0ce76 | 2018-05-22 19:47:50 +0300 | [diff] [blame] | 111 | pool = null |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 112 | } |
Roman Elizarov | dc9fd1c | 2017-04-07 10:35:28 +0300 | [diff] [blame] | 113 | |
| 114 | override fun toString(): String = "CommonPool" |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 115 | } |