blob: ce359309c7e393794248402078dc658b00a764c1 [file] [log] [blame]
Roman Elizarov53a0a402017-01-19 20:21:57 +03001package kotlinx.coroutines.experimental
2
3import kotlinx.coroutines.experimental.internal.LockFreeLinkedListHead
4import kotlinx.coroutines.experimental.internal.LockFreeLinkedListNode
5import java.util.concurrent.locks.LockSupport
6import kotlin.coroutines.Continuation
Roman Elizarov67891d82017-01-23 16:47:20 +03007import kotlin.coroutines.CoroutineContext
Roman Elizarov53a0a402017-01-19 20:21:57 +03008
Roman Elizarovd528e3e2017-01-23 15:40:05 +03009/**
10 * Implemented by [CoroutineDispatcher] implementations that have event loop inside and can
11 * be asked to process next event from their event queue. It is used by [runBlocking] to
12 * continue processing events when invoked from the event dispatch thread.
13 */
Roman Elizarov53a0a402017-01-19 20:21:57 +030014public interface EventLoop {
Roman Elizarovd528e3e2017-01-23 15:40:05 +030015 /**
16 * Processes next event in this event loop and returns `true` or returns `false` if there are
17 * no events to process or when invoked from the wrong thread.
18 */
19 public fun processNextEvent(): Boolean
20
21 public companion object Factory {
22 /**
23 * Creates a new event loop that is bound the specified [thread] (current thread by default) and
24 * stops accepting new events when [parentJob] completes. Every continuation that is scheduled
25 * onto this event loop unparks the specified thread via [LockSupport.unpark].
26 *
27 * The main event-processing loop using the resulting `eventLoop` object should look like this:
28 * ```
29 * while (needsToBeRunning) {
30 * if (Thread.interrupted()) break // or handle somehow
31 * if (!eventLoop.processNextEvent()) LockSupport.park() // event loop will unpark
32 * }
33 * ```
34 */
35 public operator fun invoke(thread: Thread = Thread.currentThread(), parentJob: Job? = null): CoroutineDispatcher =
36 EventLoopImpl(thread).apply {
37 if (parentJob != null) initParentJob(parentJob)
38 }
39 }
Roman Elizarov53a0a402017-01-19 20:21:57 +030040}
41
Roman Elizarovd528e3e2017-01-23 15:40:05 +030042internal class EventLoopImpl(
43 val thread: Thread
44) : CoroutineDispatcher(), EventLoop, Yield {
Roman Elizarov53a0a402017-01-19 20:21:57 +030045 val queue = LockFreeLinkedListHead()
Roman Elizarovd528e3e2017-01-23 15:40:05 +030046 var parentJob: Job? = null
Roman Elizarov53a0a402017-01-19 20:21:57 +030047
Roman Elizarovd528e3e2017-01-23 15:40:05 +030048 fun initParentJob(coroutine: Job) {
49 require(this.parentJob == null)
50 this.parentJob = coroutine
Roman Elizarov53a0a402017-01-19 20:21:57 +030051 }
52
Roman Elizarov67891d82017-01-23 16:47:20 +030053 override fun isDispatchNeeded(context: CoroutineContext): Boolean = Thread.currentThread() != thread
Roman Elizarov53a0a402017-01-19 20:21:57 +030054
Roman Elizarov67891d82017-01-23 16:47:20 +030055 override fun dispatch(context: CoroutineContext, block: Runnable) {
Roman Elizarov53a0a402017-01-19 20:21:57 +030056 schedule(Dispatch(block))
Roman Elizarov53a0a402017-01-19 20:21:57 +030057 }
58
Roman Elizarovd528e3e2017-01-23 15:40:05 +030059 override fun scheduleResume(continuation: CancellableContinuation<Unit>) {
60 val node = Resume(continuation)
61 if (schedule(node))
62 continuation.removeOnCompletion(node)
Roman Elizarov53a0a402017-01-19 20:21:57 +030063 }
64
Roman Elizarovd528e3e2017-01-23 15:40:05 +030065 fun schedule(node: Node): Boolean {
66 val added = if (parentJob == null) {
67 queue.addLast(node)
68 true
69 } else
70 queue.addLastIf(node) { parentJob!!.isActive }
71 if (added) {
Roman Elizarov53a0a402017-01-19 20:21:57 +030072 LockSupport.unpark(thread)
Roman Elizarovd528e3e2017-01-23 15:40:05 +030073 } else {
74 node.run()
Roman Elizarov53a0a402017-01-19 20:21:57 +030075 }
Roman Elizarovd528e3e2017-01-23 15:40:05 +030076 return added
Roman Elizarov53a0a402017-01-19 20:21:57 +030077 }
78
Roman Elizarovd528e3e2017-01-23 15:40:05 +030079 override fun processNextEvent(): Boolean {
80 if (Thread.currentThread() != thread) return false
81 (queue.removeFirstOrNull() as? Runnable)?.apply {
82 run()
83 return true
84 }
85 return false
86 }
Roman Elizarov53a0a402017-01-19 20:21:57 +030087
Roman Elizarovd528e3e2017-01-23 15:40:05 +030088 abstract class Node : LockFreeLinkedListNode(), Runnable
89
90 class Dispatch(block: Runnable) : Node(), Runnable by block
91
92 class Resume(val cont: Continuation<Unit>) : Node() {
Roman Elizarov53a0a402017-01-19 20:21:57 +030093 override fun run() = cont.resume(Unit)
94 }
95}
96