-
Notifications
You must be signed in to change notification settings - Fork 67
Mutex for Kotlin Common #494
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c879446
[WIP]
bbrockbernd 11f0221
Fix Mutex.withLock: unlock() after exception, allow non-local returns…
bbrockbernd c1d9a4f
FIX JS reentrancy bug.
bbrockbernd 313f148
Update apidump and fix actual Mutex constructor
bbrockbernd 8abc832
Change test to posix
bbrockbernd 3108b5f
Update apidump
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
8 changes: 8 additions & 0 deletions
8
...icfu/src/androidNativeMain/kotlin/kotlinx/atomicfu/locks/FutexParkingDelegator.linux64.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,8 @@ | ||
package kotlinx.atomicfu.locks | ||
|
||
internal actual object FutexParkingDelegator: ParkingDelegator { | ||
actual override fun createFutexPtr(): Long = PosixParkingDelegator.createFutexPtr() | ||
actual override fun wait(futexPrt: Long): Boolean = PosixParkingDelegator.wait(futexPrt) | ||
actual override fun wake(futexPrt: Long): Int = PosixParkingDelegator.wake(futexPrt) | ||
actual override fun manualDeallocate(futexPrt: Long) = PosixParkingDelegator.manualDeallocate(futexPrt) | ||
} |
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
36 changes: 36 additions & 0 deletions
36
atomicfu/src/appleMain/kotlin/kotlinx/atomicfu/locks/FutexParkingDelegator.apple.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,36 @@ | ||
package kotlinx.atomicfu.locks | ||
|
||
import kotlinx.cinterop.* | ||
import platform.darwin.UInt32 | ||
import platform.darwin.UInt64Var | ||
import platform.darwin.ulock.__ulock_wait | ||
import platform.darwin.ulock.__ulock_wake | ||
|
||
@OptIn(ExperimentalForeignApi::class) | ||
internal actual object FutexParkingDelegator: ParkingDelegator { | ||
actual override fun createFutexPtr(): Long { | ||
val signal = nativeHeap.alloc<UInt64Var>() | ||
signal.value = 0u | ||
return signal.ptr.toLong() | ||
} | ||
|
||
actual override fun wait(futexPrt: Long): Boolean { | ||
val cPointer = futexPrt.toCPointer<UInt64Var>() ?: throw IllegalStateException("Could not create C Pointer from futex ref") | ||
val result = __ulock_wait(UL_COMPARE_AND_WAIT, cPointer, 0u, 0u) | ||
nativeHeap.free(cPointer) | ||
// THere is very little information about ulock so not sure what returned int stands for an interrupt | ||
// In any case it should be 0 | ||
return result != 0 | ||
} | ||
|
||
actual override fun wake(futexPrt: Long): Int { | ||
return __ulock_wake(UL_COMPARE_AND_WAIT, futexPrt.toCPointer<UInt64Var>(), 0u) | ||
} | ||
|
||
actual override fun manualDeallocate(futexPrt: Long) { | ||
val cPointer = futexPrt.toCPointer<UInt64Var>() ?: throw IllegalStateException("Could not create C Pointer from futex ref") | ||
nativeHeap.free(cPointer) | ||
} | ||
|
||
private const val UL_COMPARE_AND_WAIT: UInt32 = 1u | ||
} |
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
20 changes: 20 additions & 0 deletions
20
atomicfu/src/commonMain/kotlin/kotlinx/atomicfu/locks/Mutex.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,20 @@ | ||
package kotlinx.atomicfu.locks | ||
|
||
/** | ||
* Multiplatform mutex. | ||
* On native based on futex(-like) system calls. | ||
* On JVM delegates to ReentrantLock. | ||
*/ | ||
expect class Mutex { | ||
fun isLocked(): Boolean | ||
fun tryLock(): Boolean | ||
fun lock() | ||
fun unlock() | ||
} | ||
|
||
fun <T> Mutex.withLock(block: () -> T): T { | ||
lock() | ||
val result = block() | ||
unlock() | ||
return result | ||
} | ||
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.