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 | 53a0a40 | 2017-01-19 20:21:57 +0300 | [diff] [blame] | 17 | package kotlinx.coroutines.experimental |
| 18 | |
| 19 | import kotlinx.coroutines.experimental.internal.LockFreeLinkedListHead |
| 20 | import kotlinx.coroutines.experimental.internal.LockFreeLinkedListNode |
| 21 | import java.util.concurrent.locks.LockSupport |
Roman Elizarov | ea4a51b | 2017-01-31 12:01:25 +0300 | [diff] [blame] | 22 | import kotlin.coroutines.experimental.CoroutineContext |
Roman Elizarov | 53a0a40 | 2017-01-19 20:21:57 +0300 | [diff] [blame] | 23 | |
Roman Elizarov | d528e3e | 2017-01-23 15:40:05 +0300 | [diff] [blame] | 24 | /** |
| 25 | * Implemented by [CoroutineDispatcher] implementations that have event loop inside and can |
| 26 | * be asked to process next event from their event queue. It is used by [runBlocking] to |
| 27 | * continue processing events when invoked from the event dispatch thread. |
| 28 | */ |
Roman Elizarov | 53a0a40 | 2017-01-19 20:21:57 +0300 | [diff] [blame] | 29 | public interface EventLoop { |
Roman Elizarov | d528e3e | 2017-01-23 15:40:05 +0300 | [diff] [blame] | 30 | /** |
| 31 | * Processes next event in this event loop and returns `true` or returns `false` if there are |
| 32 | * no events to process or when invoked from the wrong thread. |
| 33 | */ |
| 34 | public fun processNextEvent(): Boolean |
| 35 | |
| 36 | public companion object Factory { |
| 37 | /** |
| 38 | * Creates a new event loop that is bound the specified [thread] (current thread by default) and |
| 39 | * stops accepting new events when [parentJob] completes. Every continuation that is scheduled |
| 40 | * onto this event loop unparks the specified thread via [LockSupport.unpark]. |
| 41 | * |
| 42 | * The main event-processing loop using the resulting `eventLoop` object should look like this: |
| 43 | * ``` |
| 44 | * while (needsToBeRunning) { |
| 45 | * if (Thread.interrupted()) break // or handle somehow |
| 46 | * if (!eventLoop.processNextEvent()) LockSupport.park() // event loop will unpark |
| 47 | * } |
| 48 | * ``` |
| 49 | */ |
| 50 | public operator fun invoke(thread: Thread = Thread.currentThread(), parentJob: Job? = null): CoroutineDispatcher = |
| 51 | EventLoopImpl(thread).apply { |
| 52 | if (parentJob != null) initParentJob(parentJob) |
| 53 | } |
| 54 | } |
Roman Elizarov | 53a0a40 | 2017-01-19 20:21:57 +0300 | [diff] [blame] | 55 | } |
| 56 | |
Roman Elizarov | d528e3e | 2017-01-23 15:40:05 +0300 | [diff] [blame] | 57 | internal class EventLoopImpl( |
| 58 | val thread: Thread |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 59 | ) : CoroutineDispatcher(), EventLoop { |
Roman Elizarov | 53a0a40 | 2017-01-19 20:21:57 +0300 | [diff] [blame] | 60 | val queue = LockFreeLinkedListHead() |
Roman Elizarov | d528e3e | 2017-01-23 15:40:05 +0300 | [diff] [blame] | 61 | var parentJob: Job? = null |
Roman Elizarov | 53a0a40 | 2017-01-19 20:21:57 +0300 | [diff] [blame] | 62 | |
Roman Elizarov | d528e3e | 2017-01-23 15:40:05 +0300 | [diff] [blame] | 63 | fun initParentJob(coroutine: Job) { |
| 64 | require(this.parentJob == null) |
| 65 | this.parentJob = coroutine |
Roman Elizarov | 53a0a40 | 2017-01-19 20:21:57 +0300 | [diff] [blame] | 66 | } |
| 67 | |
Roman Elizarov | 67891d8 | 2017-01-23 16:47:20 +0300 | [diff] [blame] | 68 | override fun dispatch(context: CoroutineContext, block: Runnable) { |
Roman Elizarov | 53a0a40 | 2017-01-19 20:21:57 +0300 | [diff] [blame] | 69 | schedule(Dispatch(block)) |
Roman Elizarov | 53a0a40 | 2017-01-19 20:21:57 +0300 | [diff] [blame] | 70 | } |
| 71 | |
Roman Elizarov | d528e3e | 2017-01-23 15:40:05 +0300 | [diff] [blame] | 72 | fun schedule(node: Node): Boolean { |
| 73 | val added = if (parentJob == null) { |
| 74 | queue.addLast(node) |
| 75 | true |
| 76 | } else |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 77 | queue.addLastIf(node) { !parentJob!!.isCompleted } |
Roman Elizarov | d528e3e | 2017-01-23 15:40:05 +0300 | [diff] [blame] | 78 | if (added) { |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 79 | if (Thread.currentThread() !== thread) |
Roman Elizarov | a4becc9 | 2017-01-24 11:45:50 +0300 | [diff] [blame] | 80 | LockSupport.unpark(thread) |
Roman Elizarov | d528e3e | 2017-01-23 15:40:05 +0300 | [diff] [blame] | 81 | } else { |
| 82 | node.run() |
Roman Elizarov | 53a0a40 | 2017-01-19 20:21:57 +0300 | [diff] [blame] | 83 | } |
Roman Elizarov | d528e3e | 2017-01-23 15:40:05 +0300 | [diff] [blame] | 84 | return added |
Roman Elizarov | 53a0a40 | 2017-01-19 20:21:57 +0300 | [diff] [blame] | 85 | } |
| 86 | |
Roman Elizarov | d528e3e | 2017-01-23 15:40:05 +0300 | [diff] [blame] | 87 | override fun processNextEvent(): Boolean { |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 88 | if (Thread.currentThread() !== thread) return false |
Roman Elizarov | d528e3e | 2017-01-23 15:40:05 +0300 | [diff] [blame] | 89 | (queue.removeFirstOrNull() as? Runnable)?.apply { |
| 90 | run() |
| 91 | return true |
| 92 | } |
| 93 | return false |
| 94 | } |
Roman Elizarov | 53a0a40 | 2017-01-19 20:21:57 +0300 | [diff] [blame] | 95 | |
Roman Elizarov | d528e3e | 2017-01-23 15:40:05 +0300 | [diff] [blame] | 96 | abstract class Node : LockFreeLinkedListNode(), Runnable |
| 97 | |
| 98 | class Dispatch(block: Runnable) : Node(), Runnable by block |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 99 | |
| 100 | override fun toString(): String = "EventLoopImpl@${Integer.toHexString(System.identityHashCode(this))}" |
Roman Elizarov | 53a0a40 | 2017-01-19 20:21:57 +0300 | [diff] [blame] | 101 | } |
| 102 | |