-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
205 additions
and
120 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
31 changes: 31 additions & 0 deletions
31
atomicfu/src/androidNative32BitMain/kotlin/kotlinx/atomicfu/locks/NativeMutexNode.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,31 @@ | ||
package kotlinx.atomicfu.locks | ||
|
||
import kotlinx.cinterop.* | ||
import platform.posix.* | ||
import kotlin.concurrent.Volatile | ||
|
||
public actual 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 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) | ||
} | ||
|
||
actual fun unlock() { | ||
pthread_mutex_lock(pMutex.ptr) | ||
isLocked = false | ||
pthread_cond_broadcast(pCond.ptr) | ||
pthread_mutex_unlock(pMutex.ptr) | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
atomicfu/src/androidNative64BitMain/kotlin/kotlinx/atomicfu/locks/NativeMutexNode.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,31 @@ | ||
package kotlinx.atomicfu.locks | ||
|
||
import kotlinx.cinterop.* | ||
import platform.posix.* | ||
import kotlin.concurrent.Volatile | ||
|
||
public actual 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 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) | ||
} | ||
|
||
actual fun unlock() { | ||
pthread_mutex_lock(pMutex.ptr) | ||
isLocked = false | ||
pthread_cond_broadcast(pCond.ptr) | ||
pthread_mutex_unlock(pMutex.ptr) | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
atomicfu/src/mingwMain/kotlin/kotlinx/atomicfu/locks/NativeMutexNode.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,31 @@ | ||
package kotlinx.atomicfu.locks | ||
|
||
import kotlinx.cinterop.* | ||
import platform.posix.* | ||
import kotlin.concurrent.Volatile | ||
|
||
public actual class NativeMutexNode { | ||
|
||
@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) } | ||
|
||
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) | ||
} | ||
|
||
actual 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +0,0 @@ | ||
--- | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <pthread.h> | ||
|
||
typedef struct lock_support { | ||
volatile int locked; | ||
pthread_mutex_t mutex; | ||
pthread_cond_t cond; | ||
} lock_support_t; | ||
|
||
lock_support_t* lock_support_init() { | ||
lock_support_t * ls = (lock_support_t *) malloc(sizeof(lock_support_t)); | ||
ls->locked = 0; | ||
pthread_mutex_init(&ls->mutex, NULL); | ||
pthread_cond_init(&ls->cond, NULL); | ||
return ls; | ||
} | ||
void lock(lock_support_t* ls) { | ||
pthread_mutex_lock(&ls->mutex); | ||
while (ls->locked == 1) { // wait till locked are available | ||
pthread_cond_wait(&ls->cond, &ls->mutex); | ||
} | ||
ls->locked = 1; | ||
pthread_mutex_unlock(&ls->mutex); | ||
} | ||
|
||
void unlock(lock_support_t* ls) { | ||
pthread_mutex_lock(&ls->mutex); | ||
ls->locked = 0; | ||
pthread_cond_broadcast(&ls->cond); | ||
pthread_mutex_unlock(&ls->mutex); | ||
} | ||
9 changes: 9 additions & 0 deletions
9
atomicfu/src/nativeMain/kotlin/kotlinx/atomicfu/locks/NativeMutexNode.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,9 @@ | ||
package kotlinx.atomicfu.locks | ||
|
||
public expect class NativeMutexNode() { | ||
internal var next: NativeMutexNode? | ||
|
||
public fun lock() | ||
|
||
public fun unlock() | ||
} |
51 changes: 0 additions & 51 deletions
51
atomicfu/src/nativeMain/kotlin/kotlinx/atomicfu/locks/PosixLockSupport.kt
This file was deleted.
Oops, something went wrong.
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
31 changes: 31 additions & 0 deletions
31
atomicfu/src/nativeUnixLikeMain/kotlin/kotlinx/atomicfu/locks/NativeMutexNode.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,31 @@ | ||
package kotlinx.atomicfu.locks | ||
|
||
import kotlinx.cinterop.* | ||
import platform.posix.* | ||
import kotlin.concurrent.* | ||
|
||
public actual 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 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) | ||
} | ||
|
||
actual fun unlock() { | ||
pthread_mutex_lock(pMutex.ptr) | ||
isLocked = false | ||
pthread_cond_broadcast(pCond.ptr) | ||
pthread_mutex_unlock(pMutex.ptr) | ||
} | ||
} |