-
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.
…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
Outdated
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.
atomicfu/src/concurrentTest/kotlin/kotlinx/atomicfu/parking/TimedParkingTest.kt
Outdated
Show resolved
Hide resolved
Refactor thread parking to reflect API design
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.
The implementation is almost ready to go, I believe. Good work! Only some documentation fixes are left, and there's maybe potential to simplify the code.
I haven't looked at the tests yet, though.
atomicfu/src/concurrentMain/kotlin/kotlinx/atomicfu/locks/ParkingSupport.kt
Outdated
Show resolved
Hide resolved
atomicfu/src/concurrentMain/kotlin/kotlinx/atomicfu/locks/ParkingSupport.kt
Outdated
Show resolved
Hide resolved
if (timout == Duration.INFINITE) threadLocalParkingHandle.parker.park() | ||
else threadLocalParkingHandle.parker.parkNanos(timout.toLong(DurationUnit.NANOSECONDS)) | ||
} | ||
actual fun parkUntil(deadline: TimeMark) = park(deadline.elapsedNow() * -1) |
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.
Question: can the code be simplified if we take parkUntil
as the fundamental operation and then implement parkNanos
on top of it? The idea is that POSIX exposes something that's similar to parkUntil
already, so waitWithDeadline
may be easier to write than timedWait
, and LockSupport.parkUntil
may be somewhat simpler than LockSupport.parkNanos
(due to the simpler "is it time to exit yet?" check).
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.
AFAIK, a TimeMark
is not directly convertible to nanos from epoch. The only way I have found so far is by somehow getting the current time from epoch and subtracting TimeMark.elapsedNow
(which is converting to duration.)
Additionally, to get current time in nanos from epoch (on native) we either need kotlinx-datetime
which doesn't seem to be more granular than milliseconds. Or rely on clock_gettime
from POSIX and convert TimeMark to duration, which is what we currently do.
But I might be missing some api?
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.
You're right, TimeMark
does not expose its relationship to absolute time.
kotlin.time.Instant
(which used to be in kotlinx-datetime
) does provide nanosecond precision, and in fact, delegates to clock_gettime
. So, with Kotlin 2.1.20, which introduced Instant
, it's possible to do this (pseudocode):
fun parkUntil(deadline: TimeMark) {
val estimatedTargetTime: Instant = Clock.System.now() - deadline.elapsedNow()
sleepUntil(instant.epochSeconds, instant.nanosecondsOfSecond)
}
The problem is, we're not on Kotlin 2.1.20, so we indeed don't have access to Instant
.
But! This gets me thinking: why do we need an Instant
at all? It's semantically wrong. In fact, we may have a subtle bug.
See, CLOCK_REALTIME
(https://man7.org/linux/man-pages/man2/clock_gettime.2.html), kotlin.time.Instant
, and by default, pthread_cond_timedwait
(https://docs.oracle.com/cd/E19120-01/open.solaris/816-5137/gfvid/index.html) all have to do with system time, that is, what your clock in the corner of the screen says. This clock may get out of sync, and then get back in sync when your system queries the Internet for the up-to-date time.
It seems like the following may happen:
- At 12:13:59, we park for three seconds. We are waiting until 12:14:02.
- One second later, at 12:14:00, we query the Internet and see that our clock is too fast and is into the future by two seconds.
- One second later, it's 12:13:59 again.
- Then, three seconds later, it's finally 12:14:02... but five seconds have passed.
TimeSource.Monotonic
, though, works with elapsed time, which is what we want.
Proposed fix: https://stackoverflow.com/a/14397906. With this, we don't need to extract epoch time at all.
What do you think?
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.
That is quite the edge case! However, the fix proposed relies on pthread_condattr_setclock
which is not available on darwin.
Also, I am wondering, is the kotlin TimeSource.Monotonic
synced to the posix CLOCK_MONOTONIC
?
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.
That is quite the edge case!
Indeed, but I'd estimate that every day, someone somewhere encounters this. Having clocks drift by a couple of seconds per day is typical.
not available on darwin.
Oh, ok. If you also can't think of a way to fix this there, we need to at least document that on Native, the system clock is used and is susceptible to such issues.
Also, I am wondering, is the kotlin
TimeSource.Monotonic
synced to the posixCLOCK_MONOTONIC
?
Kotlin/Native delegates to C++'s std::conditional<high_resolution_clock::is_steady, high_resolution_clock, steady_clock>
. I don't know how our C++ standard library implements those, but it's not necessarily a wrapper around POSIX.
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:
Resume a thread by calling:
Get current thread reference by calling: