-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from touchlab/kpg/win_X86
Kpg/win x86
- Loading branch information
Showing
4 changed files
with
69 additions
and
2 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
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
File renamed without changes.
54 changes: 54 additions & 0 deletions
54
sqliter-driver/src/mingwX86Main/kotlin/co/touchlab/sqliter/concurrency/Lock.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,54 @@ | ||
package co.touchlab.sqliter.concurrency | ||
|
||
import kotlinx.cinterop.Arena | ||
import kotlinx.cinterop.alloc | ||
import kotlinx.cinterop.ptr | ||
import platform.posix.PTHREAD_MUTEX_RECURSIVE | ||
import platform.posix.pthread_mutex_destroy | ||
import platform.posix.pthread_mutex_init | ||
import platform.posix.pthread_mutex_lock | ||
import platform.posix.pthread_mutex_tVar | ||
import platform.posix.pthread_mutex_trylock | ||
import platform.posix.pthread_mutex_unlock | ||
import platform.posix.pthread_mutexattr_destroy | ||
import platform.posix.pthread_mutexattr_init | ||
import platform.posix.pthread_mutexattr_settype | ||
import platform.posix.pthread_mutexattr_tVar | ||
import kotlin.native.concurrent.freeze | ||
|
||
/** | ||
* A simple lock. | ||
* Implementations of this class should be re-entrant. | ||
*/ | ||
actual class Lock actual constructor() { | ||
private val arena = Arena() | ||
private val attr = arena.alloc<pthread_mutexattr_tVar>() | ||
private val mutex = arena.alloc<pthread_mutex_tVar>() | ||
|
||
init { | ||
pthread_mutexattr_init(attr.ptr) | ||
pthread_mutexattr_settype(attr.ptr, PTHREAD_MUTEX_RECURSIVE.toInt()) | ||
pthread_mutex_init(mutex.ptr, attr.ptr) | ||
freeze() | ||
} | ||
|
||
actual fun lock() { | ||
pthread_mutex_lock(mutex.ptr) | ||
} | ||
|
||
actual fun unlock() { | ||
pthread_mutex_unlock(mutex.ptr) | ||
} | ||
|
||
actual fun tryLock(): Boolean = pthread_mutex_trylock(mutex.ptr) == 0 | ||
|
||
fun internalClose() { | ||
pthread_mutex_destroy(mutex.ptr) | ||
pthread_mutexattr_destroy(attr.ptr) | ||
arena.clear() | ||
} | ||
} | ||
|
||
actual inline fun Lock.close() { | ||
internalClose() | ||
} |