Scoop API

scoop:scoop.platform:0.1.0

src/concurrent/mutex.scoop

Selected production source · src/concurrent/mutex.scoop

1package scoop.platform.concurrent
3import scoop.platform.io.failNative
5/**
6 * An owned native mutex. Pair lock/unlock on the owning thread or use withLock; dispose only when
7 * no operation or held lock remains.
8 */
9public class Mutex: Disposable {
10 private var native: Ptr<Unit>? = None
11 private val active: AtomicLong = AtomicLong(0L)
12 /**
13 * Whether the native mutex has been released.
14 */
15 public val isDisposed: Boolean get() = native == None
17 public constructor() {
18 @Unsafe
19 {
20 val(pointer, error) = nativeMutexCreate()
21 if (pointer == None)
22 failNative("create mutex", error)
23 native = pointer
24 }
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 pointer
31 }
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 @Unsafe
54 {
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 @Unsafe
71 {
72 return nativeMutexTryLock(pointer).check("mutex try lock") == 0
73 }
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 @Unsafe
87 {
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 return
115 if (active.load(MemoryOrder.Relaxed) != 0L)
116 throw IllegalStateException(Some("mutex operation is active"))
117 @Unsafe
118 {
119 nativeMutexDestroy(native!!).check("dispose mutex")
120 native = None
121 }
122 }
124 release {
125 when (native) {
126 case Some(val pointer) -> @Unsafe
127 {
128 nativeMutexDestroy(pointer)
129 }
130 case None -> {}
131 }
132 }
135@Extern(name = "scoop_platform_mutex_create", captureErrno = true)
136private fun nativeMutexCreate(): (Ptr<Unit>?, Int)
138@Extern(name = "scoop_platform_mutex_lock")
139private fun nativeMutexLock(mutex: Ptr<Unit>, attempt: Boolean): NativeSyncResult
141@GCLeaf
142@Extern(name = "scoop_platform_mutex_try_lock")
143private fun nativeMutexTryLock(mutex: Ptr<Unit>): NativeSyncResult
145@GCLeaf
146@Extern(name = "scoop_platform_mutex_unlock")
147private fun nativeMutexUnlock(mutex: Ptr<Unit>): NativeSyncResult
149@Extern(name = "scoop_platform_mutex_destroy")
150private fun nativeMutexDestroy(mutex: Ptr<Unit>): NativeSyncResult