begin of support for named mutexes.

This commit is contained in:
Vitor Santos Costa 2014-11-25 16:41:53 +00:00
parent ff6184863f
commit 50659967ed
3 changed files with 888 additions and 781 deletions

View File

@ -1242,7 +1242,7 @@ Yap_PutValue(Atom a, Term v)
WRITE_UNLOCK(p->VRWLock);
}
void
bool
Yap_PutAtomTranslation(Atom a, Int i)
{
AtomEntry *ae = RepAtom(a);
@ -1255,7 +1255,7 @@ Yap_PutAtomTranslation(Atom a, Int i)
p = (TranslationEntry *) Yap_AllocAtomSpace(sizeof(TranslationEntry));
if (p == NULL) {
WRITE_UNLOCK(ae->ARWLock);
return;
return false;
}
p->KindOfPE = TranslationProperty;
p->Translation = i;
@ -1264,6 +1264,32 @@ Yap_PutAtomTranslation(Atom a, Int i)
/* take care that the lock for the property will be inited even
if someone else searches for the property */
WRITE_UNLOCK(ae->ARWLock);
return true;
}
bool
Yap_PutAtomMutex(Atom a, void * i)
{
AtomEntry *ae = RepAtom(a);
Prop p0;
MutexEntry *p;
WRITE_LOCK(ae->ARWLock);
p0 = GetAPropHavingLock(ae, MutexProperty);
if (p0 == NIL) {
p = (MutexEntry *) Yap_AllocAtomSpace(sizeof(MutexEntry));
if (p == NULL) {
WRITE_UNLOCK(ae->ARWLock);
return false;
}
p->KindOfPE = MutexProperty;
p->Mutex = i;
AddPropToAtom(RepAtom(a), (PropEntry *)p);
}
/* take care that the lock for the property will be inited even
if someone else searches for the property */
WRITE_UNLOCK(ae->ARWLock);
return true;
}
Term

View File

@ -1,26 +1,26 @@
/*************************************************************************
* *
* YAP Prolog *
* *
* Yap Prolog was developed at NCCUP - Universidade do Porto *
* *
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
* *
**************************************************************************
* *
* File: stdpreds.c *
* Last rev: *
* mods: *
* comments: threads *
* *
*************************************************************************/
* *
* YAP Prolog *
* *
* Yap Prolog was developed at NCCUP - Universidade do Porto *
* *
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
* *
**************************************************************************
* *
* File: stdpreds.c *
* Last rev: *
* mods: *
* comments: threads *
* *
*************************************************************************/
#ifdef SCCS
static char SccsId[] = "%W% %G%";
#endif
/**
@ingroup Threads
@{
@ingroup Threads
@{
*/
#include "Yap.h"
@ -832,13 +832,10 @@ p_thread_exit( USES_REGS1 )
static Int
p_thread_set_concurrency( USES_REGS1 )
{
Term tnew = Deref(ARG2);
int newc;
#if HAVE_PTHREAD_GETCONCURRENCY
int cur;
#endif
int newc;
int cur;
Term tnew = Deref(ARG2);
if (IsVarTerm(tnew)) {
newc = 0;
} else if (IsIntegerTerm(tnew)) {
@ -847,7 +844,6 @@ int cur;
Yap_Error(TYPE_ERROR_INTEGER,tnew,"thread_set_concurrency/2");
return(FALSE);
}
#if HAVE_PTHREAD_GETCONCURRENCY
cur = MkIntegerTerm(pthread_getconcurrency());
if (pthread_setconcurrency(newc) != 0) {
return FALSE;
@ -906,40 +902,44 @@ p_new_mutex( USES_REGS1 )
pthread_mutex_init(&mutp->m, &mat);
mutp->owners = 0;
mutp->tid_own = 0;
return Yap_unify(ARG1, MkIntegerTerm((Int)mutp));
if (IsVarTerm((t1 = Deref(ARG1)))) {
return Yap_unify(t1, MkAddressTerm(mutp));
} else if(IsAtomTerm(t1)) {
return Yap_PutAtomMutex( AtomOfTerm(t1), mutp );
}
}
static Int
p_destroy_mutex( USES_REGS1 )
{
static Int
p_destroy_mutex( USES_REGS1 )
{
SWIMutex *mut = (SWIMutex*)IntegerOfTerm(Deref(ARG1));
if (pthread_mutex_destroy(&mut->m) < 0)
return FALSE;
Yap_FreeCodeSpace((void *)mut);
return TRUE;
}
}
static Int
p_lock_mutex( USES_REGS1 )
{
static Int
p_lock_mutex( USES_REGS1 )
{
SWIMutex *mut = (SWIMutex*)IntegerOfTerm(Deref(ARG1));
#if DEBUG_LOCKS
#if DEBUG_LOCKS
MUTEX_LOCK(&mut->m);
#else
#else
if (MUTEX_LOCK(&mut->m) < 0)
return FALSE;
#endif
#endif
mut->owners++;
mut->tid_own = worker_id;
return TRUE;
}
}
static Int
p_trylock_mutex( USES_REGS1 )
{
static Int
p_trylock_mutex( USES_REGS1 )
{
SWIMutex *mut = (SWIMutex*)IntegerOfTerm(Deref(ARG1));
if (MUTEX_TRYLOCK(&mut->m) == EBUSY)
@ -947,26 +947,26 @@ p_new_mutex( USES_REGS1 )
mut->owners++;
mut->tid_own = worker_id;
return TRUE;
}
}
static Int
p_unlock_mutex( USES_REGS1 )
{
static Int
p_unlock_mutex( USES_REGS1 )
{
SWIMutex *mut = (SWIMutex*)IntegerOfTerm(Deref(ARG1));
#if DEBUG_LOCKS
#if DEBUG_LOCKS
MUTEX_UNLOCK(&mut->m);
#else
#else
if (MUTEX_UNLOCK(&mut->m) < 0)
return FALSE;
#endif
#endif
mut->owners--;
return TRUE;
}
}
static Int
p_with_mutex( USES_REGS1 )
{
static Int
p_with_mutex( USES_REGS1 )
{
SWIMutex *mut;
Term t1 = Deref(ARG1), excep;
Int rc = FALSE;
@ -1037,12 +1037,12 @@ p_new_mutex( USES_REGS1 )
return Yap_JumpToEnv(excep);
}
return rc;
}
}
static Int
p_with_with_mutex( USES_REGS1 )
{
static Int
p_with_with_mutex( USES_REGS1 )
{
if (GLOBAL_WithMutex == NULL) {
p_new_mutex( PASS_REGS1 );
GLOBAL_WithMutex = (SWIMutex*)IntegerOfTerm(Deref(ARG1));
@ -1050,30 +1050,30 @@ p_new_mutex( USES_REGS1 )
ARG1 = MkIntegerTerm((Int)GLOBAL_WithMutex);
}
return p_lock_mutex( PASS_REGS1 );
}
}
static Int
p_unlock_with_mutex( USES_REGS1 )
{
static Int
p_unlock_with_mutex( USES_REGS1 )
{
ARG1 = MkIntegerTerm((Int)GLOBAL_WithMutex);
return p_unlock_mutex( PASS_REGS1 );
}
}
static Int
p_mutex_info( USES_REGS1 )
{
static Int
p_mutex_info( USES_REGS1 )
{
SWIMutex *mut = (SWIMutex*)IntegerOfTerm(Deref(ARG1));
return Yap_unify(ARG2, MkIntegerTerm(mut->owners)) &&
Yap_unify(ARG3, MkIntegerTerm(mut->tid_own));
return TRUE;
}
}
static Int
p_cond_create( USES_REGS1 )
{
static Int
p_cond_create( USES_REGS1 )
{
pthread_cond_t* condp;
condp = (pthread_cond_t *)Yap_AllocCodeSpace(sizeof(pthread_cond_t));
@ -1082,16 +1082,16 @@ p_new_mutex( USES_REGS1 )
}
pthread_cond_init(condp, NULL);
return Yap_unify(ARG1, MkIntegerTerm((Int)condp));
}
}
typedef struct {
typedef struct {
UInt indx;
mbox_t mbox;
} counted_mbox;
} counted_mbox;
static Int
p_mbox_create( USES_REGS1 )
{
static Int
p_mbox_create( USES_REGS1 )
{
Term namet = Deref(ARG1);
mbox_t* mboxp = GLOBAL_named_mboxes;
@ -1125,11 +1125,11 @@ p_new_mutex( USES_REGS1 )
bool rc = mboxCreate( namet, mboxp PASS_REGS );
UNLOCK(GLOBAL_mboxq_lock);
return rc;
}
}
static Int
p_mbox_destroy( USES_REGS1 )
{
static Int
p_mbox_destroy( USES_REGS1 )
{
Term namet = Deref(ARG1);
mbox_t* mboxp = GLOBAL_named_mboxes, *prevp;
@ -1157,11 +1157,11 @@ p_new_mutex( USES_REGS1 )
mboxDestroy(mboxp PASS_REGS);
Yap_FreeCodeSpace( (char *)mboxp );
return TRUE;
}
}
static mbox_t*
getMbox(Term t)
{
static mbox_t*
getMbox(Term t)
{
mbox_t* mboxp;
if (IsAtomTerm(t=Deref(t))) {
@ -1199,98 +1199,99 @@ p_new_mutex( USES_REGS1 )
return NULL;
}
return mboxp;
}
}
static Int
p_mbox_send( USES_REGS1 )
{
static Int
p_mbox_send( USES_REGS1 )
{
Term namet = Deref(ARG1);
mbox_t* mboxp = getMbox(namet) ;
if (!mboxp)
return FALSE;
return mboxSend(mboxp, Deref(ARG2) PASS_REGS);
}
}
static Int
p_mbox_size( USES_REGS1 )
{
static Int
p_mbox_size( USES_REGS1 )
{
Term namet = Deref(ARG1);
mbox_t* mboxp = getMbox(namet) ;
if (!mboxp)
return FALSE;
return Yap_unify( ARG2, MkIntTerm(mboxp->nmsgs));
}
}
static Int
p_mbox_receive( USES_REGS1 )
{
static Int
p_mbox_receive( USES_REGS1 )
{
Term namet = Deref(ARG1);
mbox_t* mboxp = getMbox(namet) ;
if (!mboxp)
return FALSE;
return mboxReceive(mboxp, Deref(ARG2) PASS_REGS);
}
}
static Int
p_mbox_peek( USES_REGS1 )
{
static Int
p_mbox_peek( USES_REGS1 )
{
Term namet = Deref(ARG1);
mbox_t* mboxp = getMbox(namet) ;
if (!mboxp)
return FALSE;
return mboxPeek(mboxp, Deref(ARG2) PASS_REGS);
}
}
static Int
p_cond_destroy( USES_REGS1 )
{
static Int
p_cond_destroy( USES_REGS1 )
{
pthread_cond_t *condp = (pthread_cond_t *)IntegerOfTerm(Deref(ARG1));
if (pthread_cond_destroy(condp) < 0)
return FALSE;
Yap_FreeCodeSpace((void *)condp);
return TRUE;
}
}
static Int
p_cond_signal( USES_REGS1 )
{
static Int
p_cond_signal( USES_REGS1 )
{
pthread_cond_t *condp = (pthread_cond_t *)IntegerOfTerm(Deref(ARG1));
if (pthread_cond_signal(condp) < 0)
return FALSE;
return TRUE;
}
}
static Int
p_cond_broadcast( USES_REGS1 )
{
static Int
p_cond_broadcast( USES_REGS1 )
{
pthread_cond_t *condp = (pthread_cond_t *)IntegerOfTerm(Deref(ARG1));
if (pthread_cond_broadcast(condp) < 0)
return FALSE;
v return TRUE;
}
else
return TRUE;
}
static Int
p_cond_wait( USES_REGS1 )
{
static Int
p_cond_wait( USES_REGS1 )
{
pthread_cond_t *condp = (pthread_cond_t *)IntegerOfTerm(Deref(ARG1));
SWIMutex *mut = (SWIMutex*)IntegerOfTerm(Deref(ARG2));
pthread_cond_wait(condp, &mut->m);
return TRUE;
}
}
static Int
p_thread_stacks( USES_REGS1 )
{ /* '$thread_signal'(+P) */
static Int
p_thread_stacks( USES_REGS1 )
{ /* '$thread_signal'(+P) */
Int tid = IntegerOfTerm(Deref(ARG1));
Int status= TRUE;
@ -1305,11 +1306,11 @@ p_new_mutex( USES_REGS1 )
}
MUTEX_UNLOCK(&(REMOTE_ThreadHandle(tid).tlock));
return FALSE;
}
}
static Int
p_thread_atexit( USES_REGS1 )
{ /* '$thread_signal'(+P) */
static Int
p_thread_atexit( USES_REGS1 )
{ /* '$thread_signal'(+P) */
Term t;
if (LOCAL_ThreadHandle.texit == NULL ||
@ -1338,13 +1339,13 @@ p_new_mutex( USES_REGS1 )
} while (t == 0);
LOCAL_ThreadHandle.texit = NULL;
return Yap_unify(ARG1, t) && Yap_unify(ARG2, LOCAL_ThreadHandle.texit_mod);
}
}
static Int
p_thread_signal( USES_REGS1 )
{ /* '$thread_signal'(+P) */
static Int
p_thread_signal( USES_REGS1 )
{ /* '$thread_signal'(+P) */
Int wid = IntegerOfTerm(Deref(ARG1));
/* make sure the lock is available */
MUTEX_LOCK(&(REMOTE_ThreadHandle(wid).tlock));
@ -1356,17 +1357,17 @@ p_new_mutex( USES_REGS1 )
Yap_external_signal( wid, YAP_ITI_SIGNAL );
MUTEX_UNLOCK(&(REMOTE_ThreadHandle(wid).tlock));
return TRUE;
}
}
static Int
p_no_threads( USES_REGS1 )
{ /* '$thread_signal'(+P) */
static Int
p_no_threads( USES_REGS1 )
{ /* '$thread_signal'(+P) */
return FALSE;
}
}
static Int
p_nof_threads( USES_REGS1 )
{ /* '$nof_threads'(+P) */
static Int
p_nof_threads( USES_REGS1 )
{ /* '$nof_threads'(+P) */
int i = 0, wid;
LOCK(GLOBAL_ThreadHandlesLock);
for (wid = 0; wid < MAX_THREADS; wid++) {
@ -1376,70 +1377,70 @@ p_new_mutex( USES_REGS1 )
}
UNLOCK(GLOBAL_ThreadHandlesLock);
return Yap_unify(ARG1,MkIntegerTerm(i));
}
}
static Int
p_max_workers( USES_REGS1 )
{ /* '$max_workers'(+P) */
static Int
p_max_workers( USES_REGS1 )
{ /* '$max_workers'(+P) */
return Yap_unify(ARG1,MkIntegerTerm(MAX_WORKERS));
}
}
static Int
p_max_threads( USES_REGS1 )
{ /* '$max_threads'(+P) */
static Int
p_max_threads( USES_REGS1 )
{ /* '$max_threads'(+P) */
return Yap_unify(ARG1,MkIntegerTerm(MAX_THREADS));
}
}
static Int
p_nof_threads_created( USES_REGS1 )
{ /* '$nof_threads'(+P) */
static Int
p_nof_threads_created( USES_REGS1 )
{ /* '$nof_threads'(+P) */
return Yap_unify(ARG1,MkIntTerm(GLOBAL_NOfThreadsCreated));
}
}
static Int
p_thread_runtime( USES_REGS1 )
{ /* '$thread_runtime'(+P) */
static Int
p_thread_runtime( USES_REGS1 )
{ /* '$thread_runtime'(+P) */
return Yap_unify(ARG1,MkIntegerTerm(GLOBAL_ThreadsTotalTime));
}
}
static Int
p_thread_self_lock( USES_REGS1 )
{ /* '$thread_unlock' */
static Int
p_thread_self_lock( USES_REGS1 )
{ /* '$thread_unlock' */
MUTEX_LOCK(&(LOCAL_ThreadHandle.tlock));
return Yap_unify(ARG1,MkIntegerTerm(worker_id));
}
}
static Int
p_thread_unlock( USES_REGS1 )
{ /* '$thread_unlock' */
static Int
p_thread_unlock( USES_REGS1 )
{ /* '$thread_unlock' */
Int wid = IntegerOfTerm(Deref(ARG1));
MUTEX_UNLOCK(&(REMOTE_ThreadHandle(wid).tlock));
return TRUE;
}
}
intptr_t
system_thread_id(PL_thread_info_t *info)
{ if ( !info )
intptr_t
system_thread_id(PL_thread_info_t *info)
{ if ( !info )
{ CACHE_REGS
if ( LOCAL )
info = SWI_thread_info(worker_id, NULL);
else
return -1;
}
#ifdef __linux__
#ifdef __linux__
return info->pid;
#else
#ifdef __WINDOWS__
#else
#ifdef __WINDOWS__
return info->w32id;
#else
#else
return (intptr_t)info->tid;
#endif
#endif
}
#endif
#endif
}
void
Yap_InitFirstWorkerThreadHandle(void)
{
void
Yap_InitFirstWorkerThreadHandle(void)
{
CACHE_REGS
set_system_thread_id(0, NULL);
LOCAL_ThreadHandle.id = 0;
@ -1455,12 +1456,12 @@ p_new_mutex( USES_REGS1 )
pthread_mutex_init(&REMOTE_ThreadHandle(0).tlock_status, NULL);
LOCAL_ThreadHandle.tdetach = MkAtomTerm(AtomFalse);
LOCAL_ThreadHandle.ref_count = 1;
}
}
FILE *debugf;
FILE *debugf;
void Yap_InitThreadPreds(void)
{
void Yap_InitThreadPreds(void)
{
Yap_InitCPred("$no_threads", 0, p_no_threads, 0);
@ -1530,89 +1531,89 @@ p_new_mutex( USES_REGS1 )
Yap_InitCPred("$thread_self_lock", 1, p_thread_self_lock, SafePredFlag);
Yap_InitCPred("$thread_run_at_exit", 2, p_thread_atexit, SafePredFlag);
Yap_InitCPred("$thread_unlock", 1, p_thread_unlock, SafePredFlag);
#if DEBUG_LOCKS||DEBUG_PE_LOCKS
#if DEBUG_LOCKS||DEBUG_PE_LOCKS
Yap_InitCPred("debug_locks", 0, p_debug_locks, SafePredFlag);
Yap_InitCPred("nodebug_locks", 0, p_nodebug_locks, SafePredFlag);
#endif
}
#endif
}
#else
#else
int
Yap_NOfThreads(void) {
int
Yap_NOfThreads(void) {
// GLOBAL_ThreadHandlesLock is held
#ifdef YAPOR
#ifdef YAPOR
return 2;
#else
#else
return 1;
#endif
}
#endif
}
static Int
p_no_threads(void)
{ /* '$thread_signal'(+P) */
static Int
p_no_threads(void)
{ /* '$thread_signal'(+P) */
return TRUE;
}
}
static Int
p_nof_threads(void)
{ /* '$nof_threads'(+P) */
static Int
p_nof_threads(void)
{ /* '$nof_threads'(+P) */
return Yap_unify(ARG1,MkIntTerm(1));
}
}
static Int
p_max_threads(void)
{ /* '$nof_threads'(+P) */
static Int
p_max_threads(void)
{ /* '$nof_threads'(+P) */
return Yap_unify(ARG1,MkIntTerm(1));
}
}
static Int
p_nof_threads_created(void)
{ /* '$nof_threads'(+P) */
static Int
p_nof_threads_created(void)
{ /* '$nof_threads'(+P) */
return Yap_unify(ARG1,MkIntTerm(1));
}
}
static Int
p_thread_runtime(void)
{ /* '$thread_runtime'(+P) */
static Int
p_thread_runtime(void)
{ /* '$thread_runtime'(+P) */
return Yap_unify(ARG1,MkIntTerm(0));
}
}
static Int
p_thread_self(void)
{ /* '$thread_runtime'(+P) */
static Int
p_thread_self(void)
{ /* '$thread_runtime'(+P) */
return Yap_unify(ARG1,MkIntTerm(0));
}
}
static Int
p_thread_stacks(void)
{ /* '$thread_runtime'(+P) */
static Int
p_thread_stacks(void)
{ /* '$thread_runtime'(+P) */
return FALSE;
}
}
static Int
p_thread_unlock(void)
{ /* '$thread_runtime'(+P) */
static Int
p_thread_unlock(void)
{ /* '$thread_runtime'(+P) */
return TRUE;
}
}
static Int
p_max_workers(void)
{ /* '$max_workers'(+P) */
static Int
p_max_workers(void)
{ /* '$max_workers'(+P) */
return Yap_unify(ARG1,MkIntTerm(1));
}
}
static Int
p_new_mutex(void)
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 )
{
static Int
p_with_mutex( USES_REGS1 )
{
Int mut;
Term t1 = Deref(ARG1), excep;
Int rc = FALSE;
@ -1679,7 +1680,7 @@ p_new_mutex( USES_REGS1 )
return Yap_JumpToEnv(excep);
}
return rc;
}
}
void
Yap_InitFirstWorkerThreadHandle(void)
@ -1710,5 +1711,5 @@ void Yap_InitThreadPreds(void)
/**
@}
@}
*/

View File

@ -1242,7 +1242,7 @@ RepTranslationProp (Prop p)
INLINE_ONLY inline EXTERN Prop AbsTranslationProp (TranslationEntry * p);
INLINE_ONLY inline EXTERN Prop
INLINE_ONLY inline EXTERN Prop
AbsTranslationProp (TranslationEntry * p)
{
return (Prop) (p);
@ -1252,7 +1252,7 @@ AbsTranslationProp (TranslationEntry * p)
#endif
#define TranslationProperty 0xfff4
void Yap_PutAtomTranslation(Atom a, Int i);
bool Yap_PutAtomTranslation(Atom a, Int i);
/* get translation prop for atom; */
static inline TranslationEntry *
@ -1271,9 +1271,6 @@ Yap_GetTranslationProp(Atom at)
return p;
}
/* only unary and binary expressions are acceptable */
INLINE_ONLY inline EXTERN PropFlags IsTranslationProperty (int);
INLINE_ONLY inline EXTERN PropFlags
@ -1282,6 +1279,89 @@ IsTranslationProperty (int flags)
return (PropFlags) ((flags == TranslationProperty));
}
/*** handle named mutexes */
/* translationnamed mutex property entry structure */
typedef struct mutex_entry
{
Prop NextOfPE; /* used to chain properties */
PropFlags KindOfPE; /* kind of property */
void *Mutex; /* used to hash the atom as an integer; */
} MutexEntry;
#if USE_OFFSETS_IN_PROPS
INLINE_ONLY inline EXTERN MutexEntry *RepMutexProp (Prop p);
INLINE_ONLY inline EXTERN MutexEntry *
RepMutexProp (Prop p)
{
return (MutexEntry *) (AtomBase + Unsigned (p));
}
INLINE_ONLY inline EXTERN Prop AbsMutexProp (MutexEntry * p);
INLINE_ONLY inline EXTERN Prop
AbsMutexProp (MutexEntry * p)
{
return (Prop) (Addr (p) - AtomBase);
}
#else
INLINE_ONLY inline EXTERN MutexEntry *RepMutexProp (Prop p);
INLINE_ONLY inline EXTERN MutexEntry *
RepMutexProp (Prop p)
{
return (MutexEntry *) (p);
}
INLINE_ONLY inline EXTERN Prop AbsMutexProp (MutexEntry * p);
INLINE_ONLY inline EXTERN Prop
AbsMutexProp (MutexEntry * p)
{
return (Prop) (p);
}
#endif
#define MutexProperty 0xfff5
bool Yap_PutAtomMutex(Atom a, void *ptr);
/* get mutex prop for atom; */
static inline MutexEntry *
Yap_GetMutexProp(Atom at)
{
Prop p0;
AtomEntry *ae = RepAtom(at);
MutexEntry *p;
READ_LOCK(ae->ARWLock);
p = RepMutexProp(p0 = ae->PropsOfAE);
while (p0 && p->KindOfPE != MutexProperty)
p = RepMutexProp(p0 = p->NextOfPE);
READ_UNLOCK(ae->ARWLock);
if (p0 == NIL) return NULL;
return p;
}
INLINE_ONLY inline EXTERN PropFlags IsMutexProperty (int);
INLINE_ONLY inline EXTERN PropFlags
IsMutexProperty (int flags)
{
return (PropFlags) ((flags == MutexProperty));
}
/* end of code for named mutexes */
typedef enum {
STATIC_ARRAY = 1,