thread fixes

git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@2251 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
This commit is contained in:
vsc 2008-05-28 17:18:35 +00:00
parent f6160fd5d6
commit ade7b34d49
4 changed files with 49 additions and 7 deletions

View File

@ -11,8 +11,12 @@
* File: cdmgr.c *
* comments: Code manager *
* *
* Last rev: $Date: 2008-04-28 23:02:32 $,$Author: vsc $ *
* Last rev: $Date: 2008-05-28 17:18:35 $,$Author: vsc $ *
* $Log: not supported by cvs2svn $
* Revision 1.228 2008/04/28 23:02:32 vsc
* fix bug in current_predicate/2
* fix bug in c_interface.
*
* Revision 1.227 2008/04/11 16:30:27 ricroc
* *** empty log message ***
*
@ -1875,9 +1879,13 @@ not_was_reconsulted(PredEntry *p, Term t, int mode)
register consult_obj *fp;
Prop p0 = AbsProp((PropEntry *)p);
for (fp = ConsultSp; fp < ConsultBase; ++fp)
if (fp->p == p0)
break;
if (p->cs.p_code.NOfClauses) {
for (fp = ConsultSp; fp < ConsultBase; ++fp)
if (fp->p == p0)
break;
} else {
fp = ConsultBase;
}
if (fp != ConsultBase)
return (FALSE);
if (mode) {

View File

@ -3871,7 +3871,7 @@ static Int
if (tokstart != NULL && tokstart->Tok != Ord (eot_tok)) {
/* we got the end of file from an abort */
if (Yap_ErrorMessage &&
strcmp(Yap_ErrorMessage,"Abort")) {
!strcmp(Yap_ErrorMessage,"Abort")) {
Yap_clean_tokenizer(tokstart, Yap_VarTable, Yap_AnonVarTable);
return FALSE;
}

View File

@ -38,6 +38,16 @@ static char SccsId[] = "%W% %G%";
*
*/
#if DEBUG
static void DEBUG_TLOCK_ACCESS( int pos, int wid) {
ThreadHandle[wid].been_here2 = ThreadHandle[wid].been_here1;
ThreadHandle[wid].been_here1 = pos;
}
#else
#define DEBUG_TLOCK_ACCESS(WID, POS)
#endif
static int
allocate_new_tid(void)
{
@ -49,6 +59,7 @@ allocate_new_tid(void)
new_worker_id++;
if (new_worker_id < MAX_THREADS) {
pthread_mutex_lock(&(ThreadHandle[new_worker_id].tlock));
DEBUG_TLOCK_ACCESS(new_worker_id, 0);
ThreadHandle[new_worker_id].in_use = TRUE;
} else {
new_worker_id = -1;
@ -108,6 +119,7 @@ kill_thread_engine (int wid)
free(ThreadHandle[wid].start_of_timesp);
free(ThreadHandle[wid].last_timep);
ThreadHandle[wid].zombie = FALSE;
DEBUG_TLOCK_ACCESS(1, wid);
pthread_mutex_unlock(&(ThreadHandle[wid].tlock));
}
@ -144,6 +156,7 @@ setup_engine(int myworker_id)
Yap_ReleasePreAllocCodeSpace(Yap_PreAllocCodeSpace());
/* I exist */
NOfThreadsCreated++;
DEBUG_TLOCK_ACCESS(2, myworker_id);
pthread_mutex_unlock(&(ThreadHandle[myworker_id].tlock));
}
@ -234,6 +247,7 @@ p_create_thread(void)
ThreadHandle[new_worker_id].ref_count = 1;
if ((ThreadHandle[new_worker_id].ret = pthread_create(&ThreadHandle[new_worker_id].handle, NULL, thread_run, (void *)(&(ThreadHandle[new_worker_id].id)))) == 0) {
/* wait until the client is initialised */
DEBUG_TLOCK_ACCESS(3, new_worker_id);
pthread_mutex_unlock(&(ThreadHandle[new_worker_id].tlock));
return TRUE;
}
@ -291,12 +305,15 @@ p_thread_zombie_self(void)
if (pthread_getspecific(Yap_yaamregs_key) == NULL)
return Yap_unify(MkIntegerTerm(-1), ARG1);
pthread_mutex_lock(&(ThreadHandle[worker_id].tlock));
DEBUG_TLOCK_ACCESS(4, worker_id);
if (Yap_heap_regs->wl[worker_id].active_signals &= YAP_ITI_SIGNAL) {
DEBUG_TLOCK_ACCESS(5, worker_id);
pthread_mutex_unlock(&(ThreadHandle[worker_id].tlock));
return FALSE;
}
Yap_heap_regs->thread_handle[worker_id].in_use = FALSE;
Yap_heap_regs->thread_handle[worker_id].zombie = TRUE;
DEBUG_TLOCK_ACCESS(6, worker_id);
pthread_mutex_unlock(&(ThreadHandle[worker_id].tlock));
return Yap_unify(MkIntegerTerm(worker_id), ARG1);
}
@ -330,14 +347,17 @@ Int
Yap_thread_attach_engine(int wid)
{
pthread_mutex_lock(&(ThreadHandle[wid].tlock));
DEBUG_TLOCK_ACCESS(7, wid);
if (ThreadHandle[wid].ref_count &&
ThreadHandle[wid].handle != pthread_self()) {
DEBUG_TLOCK_ACCESS(8, wid);
pthread_mutex_unlock(&(ThreadHandle[wid].tlock));
return FALSE;
}
ThreadHandle[wid].handle = pthread_self();
ThreadHandle[wid].ref_count++;
worker_id = wid;
DEBUG_TLOCK_ACCESS(9, wid);
pthread_mutex_unlock(&(ThreadHandle[wid].tlock));
return TRUE;
}
@ -346,9 +366,11 @@ Int
Yap_thread_detach_engine(int wid)
{
pthread_mutex_lock(&(ThreadHandle[wid].tlock));
DEBUG_TLOCK_ACCESS(10, wid);
if (ThreadHandle[wid].handle == pthread_self())
ThreadHandle[wid].handle = 0;
ThreadHandle[wid].ref_count--;
DEBUG_TLOCK_ACCESS(11, wid);
pthread_mutex_unlock(&(ThreadHandle[wid].tlock));
return TRUE;
}
@ -360,6 +382,7 @@ Yap_thread_destroy_engine(int wid)
kill_thread_engine(wid);
return TRUE;
} else {
DEBUG_TLOCK_ACCESS(12, wid);
pthread_mutex_unlock(&(ThreadHandle[wid].tlock));
return FALSE;
}
@ -382,6 +405,7 @@ p_thread_join(void)
return FALSE;
}
pthread_mutex_lock(&(ThreadHandle[tid].tlock));
DEBUG_TLOCK_ACCESS(13, tid);
UNLOCK(ThreadHandlesLock);
/* make sure this lock is accessible */
if (pthread_join(ThreadHandle[tid].handle, NULL) < 0) {
@ -406,13 +430,17 @@ p_thread_detach(void)
{
Int tid = IntegerOfTerm(Deref(ARG1));
pthread_mutex_lock(&(ThreadHandle[tid].tlock));
DEBUG_TLOCK_ACCESS(14, tid);
if (pthread_detach(ThreadHandle[tid].handle) < 0) {
/* ERROR */
DEBUG_TLOCK_ACCESS(15, tid);
pthread_mutex_unlock(&(ThreadHandle[tid].tlock));
return FALSE;
}
ThreadHandle[tid].tdetach =
MkAtomTerm(AtomTrue);
DEBUG_TLOCK_ACCESS(30, tid);
pthread_mutex_unlock(&(ThreadHandle[tid].tlock));
return TRUE;
}
@ -610,16 +638,19 @@ p_thread_signal(void)
Int wid = IntegerOfTerm(Deref(ARG1));
/* make sure the lock is available */
pthread_mutex_lock(&(ThreadHandle[wid].tlock));
DEBUG_TLOCK_ACCESS(16, wid);
if (!ThreadHandle[wid].in_use ||
!ThreadHandle[wid].current_yaam_regs) {
DEBUG_TLOCK_ACCESS(17, wid);
pthread_mutex_unlock(&(ThreadHandle[wid].tlock));
return TRUE;
return TRUE;
}
LOCK(Yap_heap_regs->wl[wid].signal_lock);
ThreadHandle[wid].current_yaam_regs->CreepFlag_ =
Unsigned(ThreadHandle[wid].current_yaam_regs->LCL0_);
Yap_heap_regs->wl[wid].active_signals |= YAP_ITI_SIGNAL;
UNLOCK(Yap_heap_regs->wl[wid].signal_lock);
DEBUG_TLOCK_ACCESS(18, wid);
pthread_mutex_unlock(&(ThreadHandle[wid].tlock));
return TRUE;
}
@ -671,6 +702,7 @@ static Int
p_thread_unlock(void)
{ /* '$thread_self_lock' */
Int wid = IntegerOfTerm(Deref(ARG1));
DEBUG_TLOCK_ACCESS(19, wid);
pthread_mutex_unlock(&(ThreadHandle[wid].tlock));
return TRUE;
}

View File

@ -10,7 +10,7 @@
* File: Heap.h *
* mods: *
* comments: Heap Init Structure *
* version: $Id: Heap.h,v 1.131 2008-05-10 23:24:12 vsc Exp $ *
* version: $Id: Heap.h,v 1.132 2008-05-28 17:18:35 vsc Exp $ *
*************************************************************************/
/* information that can be stored in Code Space */
@ -198,6 +198,8 @@ typedef struct thandle {
int ref_count;
#ifdef LOW_LEVEL_TRACER
long long int thread_inst_count;
int been_here1;
int been_here2;
#endif
pthread_mutex_t tlock;
#if HAVE_GETRUSAGE