1
Fork 0
hare-playground/backend/cmd/threads/main.ha

46 lines
1.1 KiB
Hare

use types::c;
use fmt;
export type pthread_t = u64;
export type pthread_attr_t = opaque;
export type pthread_fn = fn(nullable *opaque) nullable *opaque;
export @symbol("pthread_create") fn pthread_create(thread: nullable *pthread_t, attr: nullable *pthread_attr_t, start_routine: *pthread_fn, arg: nullable *opaque) int;
export @symbol("pthread_join") fn pthread_join(thread: pthread_t, nullable *nullable *opaque) int;
fn thread_start(arg: nullable *opaque) nullable *opaque = {
fmt::println("hello from another thread")!;
return null;
};
export fn main() void = {
fmt::println("starting a thread")!;
let tid: pthread_t = 0;
let ret = pthread_create(&tid, null, &thread_start, null);
fmt::printfln("ret: {}", ret)!;
fmt::println("joining")!;
ret = pthread_join(tid, null);
fmt::printfln("ret: {}", ret)!;
};
// spawn a thread
//
// returns a handle or an error
fn spawn(f: *pthread_fn) (join_handle | error) = {
void;
};
// join a thread
//
// no return value or an error on join
fn join(handle: join_handle) (void | error) = {
void;
};
// detach a thread.
fn detach(join_handle) (void | error) = {
void;
};