|
From: Vitor S. C. <vs...@us...> - 2008-08-06 17:32:24
|
Update of /cvsroot/yap/C In directory sc8-pr-cvs10.sourceforge.net:/tmp/cvs-serv31799/C Modified Files: absmi.c compiler.c dbase.c init.c threads.c tracer.c utilpreds.c Log Message: more thread fixes Index: absmi.c =================================================================== RCS file: /cvsroot/yap/C/absmi.c,v retrieving revision 1.242 retrieving revision 1.243 diff -u -r1.242 -r1.243 --- absmi.c 17 Jun 2008 13:37:48 -0000 1.242 +++ absmi.c 6 Aug 2008 17:32:18 -0000 1.243 @@ -12,6 +12,9 @@ * comments: Portable abstract machine interpreter * * Last rev: $Date$,$Author$ * * $Log$ +* Revision 1.243 2008/08/06 17:32:18 vsc +* more thread fixes +* * Revision 1.242 2008/06/17 13:37:48 vsc * fix c_interface not to crash when people try to recover slots that are * not there. @@ -1564,6 +1567,11 @@ ASP = YREG+E_CB; saveregs(); + if (cl->ClSource == NULL) { + fprintf(stderr,"%d CLLLLL %p %p %s\n",worker_id,cl,cl->ClSource,RepAtom(cl->ClPred->FunctorOfPred)->StrOfAE); + exit(1); + FAIL(); + } while ((t = Yap_FetchTermFromDB(cl->ClSource)) == 0L) { if (Yap_Error_TYPE == OUT_OF_ATTVARS_ERROR) { Yap_Error_TYPE = YAP_NO_ERROR; @@ -8205,9 +8213,11 @@ CACHE_Y(B); #if defined(YAPOR) || defined(THREADS) - PP = PREG->u.lld.d->ClPred; + if (!PP) { + PP = PREG->u.lld.d->ClPred; + LOCK(PP->PELock); + } #endif - LOCK(PP->PELock); timestamp = IntegerOfTerm(((CELL *)(B_YREG+1))[PREG->u.lld.t.s]); /* fprintf(stderr,"^ %p/%p %d %d %d--%u\n",PREG,PREG->u.lld.d->ClPred,timestamp,PREG->u.lld.d->ClPred->TimeStampOfPred,PREG->u.lld.d->ClTimeStart,PREG->u.lld.d->ClTimeEnd);*/ if (!VALID_TIMESTAMP(timestamp, PREG->u.lld.d)) { @@ -8239,15 +8249,17 @@ UInt timestamp = IntegerOfTerm(((CELL *)(B_YREG+1))[ap->ArityOfPE]); /* fprintf(stderr,"- %p/%p %d %d %p\n",PREG,ap,timestamp,ap->TimeStampOfPred,PREG->u.lld.d->ClCode);*/ - LOCK(ap->PELock); +#if defined(YAPOR) || defined(THREADS) + if (!PP) { + LOCK(ap->PELock); + PP = ap; + } +#endif if (!VALID_TIMESTAMP(timestamp, lcl)) { /* jump to next alternative */ PREG = FAILCODE; } else { PREG = lcl->ClCode; -#if defined(YAPOR) || defined(THREADS) - PP = ap; -#endif } /* HEY, leave indexing block alone!! */ /* check if we are the ones using this code */ @@ -8332,6 +8344,12 @@ } SET_BB(B_YREG); ENDCACHE_Y(); +#if defined(YAPOR) || defined(THREADS) + if (PREG == FAILCODE) { + UNLOCK(PP->PELock); + PP = NULL; + } +#endif JMPNext(); } ENDBOp(); Index: compiler.c =================================================================== RCS file: /cvsroot/yap/C/compiler.c,v retrieving revision 1.88 retrieving revision 1.89 diff -u -r1.88 -r1.89 --- compiler.c 13 Mar 2008 14:37:58 -0000 1.88 +++ compiler.c 6 Aug 2008 17:32:18 -0000 1.89 @@ -13,6 +13,9 @@ * * * Last rev: $Date$,$Author$ * * $Log$ +* Revision 1.89 2008/08/06 17:32:18 vsc +* more thread fixes +* * Revision 1.88 2008/03/13 14:37:58 vsc * update chr * @@ -704,10 +707,13 @@ write_num_op), (CELL) t, Zero, &cglobs->cint); } else if (IsPairTerm(t)) { if (optimizer_on && level < 6) { +#if !defined(THREADS) + /* discard code sharing because we cannot write on shared stuff */ if (!(cglobs->cint.CurrentPred->PredFlags & (DynamicPredFlag|LogUpdatePredFlag))) { if (try_store_as_dbterm(t, argno, arity, level, cglobs)) return; - } + } +#endif t = optimize_ce(t, arity, level, cglobs); if (IsVarTerm(t)) { c_var(t, argno, arity, level, cglobs); Index: dbase.c =================================================================== RCS file: /cvsroot/yap/C/dbase.c,v retrieving revision 1.174 retrieving revision 1.175 diff -u -r1.174 -r1.175 --- dbase.c 13 Jun 2008 14:10:27 -0000 1.174 +++ dbase.c 6 Aug 2008 17:32:19 -0000 1.175 @@ -1504,7 +1504,7 @@ NOfCells = ntp - ntp0; /* End Of Code Info */ *dbg->lr++ = 0; NOfLinks = (dbg->lr - dbg->LinkAr); - if (vars_found || InFlag & InQueue) { + if (vars_found || InFlag & InQueue ) { /* * Take into account the fact that one needs an entry @@ -1800,10 +1800,16 @@ yamop *ipc; int needs_vars = FALSE; struct db_globs dbg; + int d_flag = 0; +#ifdef THREADS + /* we cannot allow sharing between threads (for now) */ + if (!(pe->PredFlags & ThreadLocalPredFlag)) + d_flag |= InQueue; +#endif s_dbg = &dbg; ipc = NEXTOP(((LogUpdClause *)NULL)->ClCode,e); - if ((x = (DBTerm *)CreateDBStruct(t, NULL, 0, &needs_vars, (UInt)ipc, &dbg)) == NULL) { + if ((x = (DBTerm *)CreateDBStruct(t, NULL, d_flag, &needs_vars, (UInt)ipc, &dbg)) == NULL) { return NULL; /* crash */ } cl = (LogUpdClause *)((ADDR)x-(UInt)ipc); @@ -1928,7 +1934,7 @@ p_rcda(void) { /* Idiotic xlc's cpp does not work with ARG1 within MkDBRefTerm */ - Term TRef, t1 = Deref(ARG1), t2 = Deref(ARG2); + Term TRef, t1 = Deref(ARG1); PredEntry *pe = NULL; if (!IsVarTerm(Deref(ARG3))) @@ -1940,7 +1946,7 @@ LogUpdClause *cl; LOCK(pe->PELock); - cl = record_lu(pe, t2, MkFirst); + cl = record_lu(pe, Deref(ARG2), MkFirst); if (cl != NULL) { TRAIL_CLREF(cl); #if defined(YAPOR) || defined(THREADS) @@ -1954,11 +1960,10 @@ } UNLOCK(pe->PELock); } else { - TRef = MkDBRefTerm(record(MkFirst, t1, t2, Unsigned(0))); + TRef = MkDBRefTerm(record(MkFirst, t1, Deref(ARG2), Unsigned(0))); } if (Yap_Error_TYPE != YAP_NO_ERROR) { if (recover_from_record_error(3)) { - t2 = Deref(ARG2); goto restart_record; } else { return FALSE; Index: init.c =================================================================== RCS file: /cvsroot/yap/C/init.c,v retrieving revision 1.173 retrieving revision 1.174 diff -u -r1.173 -r1.174 --- init.c 22 Jul 2008 23:34:48 -0000 1.173 +++ init.c 6 Aug 2008 17:32:19 -0000 1.174 @@ -985,6 +985,7 @@ Yap_heap_regs->thread_handle[0].handle = pthread_self(); Yap_heap_regs->thread_handle[0].handle = pthread_self(); pthread_mutex_init(&ThreadHandle[0].tlock, NULL); + pthread_mutex_init(&ThreadHandle[0].tlock_status, NULL); Yap_heap_regs->n_of_threads = 1; Yap_heap_regs->n_of_threads_created = 1; Yap_heap_regs->threads_total_time = 0; Index: threads.c =================================================================== RCS file: /cvsroot/yap/C/threads.c,v retrieving revision 1.42 retrieving revision 1.43 diff -u -r1.42 -r1.43 --- threads.c 3 Jun 2008 09:24:27 -0000 1.42 +++ threads.c 6 Aug 2008 17:32:20 -0000 1.43 @@ -38,11 +38,9 @@ * */ -#if DEBUG - +#if DEBUGX static void DEBUG_TLOCK_ACCESS( int pos, int wid) { - ThreadHandle[wid].been_here2 = ThreadHandle[wid].been_here1; - ThreadHandle[wid].been_here1 = pos; + fprintf(stderr,"wid=%p %p\n", wid, pos); } #else #define DEBUG_TLOCK_ACCESS(WID, POS) @@ -58,8 +56,8 @@ ThreadHandle[new_worker_id].zombie == TRUE) ) new_worker_id++; if (new_worker_id < MAX_THREADS) { - pthread_mutex_lock(&(ThreadHandle[new_worker_id].tlock)); DEBUG_TLOCK_ACCESS(new_worker_id, 0); + pthread_mutex_lock(&(ThreadHandle[new_worker_id].tlock)); ThreadHandle[new_worker_id].in_use = TRUE; } else { new_worker_id = -1; @@ -98,7 +96,7 @@ static void -kill_thread_engine (int wid) +kill_thread_engine (int wid, int always_die) { Prop p0 = AbsPredProp(Yap_heap_regs->thread_handle[wid].local_preds); @@ -110,33 +108,32 @@ Yap_FreeCodeSpace((char *)ap); } Yap_KillStacks(wid); - Yap_FreeCodeSpace((ADDR)(ThreadHandle[wid].tgoal)); - ThreadHandle[wid].tgoal = NULL; Yap_heap_regs->wl[wid].active_signals = 0L; free(Yap_heap_regs->wl[wid].scratchpad.ptr); free(ThreadHandle[wid].default_yaam_regs); ThreadHandle[wid].current_yaam_regs = NULL; 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)); + LOCK(ThreadHandlesLock); + if (ThreadHandle[wid].tdetach == MkAtomTerm(AtomTrue) || + always_die) { + ThreadHandle[wid].zombie = FALSE; + ThreadHandle[wid].in_use = FALSE; + DEBUG_TLOCK_ACCESS(1, wid); + pthread_mutex_unlock(&(ThreadHandle[wid].tlock)); + } + UNLOCK(ThreadHandlesLock); } static void thread_die(int wid, int always_die) { - LOCK(ThreadHandlesLock); if (!always_die) { /* called by thread itself */ ThreadsTotalTime += Yap_cputime(); } - if (ThreadHandle[wid].tdetach == MkAtomTerm(AtomTrue) || - always_die) { - kill_thread_engine(wid); - } - UNLOCK(ThreadHandlesLock); + kill_thread_engine(wid, always_die); } static void @@ -195,6 +192,8 @@ } } } while (t == 0); + free(ThreadHandle[myworker_id].tgoal); + ThreadHandle[myworker_id].tgoal = NULL; tgs[1] = ThreadHandle[worker_id].tdetach; tgoal = Yap_MkApplTerm(FunctorThreadRun, 2, tgs); Yap_RunTopGoal(tgoal); @@ -230,6 +229,7 @@ Term x3 = Deref(ARG3); int new_worker_id = IntegerOfTerm(Deref(ARG6)); + // fprintf(stderr," %d --> %d\n", worker_id, new_worker_id); if (IsBigIntTerm(x2)) return FALSE; if (IsBigIntTerm(x3)) @@ -241,14 +241,13 @@ /* YAP ERROR */ return FALSE; } + /* make sure we can proceed */ if (!init_thread_engine(new_worker_id, ssize, tsize, tgoal, tdetach)) return FALSE; ThreadHandle[new_worker_id].id = new_worker_id; 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; } return FALSE; @@ -304,17 +303,36 @@ /* make sure the lock is available */ 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); + pthread_mutex_lock(&(ThreadHandle[worker_id].tlock)); 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; } + // fprintf(stderr," -- %d\n", worker_id); 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); +} + +static Int +p_thread_status_lock(void) +{ + /* make sure the lock is available */ + if (pthread_getspecific(Yap_yaamregs_key) == NULL) + return FALSE; + pthread_mutex_lock(&(ThreadHandle[worker_id].tlock_status)); + return Yap_unify(MkIntegerTerm(worker_id), ARG1); +} + +static Int +p_thread_status_unlock(void) +{ + /* make sure the lock is available */ + if (pthread_getspecific(Yap_yaamregs_key) == NULL) + return FALSE; + pthread_mutex_unlock(&(ThreadHandle[worker_id].tlock_status)); return Yap_unify(MkIntegerTerm(worker_id), ARG1); } @@ -346,8 +364,8 @@ Int Yap_thread_attach_engine(int wid) { - pthread_mutex_lock(&(ThreadHandle[wid].tlock)); DEBUG_TLOCK_ACCESS(7, wid); + pthread_mutex_lock(&(ThreadHandle[wid].tlock)); if (ThreadHandle[wid].ref_count && ThreadHandle[wid].handle != pthread_self()) { DEBUG_TLOCK_ACCESS(8, wid); @@ -365,8 +383,8 @@ Int Yap_thread_detach_engine(int wid) { - pthread_mutex_lock(&(ThreadHandle[wid].tlock)); DEBUG_TLOCK_ACCESS(10, wid); + pthread_mutex_lock(&(ThreadHandle[wid].tlock)); if (ThreadHandle[wid].handle == pthread_self()) ThreadHandle[wid].handle = 0; ThreadHandle[wid].ref_count--; @@ -379,7 +397,7 @@ Yap_thread_destroy_engine(int wid) { if (ThreadHandle[wid].ref_count == 0) { - kill_thread_engine(wid); + kill_thread_engine(wid, TRUE); return TRUE; } else { DEBUG_TLOCK_ACCESS(12, wid); @@ -404,8 +422,6 @@ UNLOCK(ThreadHandlesLock); 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) { @@ -421,7 +437,12 @@ { Int tid = IntegerOfTerm(Deref(ARG1)); - thread_die(tid, TRUE); + LOCK(ThreadHandlesLock); + ThreadHandle[tid].zombie = FALSE; + ThreadHandle[tid].in_use = FALSE; + DEBUG_TLOCK_ACCESS(32, tid); + pthread_mutex_unlock(&(ThreadHandle[tid].tlock)); + UNLOCK(ThreadHandlesLock); return TRUE; } @@ -445,6 +466,12 @@ } static Int +p_thread_detached(void) +{ + return Yap_unify(ARG1,ThreadHandle[worker_id].tdetach); +} + +static Int p_thread_exit(void) { thread_die(worker_id, FALSE); @@ -699,8 +726,15 @@ } static Int +p_thread_self_lock(void) +{ /* '$thread_unlock' */ + pthread_mutex_lock(&(ThreadHandle[worker_id].tlock)); + return Yap_unify(ARG1,MkIntegerTerm(worker_id)); +} + +static Int p_thread_unlock(void) -{ /* '$thread_self_lock' */ +{ /* '$thread_unlock' */ Int wid = IntegerOfTerm(Deref(ARG1)); DEBUG_TLOCK_ACCESS(19, wid); pthread_mutex_unlock(&(ThreadHandle[wid].tlock)); @@ -715,11 +749,14 @@ Yap_InitCPred("$thread_new_tid", 1, p_thread_new_tid, HiddenPredFlag); Yap_InitCPred("$create_thread", 6, p_create_thread, HiddenPredFlag); Yap_InitCPred("$thread_self", 1, p_thread_self, SafePredFlag|HiddenPredFlag); + Yap_InitCPred("$thread_status_lock", 1, p_thread_status_lock, SafePredFlag|HiddenPredFlag); + Yap_InitCPred("$thread_status_unlock", 1, p_thread_status_unlock, SafePredFlag|HiddenPredFlag); Yap_InitCPred("$thread_zombie_self", 1, p_thread_zombie_self, SafePredFlag|HiddenPredFlag); Yap_InitCPred("$thread_join", 1, p_thread_join, HiddenPredFlag); Yap_InitCPred("$thread_destroy", 1, p_thread_destroy, HiddenPredFlag); Yap_InitCPred("thread_yield", 0, p_thread_yield, 0); Yap_InitCPred("$detach_thread", 1, p_thread_detach, HiddenPredFlag); + Yap_InitCPred("$thread_detached", 1, p_thread_detached, HiddenPredFlag); Yap_InitCPred("$thread_exit", 0, p_thread_exit, HiddenPredFlag); Yap_InitCPred("thread_setconcurrency", 2, p_thread_set_concurrency, 0); Yap_InitCPred("$valid_thread", 1, p_valid_thread, HiddenPredFlag); @@ -739,6 +776,7 @@ Yap_InitCPred("$nof_threads_created", 1, p_nof_threads_created, SafePredFlag|HiddenPredFlag); Yap_InitCPred("$thread_sleep", 4, p_thread_sleep, SafePredFlag|HiddenPredFlag); Yap_InitCPred("$thread_runtime", 1, p_thread_runtime, SafePredFlag|HiddenPredFlag); + Yap_InitCPred("$thread_self_lock", 1, p_thread_self_lock, SafePredFlag); Yap_InitCPred("$thread_unlock", 1, p_thread_unlock, SafePredFlag); } Index: tracer.c =================================================================== RCS file: /cvsroot/yap/C/tracer.c,v retrieving revision 1.161 retrieving revision 1.162 diff -u -r1.161 -r1.162 --- tracer.c 8 Jun 2008 08:05:11 -0000 1.161 +++ tracer.c 6 Aug 2008 17:32:20 -0000 1.162 @@ -66,7 +66,7 @@ omax_write_args = max_write_args; max_depth = 5; max_list = 5; - max_write_args = 5; + max_write_args = 10; Yap_plwrite(args[i], TracePutchar, Handle_vars_f); max_depth = omax_depth; max_list = omax_list; @@ -361,6 +361,12 @@ return(TRUE); } +static Int p_show_low_level_trace(void) +{ + fprintf(stderr,"Call counter=%lld\n",vsc_count); + return(TRUE); +} + #ifdef THREADS static Int p_start_low_level_trace2(void) { @@ -395,6 +401,7 @@ Yap_InitCPred("start_low_level_trace", 1, p_start_low_level_trace2, SafePredFlag); #endif Yap_InitCPred("stop_low_level_trace", 0, p_stop_low_level_trace, SafePredFlag); + Yap_InitCPred("show_low_level_trace", 0, p_show_low_level_trace, SafePredFlag); Yap_InitCPred("vsc_wait", 0, p_vsc_wait, SafePredFlag); } Index: utilpreds.c =================================================================== RCS file: /cvsroot/yap/C/utilpreds.c,v retrieving revision 1.63 retrieving revision 1.64 diff -u -r1.63 -r1.64 --- utilpreds.c 10 May 2008 23:24:12 -0000 1.63 +++ utilpreds.c 6 Aug 2008 17:32:20 -0000 1.64 @@ -24,6 +24,9 @@ #include "yapio.h" #include "eval.h" #include "attvar.h" +#ifdef HAVE_STRING_H +#include "string.h" +#endif typedef struct { Term old_var; @@ -168,9 +171,30 @@ INC_DBREF_COUNT(entryref); UNLOCK(entryref->lock); } + *ptf++ = d0; /* you can just copy other extensions. */ } #endif - *ptf++ = d0; /* you can just copy other extensions. */ + else if (!share) { + UInt sz; + + *ptf++ = AbsAppl(H); /* you can just copy other extensions. */ + /* make sure to copy floats */ + if (f== FunctorDouble) { + sz = sizeof(Float)/sizeof(CELL)+2; + } else if (f== FunctorLongInt) { + sz = 3; + } else { + CELL *pt = ap2+1; + sz = 2+sizeof(MP_INT)+(((MP_INT *)(pt+1))->_mp_alloc*sizeof(mp_limb_t)); + } + if (H+sz > ASP - 2048) { + goto overflow; + } + memcpy((void *)H, (void *)ap2, sz*sizeof(CELL)); + H += sz; + } else { + *ptf++ = d0; /* you can just copy other extensions. */ + } continue; } *ptf = AbsAppl(H); @@ -252,7 +276,7 @@ goto trail_overflow; } } - Bind_Global(ptd0, ptf[-1]); + Bind(ptd0, ptf[-1]); } } else { #endif @@ -264,7 +288,7 @@ goto trail_overflow; } } - Bind_Global(ptd0, (CELL)ptf); + Bind(ptd0, (CELL)ptf); ptf++; #ifdef COROUTINING } @@ -482,6 +506,11 @@ return CopyTerm(inp, 0, TRUE, TRUE); } +Term +Yap_CopyTermNoShare(Term inp) { + return CopyTerm(inp, 0, FALSE, FALSE); +} + static Int p_copy_term(void) /* copy term t to a new instance */ { |