Files
kraken/stdlib/future.krak

87 lines
2.0 KiB
Plaintext

import mem:*
__if_comp__ __C__ simple_passthrough(::"-pthread")
"""
#include <pthread.h>
"""
fun pthread_create(thrd : *ulong, strt_routine : fun(*void) : *void, input : *void) : int {
__if_comp__ __C__ {
simple_passthrough(thrd,strt_routine,input::) """
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
//int ret = pthread_create((pthread_t*)thrd, &attr, strt_routine.func, strt_routine.data);
int ret = pthread_create((pthread_t*)thrd, &attr, strt_routine.func, input);
pthread_attr_destroy(&attr);
return ret;
"""
}
return 0
}
fun pthread_join(thrd : *ulong) : int {
__if_comp__ __C__ { simple_passthrough(thrd::) """
pthread_t thread = *((pthread_t*)thrd);
return pthread_join(thread, NULL);
"""
}
return 0
}
fun pthread_exit() : *void {
__if_comp__ __C__ { simple_passthrough
"""
pthread_exit(NULL);
"""
}
}
fun future<T>(in : fun() : T ) : future<T> {
var out.construct(in) : future<T>
return out
}
obj func_res { var func : *void; var result : *void; }
obj future<T> {
var result : T
var status : int
var psy : fun() : T
var wrapper : fun(*void) : * void;
var thread : ulong
fun construct(in : fun() : T) : *future<T> {
status = 0
psy = in
wrapper = fun(in : *void) : *void {
var triple = (in) cast *func_res;
var func = (triple->func) cast *fun() : T;
var res : *T = (triple->result) cast *T;
(*res) = (*func)();
pthread_exit();
delete(in);
return null<void>();
}
return this
}
fun run() {
var in = new<func_res>();
in->result = (&result) cast *void;
in->func = (&psy) cast *void;
status = pthread_create(&thread,wrapper,(in) cast *void)
}
fun get_status():int {
return status
}
fun finish() : T {
pthread_join(&thread)
return result
}
}