fix to thread support.

This commit is contained in:
Vítor Santos Costa
2011-03-11 19:49:32 +00:00
parent 93d2ac7e59
commit e05b84ce4e
40 changed files with 222 additions and 498 deletions

View File

@@ -74,6 +74,8 @@ STATIC_PROTO(void InitFlags, (void));
STATIC_PROTO(void InitCodes, (void));
STATIC_PROTO(void InitVersion, (void));
static void InitWorker(int wid);
STD_PROTO(void exit, (int));
@@ -1180,6 +1182,19 @@ InitThreadHandle(int wid)
FOREIGN_ThreadHandle(wid).tdetach = (CELL)0;
FOREIGN_ThreadHandle(wid).cmod = (CELL)0;
}
int
Yap_InitThread(int new_id)
{
struct worker_local *new_s;
if (new_id) {
if (!(new_s = (struct worker_local *)calloc(sizeof(struct worker_local), 1)))
return FALSE;
Yap_WLocal[new_id] = new_s;
}
InitWorker(new_id);
return TRUE;
}
#endif
static void
@@ -1232,7 +1247,7 @@ struct worker_shared Yap_Global;
#if defined(YAPOR) && !defined(THREADS)
struct worker_local *Yap_WLocal;
#elif defined(YAPOR) || defined(THREADS)
struct worker_local Yap_WLocal[MAX_AGENTS];
struct worker_local *Yap_WLocal[MAX_AGENTS];
#else
struct worker_local Yap_WLocal;
#endif
@@ -1242,8 +1257,15 @@ InitCodes(void)
{
CACHE_REGS
int wid;
for (wid = 1; wid < MAX_INITS; wid++) {
#if THREADS
Yap_WLocal[wid] = NULL;
}
#endif
#include "ihstruct.h"
Yap_InitThread(0);
InitGlobal();
InitWorker(0);
InitFirstWorkerThreadHandle();
/* make sure no one else can use these two atoms */
CurrentModule = 0;
@@ -1284,6 +1306,8 @@ Yap_InitWorkspace(UInt Heap, UInt Stack, UInt Trail, UInt Atts, UInt max_table_s
/* initialise system stuff */
#if PUSH_REGS
#ifdef THREADS
if (!(Yap_WLocal[0] = (struct worker_local *)calloc(sizeof(struct worker_local), 1)))
return;
pthread_key_create(&Yap_yaamregs_key, NULL);
pthread_setspecific(Yap_yaamregs_key, (const void *)&Yap_standard_regs);
Yap_master_thread = pthread_self();

View File

@@ -570,17 +570,15 @@ static Int
int emacs_cares = FALSE;
#endif
Term tmod = Deref(ARG3), OCurrentModule = CurrentModule, tpos;
extern void Yap_setCurrentSourceLocation(IOSTREAM *s);
Yap_setCurrentSourceLocation(inp_stream);
if (IsVarTerm(tmod)) {
tmod = CurrentModule;
} else if (!IsAtomTerm(tmod)) {
Yap_Error(TYPE_ERROR_ATOM, tmod, "read_term/2");
return FALSE;
}
if (!(inp_stream->flags & SIO_TEXT)) {
Yap_Error(PERMISSION_ERROR_INPUT_BINARY_STREAM, StreamName(inp_stream), "read_term/2");
return FALSE;
}
Yap_Error_TYPE = YAP_NO_ERROR;
tpos = StreamPosition(inp_stream);
if (!Yap_unify(tpos,ARG5)) {

View File

@@ -53,10 +53,18 @@ allocate_new_tid(void)
int new_worker_id = 0;
LOCK(ThreadHandlesLock);
while(new_worker_id < MAX_THREADS &&
Yap_WLocal[new_worker_id] &&
(FOREIGN_ThreadHandle(new_worker_id).in_use == TRUE ||
FOREIGN_ThreadHandle(new_worker_id).zombie == TRUE) )
new_worker_id++;
if (new_worker_id < MAX_THREADS) {
if (!Yap_WLocal[new_worker_id]) {
DEBUG_TLOCK_ACCESS(new_worker_id, 0);
if (!Yap_InitThread(new_worker_id)) {
return -1;
}
pthread_mutex_lock(&(FOREIGN_ThreadHandle(new_worker_id).tlock));
FOREIGN_ThreadHandle(new_worker_id).in_use = TRUE;
} else if (new_worker_id < MAX_THREADS) {
DEBUG_TLOCK_ACCESS(new_worker_id, 0);
pthread_mutex_lock(&(FOREIGN_ThreadHandle(new_worker_id).tlock));
FOREIGN_ThreadHandle(new_worker_id).in_use = TRUE;
@@ -823,6 +831,7 @@ p_nof_threads( USES_REGS1 )
int i = 0, wid;
LOCK(ThreadHandlesLock);
for (wid = 0; wid < MAX_THREADS; wid++) {
if (!Yap_WLocal[wid]) break;
if (FOREIGN_ThreadHandle(wid).in_use)
i++;
}