YAP 7.1.0
locks_pthread.h
1/************************************************************************
2** **
3** The YapTab/YapOr/OPTYap systems **
4** **
5** YapTab extends the Yap Prolog engine to support sequential tabling **
6** YapOr extends the Yap Prolog engine to support or-parallelism **
7** OPTYap extends the Yap Prolog engine to support or-parallel tabling **
8** **
9** **
10** Yap Prolog was developed at University of Porto, Portugal **
11** **
12************************************************************************/
13
14/* **********************************************************************
15** Atomic locks for PTHREADS **
16************************************************************************/
17
18#ifndef LOCK_PTHREAD_H0
19
20#define LOCK_PTHREAD_H 1
21
22#include <pthread.h>
23
24//#define DEBUG_PE_LOCKS 1
25//#define DEBUG_LOCKS 1
26#include <stdio.h>
27
28int Yap_ThreadID( void );
29#define debugf ( stderr ? stderr : stdout )
30
31#define INIT_LOCK(LOCK_VAR) pthread_mutex_init(&(LOCK_VAR), NULL)
32#define DESTROY_LOCK(LOCK_VAR) pthread_mutex_destroy(&(LOCK_VAR))
33#define TRY_LOCK(LOCK_VAR) pthread_mutex_trylock(&(LOCK_VAR))
34#if DEBUG_LOCKS
35extern bool debug_locks;
36
37#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)) )
38#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)) )
39#else
40#define LOCK(LOCK_VAR) pthread_mutex_lock(&(LOCK_VAR))
41#define UNLOCK(LOCK_VAR) pthread_mutex_unlock(&(LOCK_VAR))
42#endif
43
44static inline bool
45xIS_LOCKED(pthread_mutex_t *LOCK_VAR) {
46 if (pthread_mutex_trylock(LOCK_VAR) == 0) {
47 pthread_mutex_unlock(LOCK_VAR);
48 return true;
49 }
50 return false;
51}
52static inline bool
53xIS_UNLOCKED(pthread_mutex_t *LOCK_VAR) {
54 if (pthread_mutex_trylock(LOCK_VAR) == 0) {
55 pthread_mutex_unlock(LOCK_VAR);
56 return false;
57 }
58 return true;
59}
60
61#define IS_LOCKED(LOCK_VAR) xIS_LOCKED(&(LOCK_VAR))
62#define IS_UNLOCKED(LOCK_VAR) xIS_UNLOCKED(&(LOCK_VAR))
63
64
65#define INIT_RWLOCK(X) pthread_rwlock_init(&(X), NULL)
66#define DESTROY_RWLOCK(X) pthread_rwlock_destroy(&(X))
67#if DEBUG_PE_LOCKS
68extern bool debug_pe_locks;
69
70#define READ_LOCK(X) ((debug_pe_locks ? \
71 fprintf(debugf, "[%d] %s:%d: RLOCK(%p)\n", \
72 Yap_ThreadID(),__BASE_FILE__, __LINE__,&(X)) \
73 : 1) && pthread_rwlock_rdlock(&(X)) )
74#define READ_UNLOCK(X) ((debug_pe_locks ? \
75 fprintf(debugf, "[%d] %s:%d: UNLOCK(%p)\n", \
76 Yap_ThreadID(),__BASE_FILE__, __LINE__,&(X)) \
77 : 1) && pthread_rwlock_unlock(&(X)) )
78#define WRITE_LOCK(X) ((debug_pe_locks ? \
79 fprintf(debugf, "[%d] %s:%d: RLOCK(%p)\n", \
80 Yap_ThreadID(),__BASE_FILE__, __LINE__,&(X)) \
81 : 1) && pthread_rwlock_rdlock(&(X)) )
82#define WRITE_UNLOCK(X) ((debug_pe_locks ? \
83 fprintf(debugf, "[%d] %s:%d: UNLOCK(%p)\n", \
84 Yap_ThreadID(),__BASE_FILE__, __LINE__,&(X))\
85 : 1) && pthread_rwlock_unlock(&(X)) )
86
87#else
88#define READ_LOCK(X) pthread_rwlock_rdlock(&(X))
89#define READ_UNLOCK(X) pthread_rwlock_unlock(&(X))
90#define WRITE_LOCK(X) pthread_rwlock_wrlock(&(X))
91#define WRITE_UNLOCK(X) pthread_rwlock_unlock(&(X))
92#endif
93
94
95#define TRUE_FUNC_WRITE_LOCK(F) WRITE_LOCK((F)->FRWLock)
96#define TRUE_FUNC_WRITE_UNLOCK(F) WRITE_UNLOCK((F)->FRWLock)
97
98#if THREADS
99
100/* pthread mutex */
101
102#if DEBUG_LOCKS
103
104#define MUTEX_LOCK(LOCK_VAR) (void)( ( debug_locks ? fprintf(stderr,"[%d] %s:%d: MULOCK(%p)\n", Yap_ThreadID(), \
105 __BASE_FILE__, __LINE__,(LOCK_VAR)) : 1 ) && \
106 pthread_mutex_lock((LOCK_VAR)) )
107#define MUTEX_TRYLOCK(LOCK_VAR) pthread_mutex_trylock((LOCK_VAR))
108#define MUTEX_UNLOCK(LOCK_VAR) (void)((debug_locks? fprintf(stderr,"[%d] %s:%d: UNMULOCK(%p)\n", Yap_ThreadID(), \
109 __BASE_FILE__, __LINE__,(LOCK_VAR)) : 1) && \
110 pthread_mutex_unlock((LOCK_VAR)))
111#else
112#define MUTEX_LOCK(LOCK_VAR) pthread_mutex_lock((LOCK_VAR))
113#define MUTEX_TRYLOCK(LOCK_VAR) pthread_mutex_trylock((LOCK_VAR))
114#define MUTEX_UNLOCK(LOCK_VAR) pthread_mutex_unlock((LOCK_VAR))
115#endif
116
117#else
118
119#define MUTEX_LOCK(LOCK_VAR)
120#define MUTEX_TRYLOCK(LOCK_VAR)
121#define MUTEX_UNLOCK(LOCK_VAR)
122
123
124#endif
125
126#endif // LOCK_PTHREAD_H