2016-06-14 23:30:12 -04:00
|
|
|
import mem:*
|
2015-07-05 01:27:43 -04:00
|
|
|
|
2016-06-14 23:30:12 -04:00
|
|
|
__if_comp__ __C__ simple_passthrough(::"-pthread")
|
|
|
|
|
"""
|
2015-07-05 01:27:43 -04:00
|
|
|
#include <pthread.h>
|
|
|
|
|
"""
|
|
|
|
|
|
2016-06-14 23:30:12 -04:00
|
|
|
fun pthread_create(thrd : *ulong, strt_routine : fun(*void) : *void, input : *void) : int {
|
2015-07-05 01:27:43 -04:00
|
|
|
__if_comp__ __C__ {
|
2016-06-14 23:30:12 -04:00
|
|
|
simple_passthrough(thrd,strt_routine,input::) """
|
2015-07-05 01:27:43 -04:00
|
|
|
pthread_attr_t attr;
|
|
|
|
|
pthread_attr_init(&attr);
|
|
|
|
|
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
|
2016-06-14 23:30:12 -04:00
|
|
|
//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);
|
2015-07-05 01:27:43 -04:00
|
|
|
pthread_attr_destroy(&attr);
|
|
|
|
|
return ret;
|
|
|
|
|
"""
|
|
|
|
|
}
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-30 15:38:28 -04:00
|
|
|
fun pthread_join(thrd : *ulong) : int {
|
2015-07-05 01:27:43 -04:00
|
|
|
__if_comp__ __C__ { simple_passthrough(thrd::) """
|
|
|
|
|
pthread_t thread = *((pthread_t*)thrd);
|
|
|
|
|
return pthread_join(thread, NULL);
|
|
|
|
|
"""
|
|
|
|
|
}
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun pthread_exit() : *void {
|
2016-06-14 23:30:12 -04:00
|
|
|
__if_comp__ __C__ { simple_passthrough
|
|
|
|
|
"""
|
2015-07-05 01:27:43 -04:00
|
|
|
pthread_exit(NULL);
|
|
|
|
|
"""
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun future<T>(in : fun() : T ) : future<T> {
|
|
|
|
|
var out.construct(in) : future<T>
|
|
|
|
|
return out
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-14 23:30:12 -04:00
|
|
|
obj func_res { var func : *void; var result : *void; }
|
|
|
|
|
|
2015-07-05 01:27:43 -04:00
|
|
|
obj future<T> {
|
|
|
|
|
var result : T
|
|
|
|
|
var status : int
|
|
|
|
|
var psy : fun() : T
|
2016-06-14 23:30:12 -04:00
|
|
|
var wrapper : fun(*void) : * void;
|
2016-04-30 15:38:28 -04:00
|
|
|
var thread : ulong
|
2015-07-05 01:27:43 -04:00
|
|
|
|
|
|
|
|
fun construct(in : fun() : T) : *future<T> {
|
|
|
|
|
status = 0
|
|
|
|
|
psy = in
|
2016-06-14 23:30:12 -04:00
|
|
|
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>();
|
2015-07-05 01:27:43 -04:00
|
|
|
}
|
|
|
|
|
return this
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun run() {
|
2016-06-14 23:30:12 -04:00
|
|
|
var in = new<func_res>();
|
|
|
|
|
in->result = (&result) cast *void;
|
|
|
|
|
in->func = (&psy) cast *void;
|
|
|
|
|
status = pthread_create(&thread,wrapper,(in) cast *void)
|
2015-07-05 01:27:43 -04:00
|
|
|
}
|
|
|
|
|
|
2015-07-15 00:53:53 -04:00
|
|
|
fun get_status():int {
|
2015-07-05 01:27:43 -04:00
|
|
|
return status
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun finish() : T {
|
|
|
|
|
pthread_join(&thread)
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
}
|