scoop:scoop.platform:0.1.0
src/concurrent/thread.scoop
Selected production source · src/concurrent/thread.scoop
1
package scoop.platform.concurrent3
import scoop.platform.io.checkNativeStatus4
import scoop.platform.io.failNative6
/**7
* A native thread identity suitable for equality and hashing within this process.8
*/9
public struct ThreadId(val value: ULong): Equals<ThreadId>, Hash {10
/**11
* Compares native thread identity values.12
*/13
public override operator fun equals(other: ThreadId): Boolean = value == other.value14
/**15
* Hashes the native thread identity.16
*/17
public override fun hash(): Long = value.hash()18
}20
/**21
* Returns the identity of the calling native thread.22
*/23
public fun currentThreadId(): ThreadId {24
@Unsafe25
{26
return ThreadId(nativeCurrentThreadId())27
}28
}30
/**31
* Offers the current thread's execution opportunity to the operating-system scheduler.32
*/33
public fun yield() {34
@Unsafe35
{36
val(status, error) = nativeYield()37
checkNativeStatus(status, error, "thread yield")38
}39
}41
private class ThreadCompletion {42
val finished: AtomicBoolean = AtomicBoolean(false)43
var failure: Throwable? = None44
fun run(block: () -> Unit): Int {45
try {46
block()47
}48
catch (error: Throwable) {49
failure = Some(error)50
}51
finally {52
finished.store(true, MemoryOrder.Release)53
}54
return 055
}56
}58
/**59
* An owned native thread running a managed non-suspending function. Join or dispose to observe60
* completion and any captured exception.61
*/62
public class Thread private constructor(private val completion: ThreadCompletion): Disposable {63
private var native: Ptr<Unit>? = None64
/**65
* Whether the function has completed, including exceptional completion.66
*/67
public val isFinished: Boolean get() = completion.finished.load(MemoryOrder.Acquire)69
/**70
* Starts managed work on a new native thread.71
*/72
public companion object {73
/**74
* Starts the function on a GC-attached native thread and returns an owned join handle.75
*/76
public fun start(block: () -> Unit): Thread {77
val completion = ThreadCompletion()78
val thread = Thread(completion)79
@Unsafe80
{81
val callback =82
foreignCallback<(Ptr<Unit>) -> Int>(83
{ completion.run(block) },84
0,85
ForeignCallbackMode.OneShot86
)87
val(pointer, error) = nativeThreadStart(callback.function, callback.context)88
if (pointer == None) {89
releaseForeignCallback(callback)90
failNative("thread start", error)91
}92
thread.native = pointer93
}94
return thread95
}96
}98
/**99
* Waits for native termination and rethrows a captured function exception. Subsequent joins100
* retain the same completion outcome.101
*/102
public fun join() {103
when (native) {104
case Some(val pointer) -> @Unsafe105
{106
nativeThreadJoin(pointer).check("thread join")107
native = None108
}109
case None -> {}110
}111
when (completion.failure) {112
case Some(val error) -> throw error113
case None -> {}114
}115
}117
/**118
* Joins an outstanding thread and releases its native join handle.119
*/120
public override fun dispose() {121
if (native != None)122
join()123
}125
release {126
when (native) {127
case Some(val pointer) -> @Unsafe128
{129
nativeThreadAbandon(pointer)130
}131
case None -> {}132
}133
}134
}136
@Extern(name = "scoop_platform_thread_start", captureErrno = true)137
private fun nativeThreadStart(function: FunPtr<(Ptr<Unit>) -> Int>, context: Ptr<Unit>): (138
Ptr<Unit>?,139
Int140
)142
@Extern(name = "scoop_platform_thread_join")143
private fun nativeThreadJoin(thread: Ptr<Unit>): NativeSyncResult145
@Extern(name = "scoop_platform_thread_abandon")146
private fun nativeThreadAbandon(thread: Ptr<Unit>)148
@GCLeaf149
@Extern(name = "scoop_platform_current_thread_id")150
private fun nativeCurrentThreadId(): ULong152
@Extern(name = "scoop_platform_thread_yield", captureErrno = true)153
private fun nativeYield(): (Int, Int)