Scoop API

scoop:scoop.platform:0.1.0

src/concurrent/thread.scoop

Selected production source · src/concurrent/thread.scoop

1package scoop.platform.concurrent
3import scoop.platform.io.checkNativeStatus
4import scoop.platform.io.failNative
6/**
7 * A native thread identity suitable for equality and hashing within this process.
8 */
9public 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.value
14 /**
15 * Hashes the native thread identity.
16 */
17 public override fun hash(): Long = value.hash()
20/**
21 * Returns the identity of the calling native thread.
22 */
23public fun currentThreadId(): ThreadId {
24 @Unsafe
25 {
26 return ThreadId(nativeCurrentThreadId())
27 }
30/**
31 * Offers the current thread's execution opportunity to the operating-system scheduler.
32 */
33public fun yield() {
34 @Unsafe
35 {
36 val(status, error) = nativeYield()
37 checkNativeStatus(status, error, "thread yield")
38 }
41private class ThreadCompletion {
42 val finished: AtomicBoolean = AtomicBoolean(false)
43 var failure: Throwable? = None
44 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 0
55 }
58/**
59 * An owned native thread running a managed non-suspending function. Join or dispose to observe
60 * completion and any captured exception.
61 */
62public class Thread private constructor(private val completion: ThreadCompletion): Disposable {
63 private var native: Ptr<Unit>? = None
64 /**
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 @Unsafe
80 {
81 val callback =
82 foreignCallback<(Ptr<Unit>) -> Int>(
83 { completion.run(block) },
84 0,
85 ForeignCallbackMode.OneShot
86 )
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 = pointer
93 }
94 return thread
95 }
96 }
98 /**
99 * Waits for native termination and rethrows a captured function exception. Subsequent joins
100 * retain the same completion outcome.
101 */
102 public fun join() {
103 when (native) {
104 case Some(val pointer) -> @Unsafe
105 {
106 nativeThreadJoin(pointer).check("thread join")
107 native = None
108 }
109 case None -> {}
110 }
111 when (completion.failure) {
112 case Some(val error) -> throw error
113 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) -> @Unsafe
128 {
129 nativeThreadAbandon(pointer)
130 }
131 case None -> {}
132 }
133 }
136@Extern(name = "scoop_platform_thread_start", captureErrno = true)
137private fun nativeThreadStart(function: FunPtr<(Ptr<Unit>) -> Int>, context: Ptr<Unit>): (
138 Ptr<Unit>?,
139 Int
142@Extern(name = "scoop_platform_thread_join")
143private fun nativeThreadJoin(thread: Ptr<Unit>): NativeSyncResult
145@Extern(name = "scoop_platform_thread_abandon")
146private fun nativeThreadAbandon(thread: Ptr<Unit>)
148@GCLeaf
149@Extern(name = "scoop_platform_current_thread_id")
150private fun nativeCurrentThreadId(): ULong
152@Extern(name = "scoop_platform_thread_yield", captureErrno = true)
153private fun nativeYield(): (Int, Int)