Roman Elizarov | f16fd27 | 2017-02-07 11:26:00 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-2017 JetBrains s.r.o. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 17 | package kotlinx.coroutines.experimental |
| 18 | |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 19 | import kotlinx.coroutines.experimental.timeunit.TimeUnit |
Roman Elizarov | 1a016bd | 2017-07-12 11:41:34 +0300 | [diff] [blame] | 20 | import java.util.concurrent.* |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 21 | import java.util.concurrent.atomic.AtomicInteger |
Roman Elizarov | ea4a51b | 2017-01-31 12:01:25 +0300 | [diff] [blame] | 22 | import kotlin.coroutines.experimental.CoroutineContext |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 23 | |
| 24 | /** |
Roman Elizarov | ed7b864 | 2017-01-19 11:22:28 +0300 | [diff] [blame] | 25 | * 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] | 26 | * |
| 27 | * If there isn't a SecurityManager present it uses [java.util.concurrent.ForkJoinPool] when available, which implements |
| 28 | * efficient work-stealing algorithm for its queues, so every coroutine resumption is dispatched as a separate task even |
| 29 | * when it already executes inside the pool. When available, it wraps `ForkJoinPool.commonPool` and provides a similar |
| 30 | * shared pool where not. |
| 31 | * |
| 32 | * If there is a SecurityManager present (as would be if running inside a Java Web Start context) then a plain thread |
| 33 | * pool is created. This is to work around the fact that ForkJoinPool creates threads that cannot perform |
| 34 | * privileged actions. |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 35 | */ |
Roman Elizarov | 67891d8 | 2017-01-23 16:47:20 +0300 | [diff] [blame] | 36 | object CommonPool : CoroutineDispatcher() { |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 37 | private var usePrivatePool = false |
| 38 | |
| 39 | @Volatile |
Roman Elizarov | 1a016bd | 2017-07-12 11:41:34 +0300 | [diff] [blame] | 40 | private var _pool: Executor? = null |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 41 | |
| 42 | private inline fun <T> Try(block: () -> T) = try { block() } catch (e: Throwable) { null } |
| 43 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 44 | private fun createPool(): ExecutorService { |
Nikolay Metchev | c1c380c | 2018-02-11 13:13:08 +0000 | [diff] [blame] | 45 | if (System.getSecurityManager() != null) return createPlainPool() |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 46 | val fjpClass = Try { Class.forName("java.util.concurrent.ForkJoinPool") } |
| 47 | ?: return createPlainPool() |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 48 | if (!usePrivatePool) { |
| 49 | Try { fjpClass.getMethod("commonPool")?.invoke(null) as? ExecutorService } |
| 50 | ?.let { return it } |
| 51 | } |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 52 | Try { fjpClass.getConstructor(Int::class.java).newInstance(defaultParallelism()) as? ExecutorService } |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 53 | ?. let { return it } |
| 54 | return createPlainPool() |
| 55 | } |
| 56 | |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 57 | private fun createPlainPool(): ExecutorService { |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 58 | val threadId = AtomicInteger() |
| 59 | return Executors.newFixedThreadPool(defaultParallelism()) { |
| 60 | Thread(it, "CommonPool-worker-${threadId.incrementAndGet()}").apply { isDaemon = true } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | private fun defaultParallelism() = (Runtime.getRuntime().availableProcessors() - 1).coerceAtLeast(1) |
| 65 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 66 | @Synchronized |
Roman Elizarov | 1a016bd | 2017-07-12 11:41:34 +0300 | [diff] [blame] | 67 | private fun getOrCreatePoolSync(): Executor = |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 68 | _pool ?: createPool().also { _pool = it } |
| 69 | |
| 70 | override fun dispatch(context: CoroutineContext, block: Runnable) = |
Roman Elizarov | 35d2c34 | 2017-07-20 14:54:39 +0300 | [diff] [blame] | 71 | try { (_pool ?: getOrCreatePoolSync()).execute(timeSource.trackTask(block)) } |
| 72 | catch (e: RejectedExecutionException) { |
| 73 | timeSource.unTrackTask() |
| 74 | DefaultExecutor.execute(block) |
| 75 | } |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 76 | |
| 77 | // used for tests |
| 78 | @Synchronized |
| 79 | internal fun usePrivatePool() { |
Roman Elizarov | 1a016bd | 2017-07-12 11:41:34 +0300 | [diff] [blame] | 80 | shutdown(0) |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 81 | usePrivatePool = true |
Roman Elizarov | 1a016bd | 2017-07-12 11:41:34 +0300 | [diff] [blame] | 82 | _pool = null |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | // used for tests |
| 86 | @Synchronized |
Roman Elizarov | 1a016bd | 2017-07-12 11:41:34 +0300 | [diff] [blame] | 87 | internal fun shutdown(timeout: Long) { |
| 88 | (_pool as? ExecutorService)?.apply { |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 89 | shutdown() |
| 90 | if (timeout > 0) |
| 91 | awaitTermination(timeout, TimeUnit.MILLISECONDS) |
Roman Elizarov | 35d2c34 | 2017-07-20 14:54:39 +0300 | [diff] [blame] | 92 | shutdownNow().forEach { DefaultExecutor.execute(it) } |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 93 | } |
Roman Elizarov | 1a016bd | 2017-07-12 11:41:34 +0300 | [diff] [blame] | 94 | _pool = Executor { throw RejectedExecutionException("CommonPool was shutdown") } |
| 95 | } |
| 96 | |
| 97 | // used for tests |
| 98 | @Synchronized |
| 99 | internal fun restore() { |
| 100 | shutdown(0) |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 101 | usePrivatePool = false |
Roman Elizarov | 1a016bd | 2017-07-12 11:41:34 +0300 | [diff] [blame] | 102 | _pool = null |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 103 | } |
Roman Elizarov | dc9fd1c | 2017-04-07 10:35:28 +0300 | [diff] [blame] | 104 | |
| 105 | override fun toString(): String = "CommonPool" |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 106 | } |