From fcc9210aa32d671e16b392cf48546c4e2001ff8f Mon Sep 17 00:00:00 2001 From: Lam Wei Lun Date: Sun, 1 Feb 2026 14:46:46 +0800 Subject: Added detach/cancel and equality check for std/thread --- std/thread.zc | 56 +++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 49 insertions(+), 7 deletions(-) (limited to 'std') 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 { let t: usize = 0; let ctx_copy = malloc(16); // z_closure_T is 16 bytes - if (ctx_copy == NULL) return Result::Err("OOM"); + if ctx_copy == NULL { + return Result::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::Err("Failed to create thread"); } @@ -97,8 +121,26 @@ impl Thread { } fn join(self) -> Result { - let ret = _z_thread_join(self.handle); - if (ret != 0) return Result::Err("Join failed"); + let err = _z_thread_join(self.handle); + if err { + return Result::Err("Join failed"); + } + return Result::Ok(true); + } + + fn detach(self) -> Result { + let err = _z_thread_detach(self.handle); + if err { + return Result::Err("Detach failed"); + } + return Result::Ok(true); + } + + fn cancel(self) -> Result { + let err = _z_thread_cancel(self.handle); + if err { + return Result::Err("Cancel failed"); + } return Result::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; -- cgit v1.2.3 From dadabf8b9d11d099777acc261068a3ed8ca06f24 Mon Sep 17 00:00:00 2001 From: Zuhaitz Méndez Fernández de Aránguiz Date: Mon, 2 Feb 2026 00:33:12 +0000 Subject: Fix concurrency suite compilation errors involving C types --- src/ast/ast.c | 3 +++ src/parser/parser_expr.c | 3 +-- std/thread.zc | 4 ++-- 3 files changed, 6 insertions(+), 4 deletions(-) (limited to 'std') diff --git a/src/ast/ast.c b/src/ast/ast.c index 1b35500..28fe678 100644 --- a/src/ast/ast.c +++ b/src/ast/ast.c @@ -101,6 +101,9 @@ int is_integer_type(Type *t) t->kind == TYPE_ISIZE || t->kind == TYPE_BYTE || t->kind == TYPE_RUNE || t->kind == TYPE_UINT || t->kind == TYPE_I128 || t->kind == TYPE_U128 || t->kind == TYPE_BITINT || t->kind == TYPE_UBITINT || + t->kind == TYPE_C_INT || t->kind == TYPE_C_UINT || t->kind == TYPE_C_LONG || + t->kind == TYPE_C_ULONG || t->kind == TYPE_C_SHORT || t->kind == TYPE_C_USHORT || + t->kind == TYPE_C_CHAR || t->kind == TYPE_C_UCHAR || (t->kind == TYPE_STRUCT && t->name && (0 == strcmp(t->name, "int8_t") || 0 == strcmp(t->name, "uint8_t") || 0 == strcmp(t->name, "int16_t") || 0 == strcmp(t->name, "uint16_t") || diff --git a/src/parser/parser_expr.c b/src/parser/parser_expr.c index a732448..f27e2c3 100644 --- a/src/parser/parser_expr.c +++ b/src/parser/parser_expr.c @@ -370,8 +370,7 @@ static void check_format_string(ASTNode *call, Token t) if (spec == 'd' || spec == 'i' || spec == 'u' || spec == 'x' || spec == 'X' || spec == 'o') { - if (vt && vt->kind != TYPE_INT && vt->kind != TYPE_I64 && !type_is_unsigned(vt) && - vt->kind != TYPE_CHAR) + if (vt && !is_integer_type(vt)) { warn_format_string(t, arg_num, "integer", got_type); } diff --git a/std/thread.zc b/std/thread.zc index da21772..0722b60 100644 --- a/std/thread.zc +++ b/std/thread.zc @@ -108,10 +108,10 @@ impl Thread { memcpy(ctx, &func, 16); - let ret = _z_thread_spawn(ctx_copy, &t); + let ret = _z_thread_spawn(ctx, &out_handle); if ret != 0 { - free(ctx_copy); + free(ctx); return Result::Err("Failed to create thread"); } -- cgit v1.2.3