-
Notifications
You must be signed in to change notification settings - Fork 59
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
Tidy up our synchronization code #413
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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
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.
Do you expect commonizer being able to unlock this implementation one day? Do we really need to keep it here?
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.
This, or that we'll decide to give up on the commonizer and just copy-paste it over a few native sourcesets