Skip to content

Commit 59eec08

Browse files
committed
Intermediate value
1 parent d93cac4 commit 59eec08

File tree

2 files changed

+14
-21
lines changed

2 files changed

+14
-21
lines changed

kotlinx-coroutines-core/jvm/src/scheduling/CoroutineScheduler.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,3 +951,12 @@ internal class CoroutineScheduler(
951951
TERMINATED
952952
}
953953
}
954+
955+
@Suppress("UNUSED")
956+
@JvmName("isSchedulerWorker")
957+
internal fun isSchedulerWorker(thread: Thread) = thread is CoroutineScheduler.Worker
958+
959+
@Suppress("UNUSED")
960+
@JvmName("mayNotBlock")
961+
internal fun mayNotBlock(thread: Thread) = thread is CoroutineScheduler.Worker &&
962+
thread.state == CoroutineScheduler.WorkerState.CPU_ACQUIRED

kotlinx-coroutines-debug/src/internal/BlockHoundIntegration.kt

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,16 @@
1+
@file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
12
package kotlinx.coroutines.debug.internal
23

34
import reactor.blockhound.BlockHound
4-
import kotlin.reflect.KClass
5-
import kotlin.reflect.full.*
5+
import kotlinx.coroutines.scheduling.*
66

77
internal object BlockHoundIntegration {
88

99
init {
10-
val cls = Class.forName("kotlinx.coroutines.scheduling.CoroutineScheduler\$Worker").kotlin
11-
initializerHelper(cls)
12-
}
13-
14-
private fun <T : Any> initializerHelper(cls: KClass<T>) {
15-
val field = cls.declaredMemberProperties.find { it.name == "state" }!!
1610
BlockHound.builder()
17-
.addDynamicThreadPredicate(cls::isInstance)
18-
.nonBlockingThreadPredicate { p ->
19-
p.or { thread ->
20-
val castThread = cls.safeCast(thread)
21-
if (!enabled || castThread == null) {
22-
false
23-
} else {
24-
val state = field(castThread) as Enum<*>
25-
state.name == "CPU_ACQUIRED"
26-
}
27-
}
28-
}
29-
.install()
11+
.addDynamicThreadPredicate { isSchedulerWorker(it) }
12+
.nonBlockingThreadPredicate { p -> p.or { thread -> enabled && mayNotBlock(thread) } }
13+
.install()
3014
}
3115

3216
@Volatile

0 commit comments

Comments
 (0)