diff options
| author | Zuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian> | 2026-02-01 14:01:51 +0000 |
|---|---|---|
| committer | Zuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian> | 2026-02-01 14:01:51 +0000 |
| commit | fbfce63744882d48ea2fc514ab1594000254db80 (patch) | |
| tree | cc7949dab2149d829d3c04e3d542553ed883593f /std/thread.zc | |
| parent | eafd8c67012ea253436b79f703dc0702046703f8 (diff) | |
Related to #138
Diffstat (limited to 'std/thread.zc')
| -rw-r--r-- | std/thread.zc | 35 |
1 files changed, 19 insertions, 16 deletions
diff --git a/std/thread.zc b/std/thread.zc index 16f3ca1..78d2547 100644 --- a/std/thread.zc +++ b/std/thread.zc @@ -35,11 +35,11 @@ raw { if (ret == 0) { *out_handle = (size_t)pt; } - return ret; + return (int)ret; } static int _z_thread_join(void *handle) { - return pthread_join((pthread_t)handle, NULL); + return (int)pthread_join((pthread_t)handle, NULL); } static void _z_mutex_init(void *ptr) { @@ -63,13 +63,13 @@ raw { } } -extern fn _z_thread_spawn(ctx: void*, out: usize*) -> int; -extern fn _z_thread_join(handle: void*) -> int; +extern fn _z_thread_spawn(ctx: void*, out: usize*) -> c_int; +extern fn _z_thread_join(handle: void*) -> c_int; extern fn _z_mutex_init(ptr: void*); extern fn _z_mutex_lock(ptr: void*); extern fn _z_mutex_unlock(ptr: void*); extern fn _z_mutex_destroy(ptr: void*); -extern fn _z_usleep(micros: int); +extern fn _z_usleep(micros: c_int); @@ -79,26 +79,28 @@ struct Thread { impl Thread { fn spawn(func: fn()) -> Result<Thread> { - let t: usize = 0; + let out_handle: usize = 0; - let ctx_copy = malloc(16); // z_closure_T is 16 bytes - if (ctx_copy == NULL) return Result<Thread>::Err("OOM"); + let ctx = malloc(16); // z_closure_T is 16 bytes + if (ctx == NULL) return Result<Thread>::Err("OOM"); - memcpy(ctx_copy, &func, 16); + memcpy(ctx, &func, 16); - let ret = _z_thread_spawn(ctx_copy, &t); - - if (ret != 0) { - free(ctx_copy); + let ret = _z_thread_spawn(ctx, &out_handle); + let zero: c_int = 0; + if (ret != zero) { + // Failed to spawn + free(ctx); return Result<Thread>::Err("Failed to create thread"); } - return Result<Thread>::Ok(Thread { handle: (void*)t }); + return Result<Thread>::Ok(Thread { handle: (void*)out_handle }); } fn join(self) -> Result<bool> { let ret = _z_thread_join(self.handle); - if (ret != 0) return Result<bool>::Err("Join failed"); + let zero: c_int = 0; + if (ret != zero) return Result<bool>::Err("Join failed"); return Result<bool>::Ok(true); } } @@ -138,5 +140,6 @@ impl Drop for Mutex { } fn sleep_ms(ms: int) { - _z_usleep(ms * 1000); + let micros: c_int = (c_int)(ms * 1000); + _z_usleep(micros); } |
