-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathConcurrentTestUtilities.kt
40 lines (32 loc) · 1.31 KB
/
ConcurrentTestUtilities.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines.exceptions
import kotlinx.coroutines.internal.*
import platform.posix.*
import kotlin.native.concurrent.*
import kotlin.random.*
actual fun randomWait() {
val n = Random.nextInt(1000)
if (n < 500) return // no wait 50% of time
repeat(n) {
BlackHole.sink *= 3
}
// use the BlackHole value somehow, so even if the compiler gets smarter, it won't remove the object
val sinkValue = if (BlackHole.sink > 16) 1 else 0
if (n + sinkValue > 900) sched_yield()
}
@ThreadLocal
private object BlackHole {
var sink = 1
}
@Suppress(
// fixme replace the suppress with AllowDifferentMembersInActual once stdlib is updated to 1.9.20 https://github.com/Kotlin/kotlinx.coroutines/issues/3846
"ACTUAL_CLASSIFIER_MUST_HAVE_THE_SAME_MEMBERS_AS_NON_FINAL_EXPECT_CLASSIFIER",
)
internal actual typealias SuppressSupportingThrowable = SuppressSupportingThrowableImpl
actual val Throwable.suppressed: Array<Throwable>
get() = (this as? SuppressSupportingThrowableImpl)?.suppressed ?: emptyArray()
@Suppress("EXTENSION_SHADOWED_BY_MEMBER", "unused")
actual fun Throwable.printStackTrace() = printStackTrace()
actual fun currentThreadName(): String = Worker.current.name