-
Notifications
You must be signed in to change notification settings - Fork 65
Thread parking for Kotlin/Common #498
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
Open
bbrockbernd
wants to merge
5
commits into
develop
Choose a base branch
from
native-thread-parking
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c7df10f
Thread parking for kotlin/common
bbrockbernd 0d9ea07
Fix after rebase
bbrockbernd 524811c
PR comments: docs and naming, default return value for `callAndVerify…
bbrockbernd 32217bd
Timed parking test improvements: parameterize tests, more slack in ti…
bbrockbernd 0260de2
Formatting
bbrockbernd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 0 additions & 50 deletions
50
atomicfu/src/androidNative64BitMain/kotlin/kotlinx/atomicfu/locks/NativeMutexNode.kt
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
atomicfu/src/androidNativeMain/kotlin/kotlinx/atomicfu/locks/PosixParkingDelegator.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package kotlinx.atomicfu.locks | ||
|
||
import kotlinx.cinterop.* | ||
import platform.posix.* | ||
|
||
@OptIn(ExperimentalForeignApi::class, UnsafeNumber::class) | ||
internal actual object ParkingDelegator { | ||
actual fun createRef(): ParkingData { | ||
val mut = nativeHeap.alloc<pthread_mutex_t>().ptr | ||
val cond = nativeHeap.alloc<pthread_cond_t>().ptr | ||
val attr = nativeHeap.alloc<pthread_condattr_tVar>().ptr | ||
callAndVerify { pthread_mutex_init(mut, null) } | ||
callAndVerify { pthread_condattr_init(attr) } | ||
callAndVerify { pthread_condattr_setclock(attr, CLOCK_MONOTONIC) } | ||
callAndVerify { pthread_cond_init(cond, attr) } | ||
|
||
callAndVerify { pthread_condattr_destroy(attr) } | ||
nativeHeap.free(attr) | ||
return ParkingData(mut, cond) | ||
} | ||
|
||
actual inline fun wait(ref: ParkingData, shouldWait: () -> Boolean) { | ||
callAndVerify { pthread_mutex_lock(ref.mut) } | ||
try { | ||
if (shouldWait()) callAndVerify { pthread_cond_wait(ref.cond, ref.mut) } | ||
} finally { | ||
callAndVerify { pthread_mutex_unlock(ref.mut) } | ||
} | ||
} | ||
|
||
actual inline fun timedWait(ref: ParkingData, nanos: Long, shouldWait: () -> Boolean): Unit = memScoped { | ||
val ts = alloc<timespec>().ptr | ||
|
||
// Add nanos to current time | ||
callAndVerify { clock_gettime(CLOCK_MONOTONIC.convert(), ts) } | ||
ts.pointed.tv_sec = ts.pointed.tv_sec.addNanosToSeconds(nanos) | ||
ts.pointed.tv_nsec = (ts.pointed.tv_nsec + nanos % 1_000_000_000).convert() | ||
//Fix overflow | ||
if (ts.pointed.tv_nsec >= 1_000_000_000) { | ||
ts.pointed.tv_sec = ts.pointed.tv_sec.addNanosToSeconds(1_000_000_000) | ||
ts.pointed.tv_nsec -= 1_000_000_000 | ||
} | ||
callAndVerify { pthread_mutex_lock(ref.mut) } | ||
try { | ||
if (shouldWait()) callAndVerify(0, ETIMEDOUT) { pthread_cond_timedwait(ref.cond, ref.mut, ts) } | ||
} finally { | ||
callAndVerify { pthread_mutex_unlock(ref.mut) } | ||
} | ||
} | ||
|
||
actual fun wake(ref: ParkingData) { | ||
callAndVerify { pthread_mutex_lock(ref.mut) } | ||
try { | ||
callAndVerify { pthread_cond_signal(ref.cond) } | ||
} finally { | ||
callAndVerify { pthread_mutex_unlock(ref.mut) } | ||
} | ||
} | ||
|
||
actual fun destroyRef(ref: ParkingData) { | ||
callAndVerify { pthread_mutex_destroy(ref.mut) } | ||
callAndVerify { pthread_cond_destroy(ref.cond) } | ||
nativeHeap.free(ref.mut) | ||
nativeHeap.free(ref.cond) | ||
} | ||
} | ||
|
||
internal actual class ParkingData @OptIn(UnsafeNumber::class) constructor( | ||
val mut: CPointer<pthread_mutex_t>, | ||
val cond: CPointer<pthread_cond_t> | ||
) |
13 changes: 13 additions & 0 deletions
13
atomicfu/src/appleMain/kotlin/kotlinx/atomicfu/locks/PosixParkingDelegator.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package kotlinx.atomicfu.locks | ||
|
||
import kotlinx.cinterop.CPointer | ||
import platform.posix.CLOCK_REALTIME | ||
import platform.posix.* | ||
|
||
// CLOCK_REALTIME should be equal to CLOCK_SYSTEM on darwin. | ||
// https://github.com/apple/darwin-xnu/blob/2ff845c2e033bd0ff64b5b6aa6063a1f8f65aa32/osfmk/mach/clock_types.h#L70-L73 | ||
// Where CLOCK_CALENDAR is the time from epoch. | ||
internal actual val posixGetTimeClockId: Int | ||
get() = CLOCK_REALTIME | ||
|
||
actual fun pthreadCondAttrSetClock(attr: CPointer<pthread_condattr_t>): Int = 0 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Note: this is irritatingly close to
pthread_condattr_t
, which is used by the other Unix-like platforms, but after several attempts, I did not manage to find an incantation that would allow avoiding the code duplication forandroidNativeMain
.