Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 1 | package kotlinx.coroutines.experimental |
| 2 | |
| 3 | import java.util.concurrent.Executor |
| 4 | import java.util.concurrent.Executors |
| 5 | import java.util.concurrent.ForkJoinPool |
| 6 | import java.util.concurrent.atomic.AtomicInteger |
Roman Elizarov | 67891d8 | 2017-01-23 16:47:20 +0300 | [diff] [blame^] | 7 | import kotlin.coroutines.CoroutineContext |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 8 | |
| 9 | /** |
Roman Elizarov | ed7b864 | 2017-01-19 11:22:28 +0300 | [diff] [blame] | 10 | * Represents common pool of shared threads as coroutine dispatcher for compute-intensive tasks. |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 11 | * It uses [ForkJoinPool] when available, which implements efficient work-stealing algorithm for its queues, so every |
| 12 | * coroutine resumption is dispatched as a separate task even when it already executes inside the pool. |
Roman Elizarov | ed7b864 | 2017-01-19 11:22:28 +0300 | [diff] [blame] | 13 | * When available, it wraps [ForkJoinPool.commonPool] and provides a similar shared pool where not. |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 14 | */ |
Roman Elizarov | 67891d8 | 2017-01-23 16:47:20 +0300 | [diff] [blame^] | 15 | object CommonPool : CoroutineDispatcher() { |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 16 | private val pool: Executor = findPool() |
| 17 | |
| 18 | private inline fun <T> Try(block: () -> T) = try { block() } catch (e: Throwable) { null } |
| 19 | |
| 20 | private fun findPool(): Executor { |
| 21 | val fjpClass = Try { Class.forName("java.util.concurrent.ForkJoinPool") } |
| 22 | ?: return createPlainPool() |
| 23 | Try { fjpClass.getMethod("commonPool")?.invoke(null) as? Executor } |
| 24 | ?. let { return it } |
| 25 | Try { fjpClass.getConstructor(Int::class.java).newInstance(defaultParallelism()) as? Executor } |
| 26 | ?. let { return it } |
| 27 | return createPlainPool() |
| 28 | } |
| 29 | |
| 30 | private fun createPlainPool(): Executor { |
| 31 | val threadId = AtomicInteger() |
| 32 | return Executors.newFixedThreadPool(defaultParallelism()) { |
| 33 | Thread(it, "CommonPool-worker-${threadId.incrementAndGet()}").apply { isDaemon = true } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | private fun defaultParallelism() = (Runtime.getRuntime().availableProcessors() - 1).coerceAtLeast(1) |
| 38 | |
Roman Elizarov | 67891d8 | 2017-01-23 16:47:20 +0300 | [diff] [blame^] | 39 | override fun isDispatchNeeded(context: CoroutineContext): Boolean = true |
| 40 | override fun dispatch(context: CoroutineContext, block: Runnable) = pool.execute(block) |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 41 | } |