scoop:scoop.platform:0.1.0
src/concurrent/condition.scoop
Selected production source · src/concurrent/condition.scoop
1
package scoop.platform.concurrent3
import scoop.platform.io.failNative4
import scoop.time.Duration6
/**7
* A native condition variable associated with one mutex. Wait while holding that mutex and recheck8
* the application predicate after waking.9
*/10
public class ConditionVariable public constructor(11
/**12
* The mutex released during waiting and reacquired before return.13
*/14
public val mutex: Mutex15
): Disposable {16
private var native: Ptr<Unit>? = None17
private val active: AtomicLong = AtomicLong(0L)18
/**19
* Whether native condition-variable state has been released.20
*/21
public val isDisposed: Boolean get() = native == None23
init {24
native =25
mutex.withNative {26
_ -> @Unsafe27
{28
val(created, error) = nativeConditionCreate()29
if (created == None)30
failNative("create condition variable", error)31
return@withNative created32
}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 overload50
* 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 overload57
* 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 -> @Unsafe69
{70
val result =71
nativeConditionWait(72
condition,73
lock,74
timeout != None,75
timeout?.floorSeconds ?: 0L,76
timeout?.nanosecondsOfSecond ?: 077
)78
return@lockScope result.check("condition wait") == 079
}80
}81
}83
/**84
* Wakes one eligible waiter; callers must still protect the shared predicate with the85
* 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 -> @Unsafe99
{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
return111
if (active.load(MemoryOrder.Relaxed) != 0L)112
throw IllegalStateException(Some("condition wait is active"))113
@Unsafe114
{115
nativeConditionDestroy(native!!).check("dispose condition variable")116
native = None117
}118
}120
release {121
when (native) {122
case Some(val pointer) -> @Unsafe123
{124
nativeConditionDestroy(pointer)125
}126
case None -> {}127
}128
}129
}131
@Extern(name = "scoop_platform_condition_create", captureErrno = true)132
private fun nativeConditionCreate(): (Ptr<Unit>?, Int)134
@Extern(name = "scoop_platform_condition_wait")135
private fun nativeConditionWait(136
condition: Ptr<Unit>,137
mutex: Ptr<Unit>,138
timed: Boolean,139
seconds: Long,140
nanoseconds: Int141
): NativeSyncResult143
@Extern(name = "scoop_platform_condition_signal")144
private fun nativeConditionSignal(condition: Ptr<Unit>, all: Boolean): NativeSyncResult146
@Extern(name = "scoop_platform_condition_destroy")147
private fun nativeConditionDestroy(condition: Ptr<Unit>): NativeSyncResult