From eebff5df6a52c786322c3a8d7d2b21e73c70a98e Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Fri, 23 Jul 2010 12:07:33 +0100 Subject: [PATCH] fixes to YAP C-thread interface. --- C/init.c | 3 +++ C/threads.c | 17 +++++++++++++++-- H/Yap.h | 5 ++++- library/yap2swi/yap2swi.c | 5 +++-- packages/jpl | 2 +- 5 files changed, 26 insertions(+), 6 deletions(-) diff --git a/C/init.c b/C/init.c index 8b6237043..2874b369b 100755 --- a/C/init.c +++ b/C/init.c @@ -88,6 +88,8 @@ struct restore_info rinfo[MAX_THREADS]; struct thread_globs Yap_thread_gl[MAX_THREADS]; +pthread_t Yap_master_thread; + #else struct restore_info rinfo; @@ -1246,6 +1248,7 @@ Yap_InitWorkspace(UInt Heap, UInt Stack, UInt Trail, UInt Atts, UInt max_table_s #ifdef THREADS pthread_key_create(&Yap_yaamregs_key, NULL); pthread_setspecific(Yap_yaamregs_key, (const void *)&Yap_standard_regs); + Yap_master_thread = pthread_self(); #else /* In this case we need to initialise the abstract registers */ Yap_regp = &Yap_standard_regs; diff --git a/C/threads.c b/C/threads.c index 917f9beaa..e1134b6a5 100755 --- a/C/threads.c +++ b/C/threads.c @@ -368,8 +368,13 @@ CELL Yap_thread_create_engine(thread_attr *ops) { thread_attr opsv; - Term t = TermNil; int new_id = allocate_new_tid(); + Term t = TermNil; + + /* + ok, this creates a problem, because we are initializing an engine from some "empty" thread. + We need first to foool the thread into believing it is the main thread + */ if (new_id == -1) { /* YAP ERROR */ return FALSE; @@ -381,13 +386,21 @@ Yap_thread_create_engine(thread_attr *ops) ops->sysize = 0; ops->egoal = t; } + if (pthread_self() != Yap_master_thread) { + pthread_setspecific(Yap_yaamregs_key, (const void *)&Yap_standard_regs); + /* we are worker_id 0 for now, lock master thread so that no one messes with us */ + pthread_mutex_lock(&(FOREIGN_ThreadHandle(0).tlock)); + } if (!init_thread_engine(new_id, ops->ssize, ops->tsize, ops->sysize, &t, &t, &(ops->egoal))) return FALSE; FOREIGN_ThreadHandle(new_id).id = new_id; FOREIGN_ThreadHandle(new_id).pthread_handle = pthread_self(); FOREIGN_ThreadHandle(new_id).ref_count = 0; setup_engine(new_id); - return TRUE; + if (pthread_self() != Yap_master_thread) { + pthread_mutex_unlock(&(FOREIGN_ThreadHandle(0).tlock)); + } + return new_id; } Int diff --git a/H/Yap.h b/H/Yap.h index 64cb0e978..4a0abc50b 100755 --- a/H/Yap.h +++ b/H/Yap.h @@ -773,7 +773,6 @@ typedef struct thread_globs extern struct thread_globs Yap_thread_gl[MAX_THREADS]; - #define Yap_LocalBase Yap_thread_gl[worker_id].local_base #define Yap_GlobalBase Yap_thread_gl[worker_id].global_base #define Yap_TrailBase Yap_thread_gl[worker_id].trail_base @@ -784,6 +783,10 @@ extern struct thread_globs Yap_thread_gl[MAX_THREADS]; #define Yap_Error_Size Yap_thread_gl[worker_id].error_size #define Yap_ErrorSay Yap_thread_gl[worker_id].error_say #define Yap_RestartEnv Yap_thread_gl[worker_id].restart_env + +/* This is the guy who actually started the system, and who has the correct registers */ +extern pthread_t Yap_master_thread; + #else extern ADDR Yap_HeapBase, Yap_LocalBase, Yap_GlobalBase, Yap_TrailBase, Yap_TrailTop; diff --git a/library/yap2swi/yap2swi.c b/library/yap2swi/yap2swi.c index 794040026..47448cc2d 100755 --- a/library/yap2swi/yap2swi.c +++ b/library/yap2swi/yap2swi.c @@ -2784,7 +2784,6 @@ X_API int PL_thread_self(void) X_API int PL_thread_attach_engine(const PL_thread_attr_t *attr) { int wid = YAP_ThreadSelf(); - fprintf(stderr,"attaching new engine %p\n", attr); if (wid < 0) { /* we do not have an engine */ @@ -2861,7 +2860,9 @@ X_API int PL_set_engine(PL_engine_t engine, PL_engine_t *old) { YAP_Int cwid = YAP_ThreadSelf(); - if (*old) *old = (PL_engine_t)cwid; + if (old) { + if (*old) *old = (PL_engine_t)cwid; + } if (engine == PL_ENGINE_CURRENT) return PL_ENGINE_SET; if (engine < 0) /* should really check if engine does not exist */ diff --git a/packages/jpl b/packages/jpl index daedb3738..7c9456b5c 160000 --- a/packages/jpl +++ b/packages/jpl @@ -1 +1 @@ -Subproject commit daedb37386cd9cdebd1473eed60d9bf240379d81 +Subproject commit 7c9456b5c9c60ab227674f7653dc226e13973484