57 lines
1.3 KiB
Hare
57 lines
1.3 KiB
Hare
use types::c;
|
|
|
|
export type pthread_t = [8]u8;
|
|
type pthread_attr_t = opaque;
|
|
type pthread_fn = fn(nullable *opaque) nullable *opaque;
|
|
export type thread_fn = fn() void;
|
|
|
|
@symbol("pthread_create") fn pthread_create(thread: nullable *pthread_t, attr: nullable *pthread_attr_t, start_routine: *pthread_fn, arg: nullable *opaque) int;
|
|
@symbol("pthread_join") fn pthread_join(thread: pthread_t, nullable *nullable *opaque) int;
|
|
@symbol("pthread_detach") fn pthread_detach(thread: pthread_t) int;
|
|
|
|
fn thread_spawn_wrapper(data: nullable *opaque) nullable *opaque = {
|
|
let f = data: *thread_fn;
|
|
f();
|
|
return null;
|
|
};
|
|
|
|
export type error = !void;
|
|
export type join_handle = struct {
|
|
thread: pthread_t,
|
|
};
|
|
|
|
// spawn a thread
|
|
//
|
|
// returns a handle or an error
|
|
export fn spawn(f: *thread_fn) (join_handle | error) = {
|
|
let data = f: *opaque;
|
|
|
|
let tid: pthread_t = [0...];
|
|
let ret = pthread_create(&tid, null, &thread_spawn_wrapper, f);
|
|
if (ret != 0) {
|
|
return error;
|
|
};
|
|
|
|
return join_handle {
|
|
thread = tid
|
|
};
|
|
};
|
|
|
|
// join a thread
|
|
//
|
|
// no return value or an error on join
|
|
export fn join(handle: join_handle) (void | error) = {
|
|
let ret = pthread_join(handle.thread, null);
|
|
if (ret != 0) {
|
|
return error;
|
|
};
|
|
};
|
|
|
|
// detach a thread.
|
|
export fn detach(handle: join_handle) (void | error) = {
|
|
let ret = pthread_detach(handle.thread);
|
|
if (ret != 0) {
|
|
return error;
|
|
};
|
|
};
|