scoop:scoop.platform:0.1.0
src/concurrent/mutex.scoop
Selected production source · src/concurrent/mutex.scoop
1
package scoop.platform.concurrent3
import scoop.platform.io.failNative5
/**6
* An owned native mutex. Pair lock/unlock on the owning thread or use withLock; dispose only when7
* no operation or held lock remains.8
*/9
public class Mutex: Disposable {10
private var native: Ptr<Unit>? = None11
private val active: AtomicLong = AtomicLong(0L)12
/**13
* Whether the native mutex has been released.14
*/15
public val isDisposed: Boolean get() = native == None17
public constructor() {18
@Unsafe19
{20
val(pointer, error) = nativeMutexCreate()21
if (pointer == None)22
failNative("create mutex", error)23
native = pointer24
}25
}27
private fun acquireNative(): Ptr<Unit> {28
val pointer = native ?: throw IllegalStateException(Some("mutex is disposed"))29
active.fetchAdd(1L, MemoryOrder.Relaxed)30
return pointer31
}33
private fun releaseNative() {34
active.fetchSub(1L, MemoryOrder.Relaxed)35
}37
internal fun <R> withNative(block: (Ptr<Unit>) -> R): R {38
val pointer = acquireNative()39
try {40
return block(pointer)41
}42
finally {43
releaseNative()44
}45
}47
/**48
* Blocks until this thread acquires the mutex.49
*/50
public fun lock() {51
val pointer = acquireNative()52
try {53
@Unsafe54
{55
if (nativeMutexTryLock(pointer).check("mutex lock") != 0)56
nativeMutexLock(pointer, false).check("mutex lock")57
}58
}59
finally {60
releaseNative()61
}62
}64
/**65
* Attempts acquisition without blocking and returns whether this thread acquired the mutex.66
*/67
public fun tryLock(): Boolean {68
val pointer = acquireNative()69
try {70
@Unsafe71
{72
return nativeMutexTryLock(pointer).check("mutex try lock") == 073
}74
}75
finally {76
releaseNative()77
}78
}80
/**81
* Releases the mutex held by this thread.82
*/83
public fun unlock() {84
val pointer = acquireNative()85
try {86
@Unsafe87
{88
nativeMutexUnlock(pointer).check("mutex unlock")89
}90
}91
finally {92
releaseNative()93
}94
}96
/**97
* Runs the block under the mutex and unlocks on both normal and exceptional exit.98
*/99
public fun <T> withLock(block: () -> T): T {100
lock()101
try {102
return block()103
}104
finally {105
unlock()106
}107
}109
/**110
* Destroys the native mutex when it is no longer in use.111
*/112
public override fun dispose() {113
if (native == None)114
return115
if (active.load(MemoryOrder.Relaxed) != 0L)116
throw IllegalStateException(Some("mutex operation is active"))117
@Unsafe118
{119
nativeMutexDestroy(native!!).check("dispose mutex")120
native = None121
}122
}124
release {125
when (native) {126
case Some(val pointer) -> @Unsafe127
{128
nativeMutexDestroy(pointer)129
}130
case None -> {}131
}132
}133
}135
@Extern(name = "scoop_platform_mutex_create", captureErrno = true)136
private fun nativeMutexCreate(): (Ptr<Unit>?, Int)138
@Extern(name = "scoop_platform_mutex_lock")139
private fun nativeMutexLock(mutex: Ptr<Unit>, attempt: Boolean): NativeSyncResult141
@GCLeaf142
@Extern(name = "scoop_platform_mutex_try_lock")143
private fun nativeMutexTryLock(mutex: Ptr<Unit>): NativeSyncResult145
@GCLeaf146
@Extern(name = "scoop_platform_mutex_unlock")147
private fun nativeMutexUnlock(mutex: Ptr<Unit>): NativeSyncResult149
@Extern(name = "scoop_platform_mutex_destroy")150
private fun nativeMutexDestroy(mutex: Ptr<Unit>): NativeSyncResult