Fix bug in future causing a segfault. Fixed the test case.

This commit is contained in:
Marcus Godwin
2016-06-14 23:30:12 -04:00
parent e3c1cbf27e
commit 61cfe76b6d
3 changed files with 25 additions and 13 deletions

View File

@@ -1,15 +1,18 @@
import mem:*
__if_comp__ __C__ simple_passthrough(::"-pthread") """ __if_comp__ __C__ simple_passthrough(::"-pthread")
"""
#include <pthread.h> #include <pthread.h>
""" """
fun pthread_create(thrd : *ulong, strt_routine : fun() : void) : int { fun pthread_create(thrd : *ulong, strt_routine : fun(*void) : *void, input : *void) : int {
__if_comp__ __C__ { __if_comp__ __C__ {
simple_passthrough(thrd,strt_routine::) """ simple_passthrough(thrd,strt_routine,input::) """
pthread_attr_t attr; pthread_attr_t attr;
pthread_attr_init(&attr); pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); 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, strt_routine.data);
int ret = pthread_create((pthread_t*)thrd, &attr, strt_routine.func, input);
pthread_attr_destroy(&attr); pthread_attr_destroy(&attr);
return ret; return ret;
""" """
@@ -27,7 +30,8 @@ fun pthread_join(thrd : *ulong) : int {
} }
fun pthread_exit() : *void { fun pthread_exit() : *void {
__if_comp__ __C__ { simple_passthrough """ __if_comp__ __C__ { simple_passthrough
"""
pthread_exit(NULL); pthread_exit(NULL);
""" """
} }
@@ -40,25 +44,35 @@ fun future<T>(in : fun() : T ) : future<T> {
return out return out
} }
obj func_res { var func : *void; var result : *void; }
obj future<T> { obj future<T> {
var result : T var result : T
var status : int var status : int
var psy : fun() : T var psy : fun() : T
var wrapper : fun() : void var wrapper : fun(*void) : * void;
var thread : ulong var thread : ulong
fun construct(in : fun() : T) : *future<T> { fun construct(in : fun() : T) : *future<T> {
status = 0 status = 0
psy = in psy = in
wrapper = fun() : void{ wrapper = fun(in : *void) : *void {
result = psy() var triple = (in) cast *func_res;
pthread_exit() 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 return this
} }
fun run() { fun run() {
status = pthread_create(&thread,wrapper) 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 { fun get_status():int {

View File

@@ -1,4 +1,3 @@
Thread 0 Started! Thread 0 Started!
0
FINISHED THREAD 0
Threads have finished Threads have finished
All Done!

View File

@@ -22,7 +22,6 @@ fun main() : int {
println("Threads have finished") println("Threads have finished")
pthread_exit()
println("All Done!") println("All Done!")
return 0 return 0