in kotlinx-coroutines-core/jvm/src/Builders.kt [106:136]
fun joinBlocking(): T {
registerTimeLoopThread()
try {
eventLoop?.incrementUseCount()
try {
while (true) {
val parkNanos = eventLoop?.processNextEvent() ?: Long.MAX_VALUE
// note: process next even may loose unpark flag, so check if completed before parking
if (isCompleted) break
if (parkNanos > 0) {
if (compensateParallelism) {
withCompensatedParallelism(Duration.ZERO) {
parkNanos(this, parkNanos)
}
} else {
parkNanos(this, parkNanos)
}
}
if (Thread.interrupted()) cancelCoroutine(InterruptedException())
}
} finally { // paranoia
eventLoop?.decrementUseCount()
}
} finally { // paranoia
unregisterTimeLoopThread()
}
// now return result
val state = this.state.unboxState()
(state as? CompletedExceptionally)?.let { throw it.cause }
return state as T
}