Skip to content

Commit cb78787

Browse files
committed
runBlocking is improved to properly support specified dispatchers,
so that `runBlocking(UI) { ... }` can be used outside of UI thread. Fixes #209
1 parent 415df7e commit cb78787

File tree

2 files changed

+81
-5
lines changed
  • core/kotlinx-coroutines-core/src

2 files changed

+81
-5
lines changed

core/kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/Builders.kt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@ public suspend fun <T> run(context: CoroutineContext, block: suspend () -> T): T
155155
* in this blocked thread until the completion of this coroutine.
156156
* See [CoroutineDispatcher] for the other implementations that are provided by `kotlinx.coroutines`.
157157
*
158+
* When [CoroutineDispatcher] is explicitly specified in the [context], then the new coroutine runs in the context of
159+
* the specified dispatcher while the current thread is blocked. If the specified dispatcher implements [EventLoop]
160+
* interface and this `runBlocking` invocation is performed from inside of the this event loop's thread, then
161+
* this event loop is processed using its [processNextEvent][EventLoop.processNextEvent] method until coroutine completes.
162+
*
158163
* If this blocked thread is interrupted (see [Thread.interrupt]), then the coroutine job is cancelled and
159164
* this `runBlocking` invocation throws [InterruptedException].
160165
*
@@ -166,9 +171,13 @@ public suspend fun <T> run(context: CoroutineContext, block: suspend () -> T): T
166171
@Throws(InterruptedException::class)
167172
public fun <T> runBlocking(context: CoroutineContext = EmptyCoroutineContext, block: suspend CoroutineScope.() -> T): T {
168173
val currentThread = Thread.currentThread()
169-
val eventLoop = if (context[ContinuationInterceptor] == null) BlockingEventLoop(currentThread) else null
170-
val newContext = newCoroutineContext(context + (eventLoop ?: EmptyCoroutineContext))
171-
val coroutine = BlockingCoroutine<T>(newContext, currentThread, privateEventLoop = eventLoop != null)
174+
val contextInterceptor = context[ContinuationInterceptor]
175+
val privateEventLoop = contextInterceptor == null // create private event loop if no dispatcher is specified
176+
val eventLoop = if (privateEventLoop) BlockingEventLoop(currentThread) else contextInterceptor as? EventLoop
177+
val newContext = newCoroutineContext(
178+
if (privateEventLoop) context + (eventLoop as ContinuationInterceptor) else context
179+
)
180+
val coroutine = BlockingCoroutine<T>(newContext, currentThread, eventLoop, privateEventLoop)
172181
coroutine.start(CoroutineStart.DEFAULT, coroutine, block)
173182
return coroutine.joinBlocking()
174183
}
@@ -210,10 +219,9 @@ private class RunCompletion<in T>(
210219
private class BlockingCoroutine<T>(
211220
parentContext: CoroutineContext,
212221
private val blockedThread: Thread,
222+
private val eventLoop: EventLoop?,
213223
private val privateEventLoop: Boolean
214224
) : AbstractCoroutine<T>(parentContext, true) {
215-
private val eventLoop: EventLoop? = parentContext[ContinuationInterceptor] as? EventLoop
216-
217225
init {
218226
if (privateEventLoop) require(eventLoop is BlockingEventLoop)
219227
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
17+
package kotlinx.coroutines.experimental
18+
19+
import kotlin.coroutines.experimental.*
20+
import kotlin.test.*
21+
22+
class RunBlockingTest : TestBase() {
23+
@Test
24+
fun testPrivateEventLoop() {
25+
expect(1)
26+
runBlocking {
27+
expect(2)
28+
assertTrue(coroutineContext[ContinuationInterceptor] is EventLoop)
29+
yield() // is supported!
30+
expect(3)
31+
}
32+
finish(4)
33+
}
34+
35+
@Test
36+
fun testOuterEventLoop() {
37+
expect(1)
38+
runBlocking {
39+
expect(2)
40+
val outerEventLoop = coroutineContext[ContinuationInterceptor] as EventLoop
41+
runBlocking(coroutineContext) {
42+
expect(3)
43+
// still same event loop
44+
assertTrue(coroutineContext[ContinuationInterceptor] === outerEventLoop)
45+
yield() // still works
46+
expect(4)
47+
}
48+
expect(5)
49+
}
50+
finish(6)
51+
}
52+
53+
@Test
54+
fun testOtherDispatcher() {
55+
expect(1)
56+
val name = "RunBlockingTest.testOtherDispatcher"
57+
val thread = newSingleThreadContext(name)
58+
runBlocking(thread) {
59+
expect(2)
60+
assertTrue(coroutineContext[ContinuationInterceptor] === thread)
61+
assertTrue(Thread.currentThread().name.contains(name))
62+
yield() // should work
63+
expect(3)
64+
}
65+
finish(4)
66+
thread.close()
67+
}
68+
}

0 commit comments

Comments
 (0)