-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Thread parking for Kotlin/Common #498
base: master
Are you sure you want to change the base?
Conversation
atomicfu/src/jvmMain/kotlin/kotlinx/atomicfu/parking/KThread.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/jvmMain/kotlin/kotlinx/atomicfu/parking/JvmParkingDelegator.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/jvmMain/kotlin/kotlinx/atomicfu/parking/JvmParkingDelegator.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/androidNative32BitMain/kotlin/kotlinx/atomicfu/parking/PosixParkingDelegator.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/commonMain/kotlin/kotlinx/atomicfu/parking/ParkingDelegator.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/jvmMain/kotlin/kotlinx/atomicfu/parking/JvmParkingDelegator.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/androidNative32BitMain/kotlin/kotlinx/atomicfu/parking/PosixParkingDelegator.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/androidNative64BitMain/kotlin/kotlinx/atomicfu/parking/PosixParkingDelegator.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/androidNative32BitMain/kotlin/kotlinx/atomicfu/parking/PosixParkingDelegator.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/androidNative32BitMain/kotlin/kotlinx/atomicfu/parking/PosixParkingDelegator.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/jsAndWasmSharedMain/kotlin/kotlinx/atomicfu/parking/KThread.kt
Outdated
Show resolved
Hide resolved
… Commonizing is difficult due to absence of common threading api.
…arking behaviour on jvm and native.
atomicfu/src/concurrentMain/kotlin/kotlinx/atomicfu/parking/ThreadParker.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/jvmMain/kotlin/kotlinx/atomicfu/parking/JvmParkingDelegator.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/jvmMain/kotlin/kotlinx/atomicfu/parking/KThread.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/androidNative32BitMain/kotlin/kotlinx/atomicfu/parking/PosixParkingDelegator.kt
Outdated
Show resolved
Hide resolved
…ceSets after .convert() trick to commonize 32 and 64 bit sourceSets.
…variants. Also added to linuxMain sourceSet
atomicfu/src/mingwMain/kotlin/kotlinx/atomicfu/parking/PosixParkingDelegator.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/androidNativeMain/kotlin/kotlinx/atomicfu/parking/PosixParkingDelegator.kt
Outdated
Show resolved
Hide resolved
…, linux and android native) which is ~68 years.
atomicfu/src/jvmMain/kotlin/kotlinx/atomicfu/parking/JvmParkingDelegator.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/mingwMain/kotlin/kotlinx/atomicfu/parking/PosixParkingDelegator.kt
Show resolved
Hide resolved
atomicfu/src/nativeUnixLikeMain/kotlin/kotlinx/atomicfu/parking/PosixParkingDelegator.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/concurrentMain/kotlin/kotlinx/atomicfu/parking/ParkingDelegator.kt
Outdated
Show resolved
Hide resolved
val nanos = Random.nextLong() | ||
val updatedTime = currentTimeInSeconds.addNanosToSeconds(nanos) | ||
if (nanos > 0) assertTrue { updatedTime > currentTimeInSeconds } | ||
if (nanos < 0) assertTrue { updatedTime < currentTimeInSeconds } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably makes more sense to assert in the code that the negative number of nanos never even enters addNanosToSeconds
. No reason to test the code path that will never be taken.
atomicfu/src/jvmMain/kotlin/kotlinx/atomicfu/parking/JvmParkingDelegator.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/nativeTest/kotlin/kotlinx/atomicfu/parking/TestThread.native.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/nativeMain/kotlin/kotlinx/atomicfu/parking/PosixParkingDelegator.kt
Outdated
Show resolved
Hide resolved
internal inline fun callAndVerifyNative(vararg expectedReturn: Int, block: () -> Int): Int { | ||
val returnStatus = block() | ||
if (expectedReturn.all { it != returnStatus }) | ||
throw IllegalStateException("Calling native, expected one return status of ${expectedReturn.joinToString(", ")}, but was $returnStatus") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd also print the contents of https://www.man7.org/linux/man-pages/man3/errno.3.html
atomicfu/src/mingwMain/kotlin/kotlinx/atomicfu/parking/PosixParkingDelegator.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/concurrentTest/kotlin/kotlinx/atomicfu/parking/TimeArithmeticTests.kt
Outdated
Show resolved
Hide resolved
val currentTimeInInt = currentTimeInSeconds.toInt() | ||
val nanos = Random.nextLong() | ||
val updatedTime = currentTimeInInt.addNanosToSeconds(nanos) | ||
if (nanos > 0) assertTrue { updatedTime > currentTimeInSeconds && updatedTime <= Int.MAX_VALUE } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A stronger version of the check: if the value does not clamp, the (updatedTime - currentTime) == nanos / 1_000_000_000
; if the value clamps, it's because as a Long
value, it would exceed an Int
. This describes what we want from addNanosToSeconds
more precisely. As is, when I write internal fun Long.addNanosToSeconds(nanos: Long): Long = this + nanos / 2_000_000_000
, for example, no tests fail.
Allows to pause and resume thread execution. On native platforms it is based on pthread_cond_wait, on JVM it uses LockSupport.
Threads can be pre unparked (calling unpark before park cancels the parking operation). And thread can be parker with a timeout.
Suspend the current thread by calling:
Parker.park()
Resume a thread by calling:
Parker.unpark(thread)
Get current thread reference by calling:
Parking with timout of 500 nano seconds: