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 | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 19 | import java.util.concurrent.ExecutorService |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 20 | import java.util.concurrent.Executors |
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. |
Roman Elizarov | 49ebab9 | 2017-01-24 12:20:06 +0300 | [diff] [blame] | 26 | * It uses [java.util.concurrent.ForkJoinPool] when available, which implements efficient work-stealing algorithm for its queues, so every |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 27 | * coroutine resumption is dispatched as a separate task even when it already executes inside the pool. |
Roman Elizarov | 49ebab9 | 2017-01-24 12:20:06 +0300 | [diff] [blame] | 28 | * 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] | 29 | */ |
Roman Elizarov | 67891d8 | 2017-01-23 16:47:20 +0300 | [diff] [blame] | 30 | object CommonPool : CoroutineDispatcher() { |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 31 | private val pool: ExecutorService = findPool() |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 32 | |
| 33 | private inline fun <T> Try(block: () -> T) = try { block() } catch (e: Throwable) { null } |
| 34 | |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 35 | private fun findPool(): ExecutorService { |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 36 | val fjpClass = Try { Class.forName("java.util.concurrent.ForkJoinPool") } |
| 37 | ?: return createPlainPool() |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 38 | Try { fjpClass.getMethod("commonPool")?.invoke(null) as? ExecutorService } |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 39 | ?. let { return it } |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 40 | Try { fjpClass.getConstructor(Int::class.java).newInstance(defaultParallelism()) as? ExecutorService } |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 41 | ?. let { return it } |
| 42 | return createPlainPool() |
| 43 | } |
| 44 | |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 45 | private fun createPlainPool(): ExecutorService { |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 46 | val threadId = AtomicInteger() |
| 47 | return Executors.newFixedThreadPool(defaultParallelism()) { |
| 48 | Thread(it, "CommonPool-worker-${threadId.incrementAndGet()}").apply { isDaemon = true } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | private fun defaultParallelism() = (Runtime.getRuntime().availableProcessors() - 1).coerceAtLeast(1) |
| 53 | |
Roman Elizarov | 67891d8 | 2017-01-23 16:47:20 +0300 | [diff] [blame] | 54 | override fun dispatch(context: CoroutineContext, block: Runnable) = pool.execute(block) |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 55 | } |