more fixes for thread support.

git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@998 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
This commit is contained in:
vsc 2004-02-20 02:28:19 +00:00
parent 026bfb3c18
commit 53c1eb54a2
6 changed files with 71 additions and 55 deletions

View File

@ -187,9 +187,6 @@ LookupAtom(char *atom)
if (NOfAtoms > 2*AtomHashTableSize) { if (NOfAtoms > 2*AtomHashTableSize) {
Yap_signal(YAP_CDOVF_SIGNAL); Yap_signal(YAP_CDOVF_SIGNAL);
} }
{extern int vsc_xstop;
if (ae == 0x81cf80c) vsc_xstop = 1;
}
return na; return na;
} }

View File

@ -254,15 +254,8 @@ decrease_ref_counter(yamop *ptr, yamop *b, yamop *e, yamop *sc)
!(cl->ClRefCount) && !(cl->ClRefCount) &&
!(cl->ClFlags & InUseMask)) { !(cl->ClFlags & InUseMask)) {
/* last ref to the clause */ /* last ref to the clause */
#if defined(YAPOR) || defined(THREADS)
/* can't do erase now without risking deadlocks */
cl->ClRefCount++;
TRAIL_CLREF(cl);
UNLOCK(cl->ClLock);
#else
UNLOCK(cl->ClLock); UNLOCK(cl->ClLock);
Yap_ErLogUpdCl(cl); Yap_ErLogUpdCl(cl);
#endif
} else { } else {
UNLOCK(cl->ClLock); UNLOCK(cl->ClLock);
} }
@ -415,9 +408,9 @@ kill_first_log_iblock(LogUpdIndex *c, LogUpdIndex *parent, PredEntry *ap)
LogUpdIndex *ncl; LogUpdIndex *ncl;
/* parent is always locked, now I lock myself */ /* parent is always locked, now I lock myself */
LOCK(c->ClLock); LOCK(c->ClLock);
if (parent != NULL && if (parent != NULL) {
!(c->ClFlags & ErasedMask)) {
/* remove myself from parent */ /* remove myself from parent */
LOCK(parent->ClLock);
if (c == parent->ChildIndex) { if (c == parent->ChildIndex) {
parent->ChildIndex = c->SiblingIndex; parent->ChildIndex = c->SiblingIndex;
} else { } else {
@ -427,14 +420,17 @@ kill_first_log_iblock(LogUpdIndex *c, LogUpdIndex *parent, PredEntry *ap)
} }
tcl->SiblingIndex = c->SiblingIndex; tcl->SiblingIndex = c->SiblingIndex;
} }
UNLOCK(parent->ClLock);
} }
/* make sure that a child cannot remove us */ /* make sure that a child cannot remove us */
c->ClRefCount++; c->ClRefCount++;
ncl = c->ChildIndex; ncl = c->ChildIndex;
while (ncl != NULL) { /* kill children */
LogUpdIndex *next = ncl->SiblingIndex; while (ncl) {
UNLOCK(c->ClLock);
kill_first_log_iblock(ncl, c, ap); kill_first_log_iblock(ncl, c, ap);
ncl = next; LOCK(c->ClLock);
ncl = c->ChildIndex;
} }
c->ClRefCount--; c->ClRefCount--;
/* check if we are still the main index */ /* check if we are still the main index */
@ -442,6 +438,7 @@ kill_first_log_iblock(LogUpdIndex *c, LogUpdIndex *parent, PredEntry *ap)
ap->cs.p_code.TrueCodeOfPred == c->ClCode) { ap->cs.p_code.TrueCodeOfPred == c->ClCode) {
RemoveMainIndex(ap); RemoveMainIndex(ap);
} }
/* decrease refs */
decrease_log_indices(c, (yamop *)&(ap->cs.p_code.ExpandCode)); decrease_log_indices(c, (yamop *)&(ap->cs.p_code.ExpandCode));
#ifdef DEBUG #ifdef DEBUG
{ {
@ -458,6 +455,8 @@ kill_first_log_iblock(LogUpdIndex *c, LogUpdIndex *parent, PredEntry *ap)
#endif #endif
if (!((c->ClFlags & InUseMask) || c->ClRefCount)) { if (!((c->ClFlags & InUseMask) || c->ClRefCount)) {
if (parent != NULL) { if (parent != NULL) {
/* sat bye bye */
LOCK(parent->ClLock);
parent->ClRefCount--; parent->ClRefCount--;
if (parent->ClFlags & ErasedMask && if (parent->ClFlags & ErasedMask &&
!(parent->ClFlags & InUseMask) && !(parent->ClFlags & InUseMask) &&
@ -466,34 +465,35 @@ kill_first_log_iblock(LogUpdIndex *c, LogUpdIndex *parent, PredEntry *ap)
if (parent->ClFlags & SwitchRootMask) { if (parent->ClFlags & SwitchRootMask) {
UNLOCK(parent->ClLock); UNLOCK(parent->ClLock);
kill_first_log_iblock(parent, NULL, ap); kill_first_log_iblock(parent, NULL, ap);
LOCK(parent->ClLock);
} else { } else {
LOCK(parent->u.ParentIndex->ClLock);
UNLOCK(parent->ClLock); UNLOCK(parent->ClLock);
kill_first_log_iblock(parent, parent->u.ParentIndex, ap); kill_first_log_iblock(parent, parent->u.ParentIndex, ap);
LOCK(parent->ClLock);
UNLOCK(parent->u.ParentIndex->ClLock);
} }
} else {
UNLOCK(parent->ClLock);
} }
} }
UNLOCK(c->ClLock); UNLOCK(c->ClLock);
Yap_FreeCodeSpace((CODEADDR)c); Yap_FreeCodeSpace((CODEADDR)c);
} else { } else {
c->ClFlags |= ErasedMask;
/* try to move up, so that we don't hold a switch table */
if (parent != NULL &&
parent->ClFlags & SwitchTableMask) {
LOCK(parent->ClLock);
c->u.ParentIndex = parent->u.ParentIndex;
LOCK(parent->u.ParentIndex->ClLock);
parent->u.ParentIndex->ClRefCount++;
UNLOCK(parent->u.ParentIndex->ClLock);
parent->ClRefCount--;
UNLOCK(parent->ClLock);
}
c->ChildIndex = NULL;
#ifdef DEBUG #ifdef DEBUG
c->SiblingIndex = DBErasedIList; c->SiblingIndex = DBErasedIList;
DBErasedIList = c; DBErasedIList = c;
#endif #endif
c->ClFlags |= ErasedMask;
#if !defined(THREADS) && !defined(YAPOR)
/* try to move up, so that we don't hold an index */
if (parent != NULL &&
parent->ClFlags & SwitchTableMask) {
c->u.ParentIndex = parent->u.ParentIndex;
parent->u.ParentIndex->ClRefCount++;
parent->ClRefCount--;
}
#endif
c->ChildIndex = NULL;
UNLOCK(c->ClLock); UNLOCK(c->ClLock);
} }
} }
@ -512,9 +512,18 @@ Yap_kill_iblock(ClauseUnion *blk, ClauseUnion *parent_blk, PredEntry *ap)
LogUpdIndex *c = (LogUpdIndex *)blk; LogUpdIndex *c = (LogUpdIndex *)blk;
if (parent_blk != NULL) { if (parent_blk != NULL) {
LogUpdIndex *cl = (LogUpdIndex *)parent_blk; LogUpdIndex *cl = (LogUpdIndex *)parent_blk;
#if defined(THREADS) || defined(YAPOR)
LOCK(cl->ClLock); LOCK(cl->ClLock);
kill_first_log_iblock(c, cl, ap); /* protect against attempts at erasing */
cl->ClRefCount++;
UNLOCK(cl->ClLock); UNLOCK(cl->ClLock);
#endif
kill_first_log_iblock(c, cl, ap);
#if defined(THREADS) || defined(YAPOR)
LOCK(cl->ClLock);
cl->ClRefCount--;
UNLOCK(cl->ClLock);
#endif
} else { } else {
kill_first_log_iblock(c, NULL, ap); kill_first_log_iblock(c, NULL, ap);
} }
@ -548,13 +557,23 @@ Yap_ErLogUpdIndex(LogUpdIndex *clau)
return; return;
} }
if (c->ClFlags & SwitchRootMask) { if (c->ClFlags & SwitchRootMask) {
kill_first_log_iblock(clau, NULL, c->u.pred); kill_first_log_iblock(clau, NULL, c->u.pred);
} else { } else {
while (!(c->ClFlags & SwitchRootMask)) while (!(c->ClFlags & SwitchRootMask))
c = c->u.ParentIndex; c = c->u.ParentIndex;
#if defined(THREADS) || defined(YAPOR)
LOCK(clau->u.ParentIndex->ClLock); LOCK(clau->u.ParentIndex->ClLock);
kill_first_log_iblock(clau, clau->u.ParentIndex, c->u.pred); /* protect against attempts at erasing */
clau->ClRefCount++;
UNLOCK(clau->u.ParentIndex->ClLock); UNLOCK(clau->u.ParentIndex->ClLock);
#endif
kill_first_log_iblock(clau, clau->u.ParentIndex, c->u.pred);
#if defined(THREADS) || defined(YAPOR)
LOCK(clau->u.ParentIndex->ClLock);
/* protect against attempts at erasing */
clau->ClRefCount--;
UNLOCK(clau->u.ParentIndex->ClLock);
#endif
} }
} }
@ -572,9 +591,19 @@ Yap_RemoveLogUpdIndex(LogUpdIndex *cl)
while (!(pcl->ClFlags & SwitchRootMask)) { while (!(pcl->ClFlags & SwitchRootMask)) {
pcl = pcl->u.ParentIndex; pcl = pcl->u.ParentIndex;
} }
#if defined(THREADS) || defined(YAPOR)
LOCK(cl->u.ParentIndex->ClLock); LOCK(cl->u.ParentIndex->ClLock);
kill_first_log_iblock(cl, cl->u.ParentIndex, pcl->u.pred); /* protect against attempts at erasing */
cl->u.ParentIndex->ClRefCount++;
UNLOCK(cl->u.ParentIndex->ClLock); UNLOCK(cl->u.ParentIndex->ClLock);
#endif
kill_first_log_iblock(cl, cl->u.ParentIndex, pcl->u.pred);
#if defined(THREADS) || defined(YAPOR)
LOCK(cl->u.ParentIndex->ClLock);
/* protect against attempts at erasing */
cl->u.ParentIndex->ClRefCount--;
UNLOCK(cl->u.ParentIndex->ClLock);
#endif
} }
} }

View File

@ -3852,17 +3852,21 @@ EraseLogUpdCl(LogUpdClause *clau)
PredEntry *ap; PredEntry *ap;
LOCK(clau->ClLock); LOCK(clau->ClLock);
ap = clau->ClPred; ap = clau->ClPred;
WRITE_LOCK(ap->PRWLock);
/* no need to erase what has been erased */ /* no need to erase what has been erased */
if (!(clau->ClFlags & ErasedMask)) { if (!(clau->ClFlags & ErasedMask)) {
/* get ourselves out of the list */ /* get ourselves out of the list */
if (clau->ClNext != NULL) { if (clau->ClNext != NULL) {
LOCK(clau->ClNext->ClLock);
clau->ClNext->ClPrev = clau->ClPrev; clau->ClNext->ClPrev = clau->ClPrev;
UNLOCK(clau->ClNext->ClLock);
} }
if (clau->ClPrev != NULL) { if (clau->ClPrev != NULL) {
LOCK(clau->ClPrev->ClLock);
clau->ClPrev->ClNext = clau->ClNext; clau->ClPrev->ClNext = clau->ClNext;
UNLOCK(clau->ClPrev->ClLock);
} }
WRITE_LOCK(ap->PRWLock);
if (clau->ClCode == ap->cs.p_code.FirstClause) { if (clau->ClCode == ap->cs.p_code.FirstClause) {
if (clau->ClNext == NULL) { if (clau->ClNext == NULL) {
ap->cs.p_code.FirstClause = NULL; ap->cs.p_code.FirstClause = NULL;
@ -3879,6 +3883,7 @@ EraseLogUpdCl(LogUpdClause *clau)
} }
clau->ClFlags |= ErasedMask; clau->ClFlags |= ErasedMask;
ap->cs.p_code.NOfClauses--; ap->cs.p_code.NOfClauses--;
WRITE_UNLOCK(ap->PRWLock);
#ifdef DEBUG #ifdef DEBUG
{ {
LogUpdClause *er_head = DBErasedList; LogUpdClause *er_head = DBErasedList;
@ -3900,9 +3905,8 @@ EraseLogUpdCl(LogUpdClause *clau)
LOCK(clau->ClLock); LOCK(clau->ClLock);
clau->ClRefCount--; clau->ClRefCount--;
} }
complete_lu_erase(clau);
UNLOCK(clau->ClLock); UNLOCK(clau->ClLock);
WRITE_UNLOCK(ap->PRWLock); complete_lu_erase(clau);
} }
static void static void

View File

@ -4427,6 +4427,7 @@ replace_index_block(ClauseUnion *parent_block, yamop *cod, yamop *ncod, PredEntr
ncl->ClRefCount = cl->ClRefCount; ncl->ClRefCount = cl->ClRefCount;
ncl->ChildIndex = cl->ChildIndex; ncl->ChildIndex = cl->ChildIndex;
ncl->u.ParentIndex = cl->u.ParentIndex; ncl->u.ParentIndex = cl->u.ParentIndex;
INIT_LOCK(ncl->ClLock);
if (c == cl) { if (c == cl) {
parent_block->lui.ChildIndex = ncl; parent_block->lui.ChildIndex = ncl;
} else { } else {
@ -4748,16 +4749,9 @@ cp_lu_trychain(yamop *codep, yamop *ocodep, yamop *ostart, int flag, PredEntry *
if (tgl->ClFlags & ErasedMask && if (tgl->ClFlags & ErasedMask &&
!(tgl->ClRefCount) && !(tgl->ClRefCount) &&
!(tgl->ClFlags & InUseMask)) { !(tgl->ClFlags & InUseMask)) {
#if defined(YAPOR) || defined(THREADS)
/* can't do erase now without risking deadlocks */
tgl->ClRefCount++;
TRAIL_CLREF(tgl);
UNLOCK(tgl->ClLock);
#else
/* last ref to the clause */ /* last ref to the clause */
UNLOCK(tgl->ClLock); UNLOCK(tgl->ClLock);
Yap_ErLogUpdCl(tgl); Yap_ErLogUpdCl(tgl);
#endif
} else { } else {
UNLOCK(tgl->ClLock); UNLOCK(tgl->ClLock);
} }
@ -4800,15 +4794,8 @@ cp_lu_trychain(yamop *codep, yamop *ocodep, yamop *ostart, int flag, PredEntry *
!(tgl->ClRefCount) && !(tgl->ClRefCount) &&
!(tgl->ClFlags & InUseMask)) { !(tgl->ClFlags & InUseMask)) {
/* last ref to the clause */ /* last ref to the clause */
#if defined(YAPOR) || defined(THREADS)
/* can't do erase now without risking deadlocks */
tgl->ClRefCount++;
TRAIL_CLREF(tgl);
UNLOCK(tgl->ClLock);
#else
UNLOCK(tgl->ClLock); UNLOCK(tgl->ClLock);
Yap_ErLogUpdCl(tgl); Yap_ErLogUpdCl(tgl);
#endif
} else { } else {
UNLOCK(tgl->ClLock); UNLOCK(tgl->ClLock);
} }
@ -4868,10 +4855,8 @@ replace_lu_block(LogUpdIndex *blk, int flag, PredEntry *ap, yamop *code, int has
ncl->ClRefCount = 0; ncl->ClRefCount = 0;
ncl->u.ParentIndex = blk->u.ParentIndex; ncl->u.ParentIndex = blk->u.ParentIndex;
ncl->ChildIndex = NULL; ncl->ChildIndex = NULL;
#if defined(YAPOR) || defined(THREADS)
INIT_LOCK(ncl->ClLock); INIT_LOCK(ncl->ClLock);
INIT_CLREF_COUNT(ncl); INIT_CLREF_COUNT(ncl);
#endif
codep = start = ncl->ClCode; codep = start = ncl->ClCode;
/* ok, we've allocated and set up things, now let's finish */ /* ok, we've allocated and set up things, now let's finish */
codep->opc = Yap_opcode(_enter_lu_pred); codep->opc = Yap_opcode(_enter_lu_pred);

View File

@ -116,7 +116,8 @@ low_level_trace(yap_low_level_port port, PredEntry *pred, CELL *args)
LOCK(heap_regs->low_level_trace_lock); LOCK(heap_regs->low_level_trace_lock);
vsc_count++; vsc_count++;
if (vsc_count < 12000) { if (vsc_count == 213471) vsc_xstop = 1;
if (vsc_count < 1200000) {
UNLOCK(heap_regs->low_level_trace_lock); UNLOCK(heap_regs->low_level_trace_lock);
return; return;
} }

View File

@ -153,7 +153,7 @@ typedef union clause_obj {
#define INIT_CLREF_COUNT(X) (X)->ClRefCount = 0 #define INIT_CLREF_COUNT(X) (X)->ClRefCount = 0
#define INC_CLREF_COUNT(X) (X)->ClRefCount++ #define INC_CLREF_COUNT(X) (X)->ClRefCount++
#define DEC_CLREF_COUNT(X) (X)->ClRefCount-- #define DEC_CLREF_COUNT(X) (X)->ClRefCount--
#define CL_IN_USE(X) ((X)->ClRefCount != 0) #define CL_IN_USE(X) ((X)->ClRefCount)
#else #else
#define INIT_CLREF_COUNT(X) #define INIT_CLREF_COUNT(X)
#define INC_CLREF_COUNT(X) #define INC_CLREF_COUNT(X)