1 package kotlinx.coroutines.exceptions 2 3 import kotlinx.coroutines.* 4 import kotlin.concurrent.Volatile 5 import kotlin.random.* 6 randomWaitnull7fun randomWait() { 8 val n = Random.nextInt(1000) 9 if (n < 500) return // no wait 50% of time 10 repeat(n) { 11 BlackHole.sink *= 3 12 } 13 // use the BlackHole value somehow, so even if the compiler gets smarter, it won't remove the object 14 val sinkValue = if (BlackHole.sink > 16) 1 else 0 15 if (n + sinkValue > 900) yieldThread() 16 } 17 18 private object BlackHole { 19 @Volatile 20 var sink = 1 21 } 22 yieldThreadnull23expect inline fun yieldThread() 24 25 expect fun currentThreadName(): String 26 27 inline fun CloseableCoroutineDispatcher.use(block: (CloseableCoroutineDispatcher) -> Unit) { 28 try { 29 block(this) 30 } finally { 31 close() 32 } 33 } 34