diff --git a/C/init.c b/C/init.c index c385c0341..2d878dda4 100755 --- a/C/init.c +++ b/C/init.c @@ -1155,6 +1155,14 @@ Yap_InitThread(int new_id) if (!(new_s = (struct worker_local *)calloc(sizeof(struct worker_local), 1))) return FALSE; Yap_local[new_id] = new_s; + if (!((REGSTORE *)pthread_getspecific(Yap_yaamregs_key))) { + REGSTORE *rs = (REGSTORE *)calloc(sizeof(REGSTORE),1); + pthread_setspecific(Yap_yaamregs_key, (const void *)rs); + REMOTE_ThreadHandle(new_id).default_yaam_regs = rs; + REMOTE_ThreadHandle(new_id).current_yaam_regs = REMOTE_ThreadHandle(new_id).default_yaam_regs; + rs->worker_id_ = new_id; + rs->worker_local_ = REMOTE(new_id); + } } InitWorker(new_id); return TRUE; diff --git a/C/threads.c b/C/threads.c index 29c866060..df5af91cb 100755 --- a/C/threads.c +++ b/C/threads.c @@ -94,15 +94,24 @@ store_specs(int new_worker_id, UInt ssize, UInt tsize, UInt sysize, Term *tpgoal REMOTE_ThreadHandle(new_worker_id).ssize = ssize; REMOTE_ThreadHandle(new_worker_id).tsize = tsize; REMOTE_ThreadHandle(new_worker_id).sysize = sysize; - REMOTE_c_input_stream(new_worker_id) = LOCAL_c_input_stream; - REMOTE_c_output_stream(new_worker_id) = LOCAL_c_output_stream; - REMOTE_c_error_stream(new_worker_id) = LOCAL_c_error_stream; + + if ((REGSTORE *)pthread_getspecific(Yap_yaamregs_key)) { + REMOTE_c_input_stream(new_worker_id) = LOCAL_c_input_stream; + REMOTE_c_output_stream(new_worker_id) = LOCAL_c_output_stream; + REMOTE_c_error_stream(new_worker_id) = LOCAL_c_error_stream; + } else { + // thread is created by a thread that has never run Prolog + REMOTE_c_input_stream(new_worker_id) = REMOTE_c_input_stream(0); + REMOTE_c_output_stream(new_worker_id) = REMOTE_c_output_stream(0); + REMOTE_c_error_stream(new_worker_id) = REMOTE_c_error_stream(0); + } pm = (ssize + tsize)*1024; if (!(REMOTE_ThreadHandle(new_worker_id).stack_address = malloc(pm))) { return FALSE; } REMOTE_ThreadHandle(new_worker_id).tgoal = - Yap_StoreTermInDB(Deref(*tpgoal),7); + Yap_StoreTermInDB(Deref(*tpgoal), 7); + REMOTE_ThreadHandle(new_worker_id).cmod = CurrentModule; tdetach = Deref(*tpdetach);