Skip to content

Commit 129ec8b

Browse files
qwwdfsadelizarov
authored andcommitted
Revert "Provide BlockingChecker mechanism which checks current context"
This reverts commit bac6bde.
1 parent 2a59741 commit 129ec8b

File tree

13 files changed

+26
-148
lines changed

13 files changed

+26
-148
lines changed

binary-compatibility-validator/reference-public-api/kotlinx-coroutines-android.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,3 @@ public final class kotlinx/coroutines/experimental/android/HandlerContextKt {
2323
public static final fun getUI ()Lkotlinx/coroutines/experimental/android/HandlerContext;
2424
}
2525

26-
public final class kotlinx/coroutines/experimental/android/MainLooperChecker : kotlinx/coroutines/experimental/BlockingChecker {
27-
public fun <init> ()V
28-
public fun checkRunBlocking ()V
29-
}
30-

binary-compatibility-validator/reference-public-api/kotlinx-coroutines-core.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ public final class kotlinx/coroutines/experimental/AwaitKt {
2020
public static final fun joinAll ([Lkotlinx/coroutines/experimental/Job;Lkotlin/coroutines/experimental/Continuation;)Ljava/lang/Object;
2121
}
2222

23-
public abstract interface class kotlinx/coroutines/experimental/BlockingChecker {
24-
public abstract fun checkRunBlocking ()V
25-
}
26-
2723
public final class kotlinx/coroutines/experimental/BuildersKt {
2824
public static final synthetic fun launch (Lkotlin/coroutines/experimental/CoroutineContext;Lkotlinx/coroutines/experimental/CoroutineStart;Lkotlin/jvm/functions/Function2;)Lkotlinx/coroutines/experimental/Job;
2925
public static final fun launch (Lkotlin/coroutines/experimental/CoroutineContext;Lkotlinx/coroutines/experimental/CoroutineStart;Lkotlinx/coroutines/experimental/Job;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Lkotlinx/coroutines/experimental/Job;

binary-compatibility-validator/reference-public-api/kotlinx-coroutines-javafx.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
public final class kotlinx/coroutines/experimental/javafx/ApplicationThreadChecker : kotlinx/coroutines/experimental/BlockingChecker {
2-
public fun <init> ()V
3-
public fun checkRunBlocking ()V
4-
}
5-
61
public final class kotlinx/coroutines/experimental/javafx/JavaFx : kotlinx/coroutines/experimental/CoroutineDispatcher, kotlinx/coroutines/experimental/Delay {
72
public static final field INSTANCE Lkotlinx/coroutines/experimental/javafx/JavaFx;
83
public final fun awaitPulse (Lkotlin/coroutines/experimental/Continuation;)Ljava/lang/Object;

binary-compatibility-validator/reference-public-api/kotlinx-coroutines-swing.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
public final class kotlinx/coroutines/experimental/swing/EventDispatchThreadChecker : kotlinx/coroutines/experimental/BlockingChecker {
2-
public fun <init> ()V
3-
public fun checkRunBlocking ()V
4-
}
5-
61
public final class kotlinx/coroutines/experimental/swing/Swing : kotlinx/coroutines/experimental/CoroutineDispatcher, kotlinx/coroutines/experimental/Delay {
72
public static final field INSTANCE Lkotlinx/coroutines/experimental/swing/Swing;
83
public fun delay (JLjava/util/concurrent/TimeUnit;Lkotlin/coroutines/experimental/Continuation;)Ljava/lang/Object;

core/kotlinx-coroutines-core/src/Builders.kt

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
package kotlinx.coroutines.experimental
99

10-
import kotlinx.coroutines.experimental.internal.*
11-
import java.util.*
1210
import java.util.concurrent.locks.*
1311
import kotlin.coroutines.experimental.*
1412

@@ -31,14 +29,11 @@ import kotlin.coroutines.experimental.*
3129
*
3230
* See [newCoroutineContext] for a description of debugging facilities that are available for newly created coroutine.
3331
*
34-
* @throws IllegalStateException if blocking is not allowed in current thread.
35-
* Blocking is checked by [BlockingChecker] registered in [ServiceLoader]
3632
* @param context context of the coroutine. The default value is an implementation of [EventLoop].
3733
* @param block the coroutine code.
3834
*/
3935
@Throws(InterruptedException::class)
4036
public fun <T> runBlocking(context: CoroutineContext = EmptyCoroutineContext, block: suspend CoroutineScope.() -> T): T {
41-
blockingChecker?.checkRunBlocking()
4237
val currentThread = Thread.currentThread()
4338
val contextInterceptor = context[ContinuationInterceptor]
4439
val privateEventLoop = contextInterceptor == null // create private event loop if no dispatcher is specified
@@ -51,35 +46,6 @@ public fun <T> runBlocking(context: CoroutineContext = EmptyCoroutineContext, bl
5146
return coroutine.joinBlocking()
5247
}
5348

54-
/**
55-
* Extension point which determines whether invoking [runBlocking] in the current thread is allowed.
56-
* [runBlocking] discovers all checkers via [ServiceLoader] and invokes [checkRunBlocking] on
57-
* each attempt to invoke [runBlocking].
58-
*
59-
* Common example is checking whether we're not in UI thread:
60-
*
61-
* ```
62-
* class UiChecker : BlockingChecker {
63-
* fun checkRunBlocking() =
64-
* check(!UiFramework.isInUiThread()) { "runBlocking is not allowed in UI thread" }
65-
* }
66-
* ```
67-
*/
68-
public interface BlockingChecker {
69-
/**
70-
* Throws [IllegalStateException] if [runBlocking] calls are not allowed in the current thread.
71-
*/
72-
fun checkRunBlocking()
73-
}
74-
75-
// Nullable to enable DCE when no filters are present in classpath
76-
private val blockingChecker: BlockingChecker? = run {
77-
val filters = ServiceLoader.load(BlockingChecker::class.java).toList().toTypedArray()
78-
if (filters.isEmpty()) null else object : BlockingChecker {
79-
override fun checkRunBlocking() = filters.forEach { it.checkRunBlocking() }
80-
}
81-
}
82-
8349
private class BlockingCoroutine<T>(
8450
parentContext: CoroutineContext,
8551
private val blockedThread: Thread,

ui/kotlinx-coroutines-android/resources/META-INF/services/kotlinx.coroutines.experimental.BlockingChecker

Lines changed: 0 additions & 1 deletion
This file was deleted.

ui/kotlinx-coroutines-android/src/HandlerContext.kt

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
package kotlinx.coroutines.experimental.android
66

77
import android.os.*
8-
import android.support.annotation.*
98
import android.view.*
109
import kotlinx.coroutines.experimental.*
11-
import java.util.concurrent.*
12-
import kotlin.coroutines.experimental.*
10+
import java.util.concurrent.TimeUnit
11+
import kotlin.coroutines.experimental.CoroutineContext
1312

1413
/**
1514
* Dispatches execution onto Android main UI thread and provides native [delay][Delay.delay] support.
@@ -114,12 +113,3 @@ public class HandlerContext private constructor(
114113
override fun equals(other: Any?): Boolean = other is HandlerContext && other.handler === handler
115114
override fun hashCode(): Int = System.identityHashCode(handler)
116115
}
117-
118-
/**
119-
* @suppress This is an internal impl class.
120-
*/
121-
@Keep
122-
class MainLooperChecker : BlockingChecker {
123-
override fun checkRunBlocking() =
124-
check(Looper.myLooper() != Looper.getMainLooper()) { "runBlocking is not allowed in Android main looper thread" }
125-
}

ui/kotlinx-coroutines-javafx/resources/META-INF/services/kotlinx.coroutines.experimental.BlockingChecker

Lines changed: 0 additions & 1 deletion
This file was deleted.

ui/kotlinx-coroutines-javafx/src/JavaFx.kt

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import javafx.event.*
1010
import javafx.util.*
1111
import kotlinx.coroutines.experimental.*
1212
import kotlinx.coroutines.experimental.javafx.JavaFx.delay
13-
import java.util.concurrent.*
14-
import kotlin.coroutines.experimental.*
13+
import java.util.concurrent.CopyOnWriteArrayList
14+
import java.util.concurrent.TimeUnit
15+
import kotlin.coroutines.experimental.CoroutineContext
1516

1617
/**
1718
* Dispatches execution onto JavaFx application thread and provides native [delay] support.
@@ -76,14 +77,6 @@ object JavaFx : CoroutineDispatcher(), Delay {
7677
override fun toString() = "JavaFx"
7778
}
7879

79-
/**
80-
* @suppress This is an internal impl class.
81-
*/
82-
class ApplicationThreadChecker : BlockingChecker {
83-
override fun checkRunBlocking() =
84-
check(!Platform.isFxApplicationThread()) { "runBlocking is not allowed in JavaFx application thread" }
85-
}
86-
8780
internal fun initPlatform() {
8881
// Ad-hoc workaround for #443. Will be fixed with multi-release jar.
8982
// If this code throws an exception (Java 9 + prohibited reflective access), initialize JavaFX directly

ui/kotlinx-coroutines-javafx/test/JavaFxTest.kt

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,25 @@
44

55
package kotlinx.coroutines.experimental.javafx
66

7-
import javafx.application.*
7+
import javafx.application.Platform
88
import kotlinx.coroutines.experimental.*
9-
import org.junit.*
9+
import org.junit.Before
1010
import org.junit.Test
11-
import kotlin.test.*
1211

1312
class JavaFxTest : TestBase() {
14-
15-
companion object {
16-
val fxEnabled: Boolean = try {
17-
initPlatform()
18-
true
19-
} catch (e: Throwable) {
20-
println("Skipping JavaFxTest in headless environment")
21-
false
22-
}
23-
24-
}
25-
2613
@Before
2714
fun setup() {
2815
ignoreLostThreads("JavaFX Application Thread", "Thread-", "QuantumRenderer-")
2916
}
3017

3118
@Test
3219
fun testDelay() {
33-
if (!fxEnabled) {
34-
return // ignore test in headless environments
35-
}
20+
try {
21+
initPlatform()
22+
} catch (e: UnsupportedOperationException) {
23+
println("Skipping JavaFxTest in headless environment")
24+
return // ignore test in headless environments
25+
}
3626

3727
runBlocking {
3828
expect(1)
@@ -47,23 +37,4 @@ class JavaFxTest : TestBase() {
4737
finish(4)
4838
}
4939
}
50-
51-
@Test
52-
fun testRunBlockingForbidden() {
53-
if (!fxEnabled) {
54-
return // ignore test in headless environments
55-
}
56-
57-
runBlocking(JavaFx) {
58-
expect(1)
59-
try {
60-
runBlocking(JavaFx) {
61-
expectUnreached()
62-
}
63-
} catch (e: Exception) {
64-
assertTrue(e.message!!.contains("runBlocking"))
65-
finish(2)
66-
}
67-
}
68-
}
69-
}
40+
}

0 commit comments

Comments
 (0)