Scoop API

scoop:scoop.platform:0.1.0

src/concurrent/condition.scoop

Selected production source · src/concurrent/condition.scoop

1package scoop.platform.concurrent
3import scoop.platform.io.failNative
4import scoop.time.Duration
6/**
7 * A native condition variable associated with one mutex. Wait while holding that mutex and recheck
8 * the application predicate after waking.
9 */
10public class ConditionVariable public constructor(
11 /**
12 * The mutex released during waiting and reacquired before return.
13 */
14 public val mutex: Mutex
15): Disposable {
16 private var native: Ptr<Unit>? = None
17 private val active: AtomicLong = AtomicLong(0L)
18 /**
19 * Whether native condition-variable state has been released.
20 */
21 public val isDisposed: Boolean get() = native == None
23 init {
24 native =
25 mutex.withNative {
26 _ -> @Unsafe
27 {
28 val(created, error) = nativeConditionCreate()
29 if (created == None)
30 failNative("create condition variable", error)
31 return@withNative created
32 }
33 }
34 }
36 private fun <R> withNative(block: (Ptr<Unit>) -> R): R {
37 if (native == None)
38 throw IllegalStateException(Some("condition variable is disposed"))
39 active.fetchAdd(1L, MemoryOrder.Relaxed)
40 try {
41 return block(native!!)
42 }
43 finally {
44 active.fetchSub(1L, MemoryOrder.Relaxed)
45 }
46 }
48 /**
49 * Atomically releases the held mutex while waiting, then reacquires it. The timed overload
50 * returns false on timeout; a wake does not prove the application predicate.
51 */
52 public fun await() {
53 wait(None)
54 }
55 /**
56 * Atomically releases the held mutex while waiting, then reacquires it. The timed overload
57 * returns false on timeout; a wake does not prove the application predicate.
58 */
59 public fun await(timeout: Duration): Boolean {
60 if (timeout.floorSeconds < 0L)
61 throw IllegalArgumentException(Some("negative condition timeout"))
62 return wait(Some(timeout))
63 }
65 private fun wait(timeout: Duration?): Boolean =
66 withNative {
67 condition -> mutex.withNative lockScope@{
68 lock -> @Unsafe
69 {
70 val result =
71 nativeConditionWait(
72 condition,
73 lock,
74 timeout != None,
75 timeout?.floorSeconds ?: 0L,
76 timeout?.nanosecondsOfSecond ?: 0
77 )
78 return@lockScope result.check("condition wait") == 0
79 }
80 }
81 }
83 /**
84 * Wakes one eligible waiter; callers must still protect the shared predicate with the
85 * associated mutex.
86 */
87 public fun signal() {
88 notify(false)
89 }
90 /**
91 * Wakes all eligible waiters so they can recheck their predicates.
92 */
93 public fun broadcast() {
94 notify(true)
95 }
96 private fun notify(all: Boolean) {
97 withNative {
98 pointer -> @Unsafe
99 {
100 nativeConditionSignal(pointer, all).check("condition notification")
101 }
102 }
103 }
105 /**
106 * Destroys the condition variable; disposal with an active wait is rejected.
107 */
108 public override fun dispose() {
109 if (native == None)
110 return
111 if (active.load(MemoryOrder.Relaxed) != 0L)
112 throw IllegalStateException(Some("condition wait is active"))
113 @Unsafe
114 {
115 nativeConditionDestroy(native!!).check("dispose condition variable")
116 native = None
117 }
118 }
120 release {
121 when (native) {
122 case Some(val pointer) -> @Unsafe
123 {
124 nativeConditionDestroy(pointer)
125 }
126 case None -> {}
127 }
128 }
131@Extern(name = "scoop_platform_condition_create", captureErrno = true)
132private fun nativeConditionCreate(): (Ptr<Unit>?, Int)
134@Extern(name = "scoop_platform_condition_wait")
135private fun nativeConditionWait(
136 condition: Ptr<Unit>,
137 mutex: Ptr<Unit>,
138 timed: Boolean,
139 seconds: Long,
140 nanoseconds: Int
141): NativeSyncResult
143@Extern(name = "scoop_platform_condition_signal")
144private fun nativeConditionSignal(condition: Ptr<Unit>, all: Boolean): NativeSyncResult
146@Extern(name = "scoop_platform_condition_destroy")
147private fun nativeConditionDestroy(condition: Ptr<Unit>): NativeSyncResult