summaryrefslogtreecommitdiff
path: root/std/thread.zc
diff options
context:
space:
mode:
Diffstat (limited to 'std/thread.zc')
-rw-r--r--std/thread.zc84
1 files changed, 65 insertions, 19 deletions
diff --git a/std/thread.zc b/std/thread.zc
index 98f080e..0722b60 100644
--- a/std/thread.zc
+++ b/std/thread.zc
@@ -7,6 +7,11 @@ import "./core.zc"
import "./result.zc"
import "./mem.zc"
+// Essential raw block: required for pthread operations and closure trampolining
+// This block cannot be eliminated because:
+// 1. z_closure_T is an internal compiler type for Zen-C closures
+// 2. pthread_t, pthread_mutex_t are opaque types that can't be extern'd with void*
+// 3. The trampoline function needs to cast and execute Zen-C closures
raw {
typedef void (*ZenThreadFunc)(void*);
@@ -20,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;
@@ -30,11 +39,19 @@ 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 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) {
@@ -58,42 +75,70 @@ raw {
}
}
-extern fn _z_thread_spawn(ctx: void*, out: usize*) -> int;
-extern fn _z_thread_join(handle: void*) -> int;
+extern fn _z_thread_equal(handle1: void*, handle2: void*) -> c_int;
+extern fn _z_thread_spawn(ctx: void*, out: usize*) -> c_int;
+extern fn _z_thread_join(handle: void*) -> c_int;
+extern fn _z_thread_detach(handle: void*) -> c_int;
+extern fn _z_thread_cancel(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);
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 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);
+ let ret = _z_thread_spawn(ctx, &out_handle);
- if (ret != 0) {
- free(ctx_copy);
+ if ret != 0 {
+ 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 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);
}
}
@@ -118,7 +163,7 @@ impl Mutex {
}
fn free(self) {
- if (self.handle) {
+ if self.handle {
_z_mutex_destroy(self.handle);
free(self.handle);
self.handle = NULL;
@@ -133,5 +178,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);
}