Skip to content

Commit 94339ef

Browse files
committed
parallelismCompensation: split and inline withCompensatedParallelism so that it is only present in the stacktraces when compensation is effective
1 parent 0f5b8df commit 94339ef

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,22 @@ private val parallelismCompensationEnabled: Boolean =
1010
* After the [body] completes, the effective parallelism may stay higher than the associated limit, but it is said
1111
* that eventually it will adjust to meet it.
1212
*/
13-
internal fun <T> withCompensatedParallelism(body: () -> T): T {
13+
@Suppress("NOTHING_TO_INLINE") // better stacktrace
14+
internal inline fun <T> withCompensatedParallelism(noinline body: () -> T): T {
1415
if (!parallelismCompensationEnabled) {
1516
return body()
1617
}
1718
// CoroutineScheduler.Worker implements ParallelismCompensation
1819
val worker = Thread.currentThread() as? ParallelismCompensation
1920
?: return body()
20-
worker.increaseParallelismAndLimit()
21+
return worker.withCompensatedParallelism(body)
22+
}
23+
24+
private fun <T> ParallelismCompensation.withCompensatedParallelism(body: () -> T): T {
25+
increaseParallelismAndLimit()
2126
try {
2227
return body()
2328
} finally {
24-
worker.decreaseParallelismLimit()
29+
decreaseParallelismLimit()
2530
}
2631
}

0 commit comments

Comments
 (0)