Skip to content
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

Open
wants to merge 45 commits into
base: master
Choose a base branch
from
Open

Conversation

bbrockbernd
Copy link
Collaborator

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:

val thread = KThread.currentThread()

Parking with timout of 500 nano seconds:

Parker.parkNanos(500)

@bbrockbernd bbrockbernd marked this pull request as ready for review December 19, 2024 11:10
@dkhalanskyjb dkhalanskyjb self-requested a review February 6, 2025 10:59
@dkhalanskyjb dkhalanskyjb self-requested a review February 10, 2025 14:49
@dkhalanskyjb dkhalanskyjb self-requested a review February 10, 2025 15:18
…, linux and android native) which is ~68 years.
val nanos = Random.nextLong()
val updatedTime = currentTimeInSeconds.addNanosToSeconds(nanos)
if (nanos > 0) assertTrue { updatedTime > currentTimeInSeconds }
if (nanos < 0) assertTrue { updatedTime < currentTimeInSeconds }

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.

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")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

val currentTimeInInt = currentTimeInSeconds.toInt()
val nanos = Random.nextLong()
val updatedTime = currentTimeInInt.addNanosToSeconds(nanos)
if (nanos > 0) assertTrue { updatedTime > currentTimeInSeconds && updatedTime <= Int.MAX_VALUE }

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants