This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/OPTYap/locks_pthread.h

127 lines
4.7 KiB
C
Raw Normal View History

/************************************************************************
** **
** The YapTab/YapOr/OPTYap systems **
** **
** YapTab extends the Yap Prolog engine to support sequential tabling **
** YapOr extends the Yap Prolog engine to support or-parallelism **
** OPTYap extends the Yap Prolog engine to support or-parallel tabling **
** **
** **
** Yap Prolog was developed at University of Porto, Portugal **
** **
************************************************************************/
/* **********************************************************************
** Atomic locks for PTHREADS **
************************************************************************/
2015-06-19 01:30:13 +01:00
#ifndef LOCK_PTHREAD_H0
2014-06-16 16:46:05 +01:00
#define LOCK_PTHREAD_H 1
#include <pthread.h>
//#define DEBUG_PE_LOCKS 1
2015-06-19 10:10:02 +01:00
//#define DEBUG_LOCKS 1
#include <stdio.h>
2014-03-06 02:09:48 +00:00
int Yap_ThreadID( void );
2015-06-19 10:10:02 +01:00
#define debugf ( stderr ? stderr : stdout )
2014-03-06 02:09:48 +00:00
2015-06-19 10:10:02 +01:00
#define INIT_LOCK(LOCK_VAR) pthread_mutex_init(&(LOCK_VAR), NULL)
#define DESTROY_LOCK(LOCK_VAR) pthread_mutex_destroy(&(LOCK_VAR))
2012-05-21 15:55:14 +01:00
#define TRY_LOCK(LOCK_VAR) pthread_mutex_trylock(&(LOCK_VAR))
2013-11-12 08:34:26 +00:00
#if DEBUG_LOCKS
2014-10-19 01:44:48 +01:00
extern int debug_locks;
2015-06-19 01:30:13 +01:00
#define LOCK(LOCK_VAR) (void)(fprintf(debugf, "[%d] %s:%d: LOCK(%p)\n", Yap_ThreadID(),__BASE_FILE__, __LINE__,&(LOCK_VAR)) && pthread_mutex_lock(&(LOCK_VAR)) )
#define UNLOCK(LOCK_VAR) (void)(fprintf(debugf, "[%d] %s:%d: UNLOCK(%p)\n", Yap_ThreadID(),__BASE_FILE__, __LINE__,&(LOCK_VAR)) && pthread_mutex_unlock(&(LOCK_VAR)) )
2013-11-12 08:34:26 +00:00
#else
#define LOCK(LOCK_VAR) pthread_mutex_lock(&(LOCK_VAR))
#define UNLOCK(LOCK_VAR) pthread_mutex_unlock(&(LOCK_VAR))
2013-11-12 08:34:26 +00:00
#endif
2014-10-19 01:44:48 +01:00
2010-01-15 12:04:01 +00:00
static inline int
xIS_LOCKED(pthread_mutex_t *LOCK_VAR) {
if (pthread_mutex_trylock(LOCK_VAR) == 0) {
pthread_mutex_unlock(LOCK_VAR);
return TRUE;
}
return FALSE;
}
static inline int
xIS_UNLOCKED(pthread_mutex_t *LOCK_VAR) {
if (pthread_mutex_trylock(LOCK_VAR) == 0) {
pthread_mutex_unlock(LOCK_VAR);
return FALSE;
}
return TRUE;
}
2014-10-19 01:44:48 +01:00
2010-01-15 12:04:01 +00:00
#define IS_LOCKED(LOCK_VAR) xIS_LOCKED(&(LOCK_VAR))
#define IS_UNLOCKED(LOCK_VAR) xIS_UNLOCKED(&(LOCK_VAR))
2014-10-19 01:44:48 +01:00
#define INIT_RWLOCK(X) pthread_rwlock_init(&(X), NULL)
#define DESTROY_RWLOCK(X) pthread_rwlock_destroy(&(X))
2014-10-19 01:44:48 +01:00
#if DEBUG_PE_LOCKS
extern int debug_pe_locks;
#define READ_LOCK(X) ((debug_pe_locks ? \
fprintf(debugf, "[%d] %s:%d: RLOCK(%p)\n", \
Yap_ThreadID(),__BASE_FILE__, __LINE__,&(X)) \
: 1) && pthread_rwlock_rdlock(&(X)) )
#define READ_UNLOCK(X) ((debug_pe_locks ? \
fprintf(debugf, "[%d] %s:%d: UNLOCK(%p)\n", \
Yap_ThreadID(),__BASE_FILE__, __LINE__,&(X)) \
: 1) && pthread_rwlock_unlock(&(X)) )
#define WRITE_LOCK(X) ((debug_pe_locks ? \
fprintf(debugf, "[%d] %s:%d: RLOCK(%p)\n", \
Yap_ThreadID(),__BASE_FILE__, __LINE__,&(X)) \
: 1) && pthread_rwlock_rdlock(&(X)) )
#define WRITE_UNLOCK(X) ((debug_pe_locks ? \
fprintf(debugf, "[%d] %s:%d: UNLOCK(%p)\n", \
Yap_ThreadID(),__BASE_FILE__, __LINE__,&(X))\
: 1) && pthread_rwlock_unlock(&(X)) )
#else
#define READ_LOCK(X) pthread_rwlock_rdlock(&(X))
#define READ_UNLOCK(X) pthread_rwlock_unlock(&(X))
#define WRITE_LOCK(X) pthread_rwlock_wrlock(&(X))
#define WRITE_UNLOCK(X) pthread_rwlock_unlock(&(X))
2014-10-19 01:44:48 +01:00
#endif
2013-11-12 08:34:26 +00:00
2014-06-11 19:44:01 +01:00
#define TRUE_FUNC_WRITE_LOCK(F) WRITE_LOCK((F)->FRWLock)
#define TRUE_FUNC_WRITE_UNLOCK(F) WRITE_UNLOCK((F)->FRWLock)
2013-11-12 08:34:26 +00:00
#if THREADS
/* pthread mutex */
#if DEBUG_LOCKS
2014-10-19 01:44:48 +01:00
#define MUTEX_LOCK(LOCK_VAR) (void)( ( debug_locks ? fprintf(stderr,"[%d] %s:%d: MULOCK(%p)\n", Yap_ThreadID(), \
__BASE_FILE__, __LINE__,(LOCK_VAR)) : 1 ) && \
2014-03-06 02:09:48 +00:00
pthread_mutex_lock((LOCK_VAR)) )
#define MUTEX_TRYLOCK(LOCK_VAR) pthread_mutex_trylock((LOCK_VAR))
2014-10-19 01:44:48 +01:00
#define MUTEX_UNLOCK(LOCK_VAR) (void)((debug_locks? fprintf(stderr,"[%d] %s:%d: UNMULOCK(%p)\n", Yap_ThreadID(), \
__BASE_FILE__, __LINE__,(LOCK_VAR)) : 1) && \
2014-03-06 02:09:48 +00:00
pthread_mutex_unlock((LOCK_VAR)))
2013-11-12 08:34:26 +00:00
#else
#define MUTEX_LOCK(LOCK_VAR) pthread_mutex_lock((LOCK_VAR))
#define MUTEX_TRYLOCK(LOCK_VAR) pthread_mutex_trylock((LOCK_VAR))
#define MUTEX_UNLOCK(LOCK_VAR) pthread_mutex_unlock((LOCK_VAR))
#endif
#else
#define MUTEX_LOCK(LOCK_VAR)
#define MUTEX_TRYLOCK(LOCK_VAR)
#define MUTEX_UNLOCK(LOCK_VAR)
#endif
2014-06-16 16:46:05 +01:00
#endif // LOCK_PTHREAD_H