Roman Elizarov | 53a0a40 | 2017-01-19 20:21:57 +0300 | [diff] [blame] | 1 | package kotlinx.coroutines.experimental |
| 2 | |
| 3 | import kotlinx.coroutines.experimental.internal.LockFreeLinkedListHead |
| 4 | import kotlinx.coroutines.experimental.internal.LockFreeLinkedListNode |
| 5 | import java.util.concurrent.locks.LockSupport |
| 6 | import kotlin.coroutines.Continuation |
Roman Elizarov | 67891d8 | 2017-01-23 16:47:20 +0300 | [diff] [blame^] | 7 | import kotlin.coroutines.CoroutineContext |
Roman Elizarov | 53a0a40 | 2017-01-19 20:21:57 +0300 | [diff] [blame] | 8 | |
Roman Elizarov | d528e3e | 2017-01-23 15:40:05 +0300 | [diff] [blame] | 9 | /** |
| 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 Elizarov | 53a0a40 | 2017-01-19 20:21:57 +0300 | [diff] [blame] | 14 | public interface EventLoop { |
Roman Elizarov | d528e3e | 2017-01-23 15:40:05 +0300 | [diff] [blame] | 15 | /** |
| 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 Elizarov | 53a0a40 | 2017-01-19 20:21:57 +0300 | [diff] [blame] | 40 | } |
| 41 | |
Roman Elizarov | d528e3e | 2017-01-23 15:40:05 +0300 | [diff] [blame] | 42 | internal class EventLoopImpl( |
| 43 | val thread: Thread |
| 44 | ) : CoroutineDispatcher(), EventLoop, Yield { |
Roman Elizarov | 53a0a40 | 2017-01-19 20:21:57 +0300 | [diff] [blame] | 45 | val queue = LockFreeLinkedListHead() |
Roman Elizarov | d528e3e | 2017-01-23 15:40:05 +0300 | [diff] [blame] | 46 | var parentJob: Job? = null |
Roman Elizarov | 53a0a40 | 2017-01-19 20:21:57 +0300 | [diff] [blame] | 47 | |
Roman Elizarov | d528e3e | 2017-01-23 15:40:05 +0300 | [diff] [blame] | 48 | fun initParentJob(coroutine: Job) { |
| 49 | require(this.parentJob == null) |
| 50 | this.parentJob = coroutine |
Roman Elizarov | 53a0a40 | 2017-01-19 20:21:57 +0300 | [diff] [blame] | 51 | } |
| 52 | |
Roman Elizarov | 67891d8 | 2017-01-23 16:47:20 +0300 | [diff] [blame^] | 53 | override fun isDispatchNeeded(context: CoroutineContext): Boolean = Thread.currentThread() != thread |
Roman Elizarov | 53a0a40 | 2017-01-19 20:21:57 +0300 | [diff] [blame] | 54 | |
Roman Elizarov | 67891d8 | 2017-01-23 16:47:20 +0300 | [diff] [blame^] | 55 | override fun dispatch(context: CoroutineContext, block: Runnable) { |
Roman Elizarov | 53a0a40 | 2017-01-19 20:21:57 +0300 | [diff] [blame] | 56 | schedule(Dispatch(block)) |
Roman Elizarov | 53a0a40 | 2017-01-19 20:21:57 +0300 | [diff] [blame] | 57 | } |
| 58 | |
Roman Elizarov | d528e3e | 2017-01-23 15:40:05 +0300 | [diff] [blame] | 59 | override fun scheduleResume(continuation: CancellableContinuation<Unit>) { |
| 60 | val node = Resume(continuation) |
| 61 | if (schedule(node)) |
| 62 | continuation.removeOnCompletion(node) |
Roman Elizarov | 53a0a40 | 2017-01-19 20:21:57 +0300 | [diff] [blame] | 63 | } |
| 64 | |
Roman Elizarov | d528e3e | 2017-01-23 15:40:05 +0300 | [diff] [blame] | 65 | 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 Elizarov | 53a0a40 | 2017-01-19 20:21:57 +0300 | [diff] [blame] | 72 | LockSupport.unpark(thread) |
Roman Elizarov | d528e3e | 2017-01-23 15:40:05 +0300 | [diff] [blame] | 73 | } else { |
| 74 | node.run() |
Roman Elizarov | 53a0a40 | 2017-01-19 20:21:57 +0300 | [diff] [blame] | 75 | } |
Roman Elizarov | d528e3e | 2017-01-23 15:40:05 +0300 | [diff] [blame] | 76 | return added |
Roman Elizarov | 53a0a40 | 2017-01-19 20:21:57 +0300 | [diff] [blame] | 77 | } |
| 78 | |
Roman Elizarov | d528e3e | 2017-01-23 15:40:05 +0300 | [diff] [blame] | 79 | 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 Elizarov | 53a0a40 | 2017-01-19 20:21:57 +0300 | [diff] [blame] | 87 | |
Roman Elizarov | d528e3e | 2017-01-23 15:40:05 +0300 | [diff] [blame] | 88 | 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 Elizarov | 53a0a40 | 2017-01-19 20:21:57 +0300 | [diff] [blame] | 93 | override fun run() = cont.resume(Unit) |
| 94 | } |
| 95 | } |
| 96 | |