blob: dbfab0116f5c826db166ae42bd116af673ea3fb3 [file] [log] [blame]
Roman Elizarov3754f952017-01-18 20:47:54 +03001package kotlinx.coroutines.experimental
2
3import java.util.concurrent.Executors
4import java.util.concurrent.ScheduledExecutorService
5import java.util.concurrent.TimeUnit
6import java.util.concurrent.atomic.AtomicInteger
7import kotlin.concurrent.thread
Roman Elizarov3754f952017-01-18 20:47:54 +03008import kotlin.coroutines.CoroutineContext
9
10/**
Roman Elizarov67891d82017-01-23 16:47:20 +030011 * Creates new coroutine execution context with the a single thread and built-in [yield] and [delay] support.
Roman Elizarov3754f952017-01-18 20:47:54 +030012 * All continuations are dispatched immediately when invoked inside the thread of this context.
13 * Resources of this pool (its thread) are reclaimed when job of this context is cancelled.
14 * The specified [name] defines the name of the new thread.
15 * An optional [parent] job may be specified upon creation.
16 */
17fun newSingleThreadContext(name: String, parent: Job? = null): CoroutineContext =
18 newFixedThreadPoolContext(1, name, parent)
19
20/**
Roman Elizarov67891d82017-01-23 16:47:20 +030021 * Creates new coroutine execution context with the fixed-size thread-pool and built-in [yield] and [delay] support.
Roman Elizarov3754f952017-01-18 20:47:54 +030022 * All continuations are dispatched immediately when invoked inside the threads of this context.
23 * Resources of this pool (its threads) are reclaimed when job of this context is cancelled.
24 * The specified [name] defines the names of the threads.
25 * An optional [parent] job may be specified upon creation.
26 */
27fun newFixedThreadPoolContext(nThreads: Int, name: String, parent: Job? = null): CoroutineContext {
28 require(nThreads >= 1) { "Expected at least one thread, but $nThreads specified" }
Roman Elizarov67891d82017-01-23 16:47:20 +030029 val job = Job(parent)
30 return job + ThreadPoolDispatcher(nThreads, name, job)
Roman Elizarov3754f952017-01-18 20:47:54 +030031}
32
33private val thisThreadContext = ThreadLocal<ThreadPoolDispatcher>()
34
35private class ThreadPoolDispatcher(
36 nThreads: Int,
37 name: String,
38 val job: Job
Roman Elizarov67891d82017-01-23 16:47:20 +030039) : CoroutineDispatcher(), Yield, Delay {
Roman Elizarov3754f952017-01-18 20:47:54 +030040 val threadNo = AtomicInteger()
41 val executor: ScheduledExecutorService = Executors.newScheduledThreadPool(nThreads) { target ->
42 thread(start = false, isDaemon = true,
43 name = if (nThreads == 1) name else name + "-" + threadNo.incrementAndGet()) {
44 thisThreadContext.set(this@ThreadPoolDispatcher)
45 target.run()
46 }
47 }
48
49 init {
50 job.onCompletion { executor.shutdown() }
51 }
52
Roman Elizarov67891d82017-01-23 16:47:20 +030053 override fun isDispatchNeeded(context: CoroutineContext): Boolean = thisThreadContext.get() != this
Roman Elizarov3754f952017-01-18 20:47:54 +030054
Roman Elizarov67891d82017-01-23 16:47:20 +030055 override fun dispatch(context: CoroutineContext, block: Runnable) = executor.execute(block)
Roman Elizarov3754f952017-01-18 20:47:54 +030056
Roman Elizarovd528e3e2017-01-23 15:40:05 +030057 override fun scheduleResume(continuation: CancellableContinuation<Unit>) {
Roman Elizarov67891d82017-01-23 16:47:20 +030058 executor.scheduleResume(continuation)
Roman Elizarovd528e3e2017-01-23 15:40:05 +030059 }
60
61 override fun scheduleResumeAfterDelay(time: Long, unit: TimeUnit, continuation: CancellableContinuation<Unit>) {
Roman Elizarov67891d82017-01-23 16:47:20 +030062 executor.scheduleResumeAfterDelay(time, unit, continuation)
Roman Elizarov3754f952017-01-18 20:47:54 +030063 }
64}