summaryrefslogtreecommitdiff
path: root/std
diff options
context:
space:
mode:
authorLam Wei Lun <weilun.lam@gmail.com>2026-02-01 14:46:46 +0800
committerLam Wei Lun <weilun.lam@gmail.com>2026-02-01 14:46:46 +0800
commitfcc9210aa32d671e16b392cf48546c4e2001ff8f (patch)
treef8df9cdfe3d2f422a31ab62e6685c30814067760 /std
parentaafd7e8739b14dd89b2e81148f2b07710f3c2c42 (diff)
Added detach/cancel and equality check for std/thread
Diffstat (limited to 'std')
-rw-r--r--std/thread.zc56
1 files changed, 49 insertions, 7 deletions
diff --git a/std/thread.zc b/std/thread.zc
index 16f3ca1..0ab02e4 100644
--- a/std/thread.zc
+++ b/std/thread.zc
@@ -25,9 +25,13 @@ raw {
z_closure_T *closure = (z_closure_T*)c;
void (*f)(void*) = (void(*)(void*))closure->func;
f(closure->ctx);
- free(c);
+ free(c);
return NULL;
}
+
+ static int _z_thread_equal(void* handle1, void* handle2) {
+ return pthread_equal((pthread_t)handle1, (pthread_t)handle2);
+ }
static int _z_thread_spawn(void *ctx_copy, size_t *out_handle) {
pthread_t pt;
@@ -41,6 +45,14 @@ raw {
static int _z_thread_join(void *handle) {
return pthread_join((pthread_t)handle, NULL);
}
+
+ static int _z_thread_detach(void* handle) {
+ return pthread_detach((pthread_t)handle);
+ }
+
+ static int _z_thread_cancel(void* handle) {
+ return pthread_cancel((pthread_t)handle);
+ }
static void _z_mutex_init(void *ptr) {
pthread_mutex_init((pthread_mutex_t*)ptr, NULL);
@@ -63,8 +75,11 @@ raw {
}
}
+extern fn _z_thread_equal(handle1: void*, handle2: void*) -> int;
extern fn _z_thread_spawn(ctx: void*, out: usize*) -> int;
extern fn _z_thread_join(handle: void*) -> int;
+extern fn _z_thread_detach(handle: void*) -> int;
+extern fn _z_thread_cancel(handle: void*) -> int;
extern fn _z_mutex_init(ptr: void*);
extern fn _z_mutex_lock(ptr: void*);
extern fn _z_mutex_unlock(ptr: void*);
@@ -74,21 +89,30 @@ extern fn _z_usleep(micros: int);
struct Thread {
- handle: void*;
+ handle: void*;
}
impl Thread {
+ fn eq(self, other: const Thread) -> bool {
+ return _z_thread_equal(self.handle, other.handle);
+ }
+ fn neq(self, other: const Thread) -> bool {
+ return !(self == other);
+ }
+
fn spawn(func: fn()) -> Result<Thread> {
let t: usize = 0;
let ctx_copy = malloc(16); // z_closure_T is 16 bytes
- if (ctx_copy == NULL) return Result<Thread>::Err("OOM");
+ if ctx_copy == NULL {
+ return Result<Thread>::Err("OOM");
+ }
memcpy(ctx_copy, &func, 16);
let ret = _z_thread_spawn(ctx_copy, &t);
- if (ret != 0) {
+ if ret != 0 {
free(ctx_copy);
return Result<Thread>::Err("Failed to create thread");
}
@@ -97,8 +121,26 @@ impl Thread {
}
fn join(self) -> Result<bool> {
- let ret = _z_thread_join(self.handle);
- if (ret != 0) return Result<bool>::Err("Join failed");
+ let err = _z_thread_join(self.handle);
+ if err {
+ return Result<bool>::Err("Join failed");
+ }
+ return Result<bool>::Ok(true);
+ }
+
+ fn detach(self) -> Result<bool> {
+ let err = _z_thread_detach(self.handle);
+ if err {
+ return Result<bool>::Err("Detach failed");
+ }
+ return Result<bool>::Ok(true);
+ }
+
+ fn cancel(self) -> Result<bool> {
+ let err = _z_thread_cancel(self.handle);
+ if err {
+ return Result<bool>::Err("Cancel failed");
+ }
return Result<bool>::Ok(true);
}
}
@@ -123,7 +165,7 @@ impl Mutex {
}
fn free(self) {
- if (self.handle) {
+ if self.handle {
_z_mutex_destroy(self.handle);
free(self.handle);
self.handle = NULL;