-
Notifications
You must be signed in to change notification settings - Fork 67
Back-port the Apple lock (without the Apple-specific qos parts) to other Posix platforms. #517
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3308593
Align apple implementation with other native platforms: Move NativeMu…
stefanhaustein 72fc6c2
Copyright alignment, make the mutex node internal
stefanhaustein 61ea2ea
Back-port the apple lock (without the apple-specific qos parts) to ot…
stefanhaustein 63127e5
Merge branch 'Kotlin:develop' into develop
stefanhaustein 8eab6ce
Share the bulk of Synchronized.kt for all native platforms, pushing t…
stefanhaustein ee2ab0b
Merge branch 'Kotlin:develop' into develop
stefanhaustein 440cecc
Better align error behavior with previous version; make added methods…
stefanhaustein 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
54 changes: 33 additions & 21 deletions
54
atomicfu/src/androidNative32BitMain/kotlin/kotlinx/atomicfu/locks/NativeMutexNode.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 |
---|---|---|
@@ -1,31 +1,43 @@ | ||
/* | ||
* Copyright 2025 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
package kotlinx.atomicfu.locks | ||
|
||
import kotlinx.cinterop.* | ||
import kotlinx.cinterop.Arena | ||
import kotlinx.cinterop.ExperimentalForeignApi | ||
import kotlinx.cinterop.alloc | ||
import kotlinx.cinterop.ptr | ||
import platform.posix.* | ||
import kotlin.concurrent.Volatile | ||
|
||
public actual class NativeMutexNode { | ||
@OptIn(ExperimentalForeignApi::class) | ||
actual class NativeMutexNode { | ||
actual var next: NativeMutexNode? = null | ||
|
||
@Volatile | ||
private var isLocked = false | ||
private val pMutex = nativeHeap.alloc<pthread_mutex_t>().apply { pthread_mutex_init(ptr, null) } | ||
private val pCond = nativeHeap.alloc<pthread_cond_t>().apply { pthread_cond_init(ptr, null) } | ||
private val arena: Arena = Arena() | ||
private val cond: pthread_cond_t = arena.alloc() | ||
private val mutex: pthread_mutex_t = arena.alloc() | ||
private val attr: pthread_mutexattr_tVar = arena.alloc() | ||
|
||
internal actual var next: NativeMutexNode? = null | ||
|
||
actual fun lock() { | ||
pthread_mutex_lock(pMutex.ptr) | ||
while (isLocked) { // wait till locked are available | ||
pthread_cond_wait(pCond.ptr, pMutex.ptr) | ||
} | ||
isLocked = true | ||
pthread_mutex_unlock(pMutex.ptr) | ||
init { | ||
require(pthread_cond_init(cond.ptr, null) == 0) | ||
require(pthread_mutexattr_init(attr.ptr) == 0) | ||
require(pthread_mutexattr_settype(attr.ptr, PTHREAD_MUTEX_ERRORCHECK.toInt()) == 0) | ||
require(pthread_mutex_init(mutex.ptr, attr.ptr) == 0) | ||
} | ||
|
||
actual fun unlock() { | ||
pthread_mutex_lock(pMutex.ptr) | ||
isLocked = false | ||
pthread_cond_broadcast(pCond.ptr) | ||
pthread_mutex_unlock(pMutex.ptr) | ||
actual fun lock() = require(pthread_mutex_lock(mutex.ptr) == 0) | ||
|
||
actual fun unlock() = require(pthread_mutex_unlock(mutex.ptr) == 0) | ||
|
||
actual fun wait(lockOwner: Long) = require(pthread_cond_wait(cond.ptr, mutex.ptr) == 0) | ||
|
||
actual fun notify() = require(pthread_cond_signal(cond.ptr) == 0) | ||
|
||
actual fun dispose() { | ||
pthread_cond_destroy(cond.ptr) | ||
pthread_mutex_destroy(mutex.ptr) | ||
pthread_mutexattr_destroy(attr.ptr) | ||
arena.clear() | ||
} | ||
} | ||
} |
55 changes: 33 additions & 22 deletions
55
atomicfu/src/androidNative64BitMain/kotlin/kotlinx/atomicfu/locks/NativeMutexNode.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 |
---|---|---|
@@ -1,31 +1,42 @@ | ||
/* | ||
* Copyright 2025 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
package kotlinx.atomicfu.locks | ||
|
||
import kotlinx.cinterop.* | ||
import kotlinx.cinterop.Arena | ||
import kotlinx.cinterop.ExperimentalForeignApi | ||
import kotlinx.cinterop.alloc | ||
import kotlinx.cinterop.ptr | ||
import platform.posix.* | ||
import kotlin.concurrent.Volatile | ||
|
||
public actual class NativeMutexNode { | ||
@OptIn(ExperimentalForeignApi::class) | ||
actual class NativeMutexNode { | ||
actual var next: NativeMutexNode? = null | ||
|
||
@Volatile | ||
private var isLocked = false | ||
private val pMutex = nativeHeap.alloc<pthread_mutex_t>().apply { pthread_mutex_init(ptr, null) } | ||
private val pCond = nativeHeap.alloc<pthread_cond_t>().apply { pthread_cond_init(ptr, null) } | ||
private val arena: Arena = Arena() | ||
private val cond: pthread_cond_t = arena.alloc() | ||
private val mutex: pthread_mutex_t = arena.alloc() | ||
private val attr: pthread_mutexattr_tVar = arena.alloc() | ||
|
||
internal actual var next: NativeMutexNode? = null | ||
|
||
actual fun lock() { | ||
pthread_mutex_lock(pMutex.ptr) | ||
while (isLocked) { // wait till locked are available | ||
pthread_cond_wait(pCond.ptr, pMutex.ptr) | ||
} | ||
isLocked = true | ||
pthread_mutex_unlock(pMutex.ptr) | ||
init { | ||
require(pthread_cond_init(cond.ptr, null) == 0) | ||
require(pthread_mutexattr_init(attr.ptr) == 0) | ||
require(pthread_mutexattr_settype(attr.ptr, PTHREAD_MUTEX_ERRORCHECK.toInt()) == 0) | ||
require(pthread_mutex_init(mutex.ptr, attr.ptr) == 0) | ||
} | ||
|
||
actual fun unlock() { | ||
pthread_mutex_lock(pMutex.ptr) | ||
isLocked = false | ||
pthread_cond_broadcast(pCond.ptr) | ||
pthread_mutex_unlock(pMutex.ptr) | ||
actual fun lock() = require(pthread_mutex_lock(mutex.ptr) == 0) | ||
|
||
actual fun unlock() = require(pthread_mutex_unlock(mutex.ptr) == 0) | ||
|
||
actual fun wait(lockOwner: Long) = require(pthread_cond_wait(cond.ptr, mutex.ptr) == 0) | ||
|
||
actual fun notify() = require(pthread_cond_signal(cond.ptr) == 0) | ||
|
||
actual fun dispose() { | ||
pthread_cond_destroy(cond.ptr) | ||
pthread_mutex_destroy(mutex.ptr) | ||
pthread_mutexattr_destroy(attr.ptr) | ||
arena.clear() | ||
} | ||
} | ||
} |
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
10 changes: 10 additions & 0 deletions
10
atomicfu/src/appleMain/kotlin/kotlinx/atomicfu/locks/ThreadId.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,10 @@ | ||
/* | ||
* Copyright 2025 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.atomicfu.locks | ||
|
||
import kotlinx.cinterop.toLong | ||
import platform.posix.pthread_self | ||
|
||
actual fun createThreadId() = pthread_self().toLong() |
55 changes: 33 additions & 22 deletions
55
atomicfu/src/mingwMain/kotlin/kotlinx/atomicfu/locks/NativeMutexNode.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 |
---|---|---|
@@ -1,31 +1,42 @@ | ||
/* | ||
* Copyright 2025 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
package kotlinx.atomicfu.locks | ||
|
||
import kotlinx.cinterop.* | ||
import kotlinx.cinterop.Arena | ||
import kotlinx.cinterop.ExperimentalForeignApi | ||
import kotlinx.cinterop.alloc | ||
import kotlinx.cinterop.ptr | ||
import platform.posix.* | ||
import kotlin.concurrent.Volatile | ||
|
||
public actual class NativeMutexNode { | ||
@OptIn(ExperimentalForeignApi::class) | ||
actual class NativeMutexNode { | ||
actual var next: NativeMutexNode? = null | ||
|
||
@Volatile | ||
private var isLocked = false | ||
private val pMutex = nativeHeap.alloc<pthread_mutex_tVar>().apply { pthread_mutex_init(ptr, null) } | ||
private val pCond = nativeHeap.alloc<pthread_cond_tVar>().apply { pthread_cond_init(ptr, null) } | ||
private val arena: Arena = Arena() | ||
private val cond: pthread_cond_tVar = arena.alloc() | ||
private val mutex: pthread_mutex_tVar = arena.alloc() | ||
private val attr: pthread_mutexattr_tVar = arena.alloc() | ||
|
||
internal actual var next: NativeMutexNode? = null | ||
|
||
actual fun lock() { | ||
pthread_mutex_lock(pMutex.ptr) | ||
while (isLocked) { // wait till locked are available | ||
pthread_cond_wait(pCond.ptr, pMutex.ptr) | ||
} | ||
isLocked = true | ||
pthread_mutex_unlock(pMutex.ptr) | ||
init { | ||
require(pthread_cond_init(cond.ptr, null) == 0) | ||
require(pthread_mutexattr_init(attr.ptr) == 0) | ||
require(pthread_mutexattr_settype(attr.ptr, PTHREAD_MUTEX_ERRORCHECK) == 0) | ||
require(pthread_mutex_init(mutex.ptr, attr.ptr) == 0) | ||
} | ||
|
||
actual fun unlock() { | ||
pthread_mutex_lock(pMutex.ptr) | ||
isLocked = false | ||
pthread_cond_broadcast(pCond.ptr) | ||
pthread_mutex_unlock(pMutex.ptr) | ||
actual fun lock() = require(pthread_mutex_lock(mutex.ptr) == 0) | ||
|
||
actual fun unlock() = require(pthread_mutex_unlock(mutex.ptr) == 0) | ||
|
||
actual fun wait(lockOwner: Long) = require(pthread_cond_wait(cond.ptr, mutex.ptr) == 0) | ||
|
||
actual fun notify() = require(pthread_cond_signal(cond.ptr) == 0) | ||
|
||
actual fun dispose() { | ||
pthread_cond_destroy(cond.ptr) | ||
pthread_mutex_destroy(mutex.ptr) | ||
pthread_mutexattr_destroy(attr.ptr) | ||
arena.clear() | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
atomicfu/src/nativeMain/kotlin/kotlinx/atomicfu/locks/NativeMutexNode.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,21 @@ | ||
/* | ||
* Copyright 2025 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
package kotlinx.atomicfu.locks | ||
|
||
|
||
public expect class NativeMutexNode() { | ||
|
||
internal var next: NativeMutexNode? | ||
|
||
public fun lock() | ||
|
||
public fun unlock() | ||
|
||
// The lockOwner is used for qos donation on iOS | ||
public fun wait(lockOwner: Long) | ||
stefanhaustein marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public fun notify() | ||
|
||
public fun dispose() | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.