diff --git a/C/init.c b/C/init.c index b29374532..b09a41ad9 100644 --- a/C/init.c +++ b/C/init.c @@ -1139,6 +1139,7 @@ InitThreadHandles(void) Yap_heap_regs->thread_handle[0].handle = pthread_self(); pthread_mutex_init(&ThreadHandle[0].tlock, NULL); pthread_mutex_init(&ThreadHandle[0].tlock_status, NULL); + Yap_heap_regs->thread_handle[0].tdetach = MkAtomTerm(AtomFalse); } #endif diff --git a/C/threads.c b/C/threads.c index d1824b55b..2546d587e 100644 --- a/C/threads.c +++ b/C/threads.c @@ -711,7 +711,8 @@ p_thread_atexit(void) { /* '$thread_signal'(+P) */ Term t; - if (ThreadHandle[worker_id].texit->Entry == MkAtomTerm(AtomTrue)) { + if (!ThreadHandle[worker_id].texit || + ThreadHandle[worker_id].texit->Entry == MkAtomTerm(AtomTrue)) { return FALSE; } do { diff --git a/H/dhstruct.h b/H/dhstruct.h index adf7c6ec0..ed4a77a4e 100644 --- a/H/dhstruct.h +++ b/H/dhstruct.h @@ -45,10 +45,6 @@ #define WL wl #endif #endif -#ifdef THREADS -#define ThreadHandlesLock Yap_heap_regs->thread_handles_lock -#define ThreadHandle Yap_heap_regs->thread_handle -#endif #define Yap_HoleSize Yap_heap_regs->hole_size #define Yap_av Yap_heap_regs->av_ @@ -329,3 +325,8 @@ #define ForeignCodeBase Yap_heap_regs->foreign_code_base #define ForeignCodeTop Yap_heap_regs->foreign_code_top #define ForeignCodeMax Yap_heap_regs->foreign_code_max + +#ifdef THREADS +#define ThreadHandlesLock Yap_heap_regs->thread_handles_lock +#define ThreadHandle Yap_heap_regs->thread_handle +#endif diff --git a/H/hstruct.h b/H/hstruct.h index 8ddb8a4aa..2db9a6555 100644 --- a/H/hstruct.h +++ b/H/hstruct.h @@ -44,10 +44,6 @@ #ifndef WL #define WL wl #endif -#endif -#ifdef THREADS - lockvar thread_handles_lock; - struct thandle thread_handle[MAX_THREADS]; #endif UInt hole_size; @@ -329,3 +325,8 @@ ADDR foreign_code_base; ADDR foreign_code_top; ADDR foreign_code_max; + +#ifdef THREADS + lockvar thread_handles_lock; + struct thandle thread_handle[MAX_THREADS]; +#endif diff --git a/H/ihstruct.h b/H/ihstruct.h index 3dcc832e3..9a91fcea4 100644 --- a/H/ihstruct.h +++ b/H/ihstruct.h @@ -45,10 +45,6 @@ #define WL wl #endif #endif -#ifdef THREADS - INIT_LOCK(Yap_heap_regs->thread_handles_lock); - InitThreadHandles(); -#endif @@ -329,3 +325,8 @@ Yap_heap_regs->foreign_code_base = NULL; Yap_heap_regs->foreign_code_top = NULL; Yap_heap_regs->foreign_code_max = NULL; + +#ifdef THREADS + INIT_LOCK(Yap_heap_regs->thread_handles_lock); + InitThreadHandles(); +#endif diff --git a/H/rhstruct.h b/H/rhstruct.h index 7c2d34ce1..391b75a9a 100644 --- a/H/rhstruct.h +++ b/H/rhstruct.h @@ -45,10 +45,6 @@ #define WL wl #endif #endif -#ifdef THREADS - REINIT_LOCK(Yap_heap_regs->thread_handles_lock); - -#endif @@ -329,3 +325,8 @@ + +#ifdef THREADS + REINIT_LOCK(Yap_heap_regs->thread_handles_lock); + +#endif diff --git a/misc/HEAPFIELDS b/misc/HEAPFIELDS index e0e0bf738..8b5d82458 100644 --- a/misc/HEAPFIELDS +++ b/misc/HEAPFIELDS @@ -46,10 +46,6 @@ worker_local wl WorkerLocak void void #define WL wl #endif #endif -#ifdef THREADS -lockvar thread_handles_lock ThreadHandlesLock MkLock -struct thandle thread_handle[MAX_THREADS] ThreadHandle InitThreadHandles() void -#endif /* memory management */ UInt hole_size Yap_HoleSize void void @@ -377,3 +373,9 @@ void *foreign_code_loaded ForeignCodeLoaded =NULL RestoreForeignCode() ADDR foreign_code_base ForeignCodeBase =NULL void ADDR foreign_code_top ForeignCodeTop =NULL void ADDR foreign_code_max ForeignCodeMax =NULL void + +/* Threads Array */ +#ifdef THREADS +lockvar thread_handles_lock ThreadHandlesLock MkLock +struct thandle thread_handle[MAX_THREADS] ThreadHandle InitThreadHandles() void +#endif diff --git a/pl/threads.yap b/pl/threads.yap index a44d039fc..f217df5c1 100644 --- a/pl/threads.yap +++ b/pl/threads.yap @@ -41,8 +41,10 @@ volatile(P) :- :- initialization('$init_thread0'). '$init_thread0' :- - '$no_threads', !, - recorda('$thread_alias', [0|main], _). + recorda('$thread_alias', [0|main], _), + fail. +'$init_thread0' :- + '$no_threads', !. '$init_thread0' :- recorda('$thread_defaults', [0, 0, 0, false, true], _), '$create_thread_mq'(0), @@ -863,7 +865,7 @@ thread_property(Id, Prop) :- ; Status = running ). '$thread_property'(Id, detached(Detached)) :- - '$thread_detached'(Id,Detached). + ( '$thread_detached'(Id,Detached) -> true ; Detached = false ). '$thread_property'(Id, at_exit(M:G)) :- '$thread_run_at_exit'(G,M). '$thread_property'(Id, InfoSize) :-