with_mutex should succeed even in single-thread mode

This commit is contained in:
Vitor Santos Costa 2014-11-09 17:26:08 +00:00
parent 5dfb438abb
commit e7fcc5d9a1

View File

@ -1276,7 +1276,7 @@ p_cond_broadcast( USES_REGS1 )
if (pthread_cond_broadcast(condp) < 0) if (pthread_cond_broadcast(condp) < 0)
return FALSE; return FALSE;
return TRUE; v return TRUE;
} }
static Int static Int
@ -1603,6 +1603,84 @@ p_max_workers(void)
return Yap_unify(ARG1,MkIntTerm(1)); return Yap_unify(ARG1,MkIntTerm(1));
} }
static Int
p_new_mutex(void)
{ /* '$max_workers'(+P) */
static int mutexes = 1;
return Yap_unify(ARG1, MkIntegerTerm(mutexes++) );
}
static Int
p_with_mutex( USES_REGS1 )
{
Int mut;
Term t1 = Deref(ARG1), excep;
Int rc = FALSE;
Int creeping = Yap_get_signal(YAP_CREEP_SIGNAL);
PredEntry *pe;
Term tm = CurrentModule;
Term tg = Deref(ARG2);
if (IsVarTerm(t1)) {
p_new_mutex( PASS_REGS1 );
t1 = Deref(ARG1);
mut = IntOfTerm(t1);
}
tg = Yap_StripModule(tg, &tm);
if (IsVarTerm(tg)) {
Yap_Error(INSTANTIATION_ERROR, ARG2, "with_mutex/2");
goto end;
} else if (IsApplTerm(tg)) {
register Functor f = FunctorOfTerm(tg);
register CELL *pt;
size_t i, arity;
f = FunctorOfTerm(tg);
if (IsExtensionFunctor(f)) {
Yap_Error(TYPE_ERROR_CALLABLE, tg, "with_mutex/2");
goto end;
}
arity = ArityOfFunctor(f);
if (arity > MaxTemps) {
Yap_Error(TYPE_ERROR_CALLABLE, tg, "with_mutex/2");
goto end;
}
pe = RepPredProp(PredPropByFunc(f, tm));
pt = RepAppl(tg)+1;
for (i= 0; i < arity; i++ )
XREGS[i+1] = pt[i];
} else if (IsAtomTerm(tg)) {
pe = RepPredProp(PredPropByAtom(AtomOfTerm(tg), tm));
} else if (IsPairTerm(tg)) {
register CELL *pt;
Functor f;
f = FunctorDot;
pe = RepPredProp(PredPropByFunc(f, tm));
pt = RepPair(tg);
XREGS[1] = pt[0];
XREGS[2] = pt[1];
} else {
Yap_Error(TYPE_ERROR_CALLABLE, tg, "with_mutex/2");
goto end;
}
if (
pe->OpcodeOfPred != FAIL_OPCODE &&
Yap_execute_pred(pe, NULL PASS_REGS) ) {
rc = TRUE;
}
end:
ARG1 = MkIntegerTerm(mut);
excep = Yap_GetException();
if (creeping) {
Yap_signal( YAP_CREEP_SIGNAL );
} else if ( excep != 0) {
return Yap_JumpToEnv(excep);
}
return rc;
}
void void
Yap_InitFirstWorkerThreadHandle(void) Yap_InitFirstWorkerThreadHandle(void)
{ {
@ -1610,6 +1688,8 @@ Yap_InitFirstWorkerThreadHandle(void)
void Yap_InitThreadPreds(void) void Yap_InitThreadPreds(void)
{ {
Yap_InitCPred("$with_mutex", 2, p_with_mutex, MetaPredFlag);
Yap_InitCPred("$new_mutex", 1, p_new_mutex, SafePredFlag);
Yap_InitCPred("$max_workers", 1, p_max_workers, 0); Yap_InitCPred("$max_workers", 1, p_max_workers, 0);
Yap_InitCPred("$thread_self", 1, p_thread_self, SafePredFlag); Yap_InitCPred("$thread_self", 1, p_thread_self, SafePredFlag);
Yap_InitCPred("$no_threads", 0, p_no_threads, SafePredFlag); Yap_InitCPred("$no_threads", 0, p_no_threads, SafePredFlag);