-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tidy up our synchronization code (#413)
* Confine all interop signatures into NativeMutexNode to simplify further working and maintenance * Get rid of mutex_node_t
- Loading branch information
Showing
3 changed files
with
80 additions
and
40 deletions.
There are no files selected for viewing
This file contains 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
51 changes: 51 additions & 0 deletions
51
atomicfu/src/nativeMain/kotlin/kotlinx/atomicfu/locks/PosixLockSupport.kt
This file contains 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,51 @@ | ||
package kotlinx.atomicfu.locks | ||
|
||
import interop.* | ||
import kotlinx.cinterop.* | ||
import platform.posix.* | ||
import kotlin.concurrent.* | ||
|
||
public class NativeMutexNode { | ||
private val mutex: CPointer<lock_support_t> = lock_support_init()!! | ||
|
||
internal var next: NativeMutexNode? = null | ||
|
||
fun lock() { | ||
interop.lock(mutex) | ||
} | ||
|
||
fun unlock() { | ||
interop.unlock(mutex) | ||
} | ||
} | ||
|
||
/** | ||
* This is a trivial counter-part of NativeMutexNode that does not rely on interop.def | ||
* The problem is, commonizer cannot commonize pthreads, thus this declaration should be duplicated | ||
* over multiple Native source-sets to work properly | ||
*/ | ||
//public class NativeMutexNode { | ||
// | ||
// @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) } | ||
// | ||
// internal var next: NativeMutexNode? = null | ||
// | ||
// 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) | ||
// } | ||
// | ||
// fun unlock() { | ||
// pthread_mutex_lock(pMutex.ptr) | ||
// isLocked = false | ||
// pthread_cond_broadcast(pCond.ptr) | ||
// pthread_mutex_unlock(pMutex.ptr) | ||
// } | ||
//} |
This file contains 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