00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef _EYEDBSM_MUTEX_H
00026 #define _EYEDBSM_MUTEX_H
00027
00028 #include "xm_alloc.h"
00029 #include <eyedblib/machtypes.h>
00030 #ifdef HAVE_SEMAPHORE_POLICY_SYSV_IPC
00031 #include <eyedblib/semlib.h>
00032 #endif
00033 #include <pthread.h>
00034
00035 namespace eyedbsm {
00036 #define MAXCLS 0
00037
00038 struct CondWaitP {
00039 eyedblib::uint32 magic;
00040 union {
00041 int key;
00042 pthread_cond_t cond;
00043 } u;
00044 };
00045
00046 struct CondWait {
00047 CondWaitP *pcond;
00048 int id;
00049 };
00050
00051 struct MutexP {
00052 eyedblib::uint32 magic;
00053 union {
00054 int key;
00055 pthread_mutex_t mp;
00056 } u;
00057 char mtname[16];
00058 unsigned int xid;
00059 int locked;
00060 int wait_cnt;
00061 CondWaitP pcond;
00062 };
00063
00064 struct Mutex {
00065 MutexP *pmp;
00066 CondWait cond;
00067 #ifdef HAVE_SEMAPHORE_POLICY_SYSV_IPC
00068 int id;
00069 int *plocked;
00070 #endif
00071 };
00072
00073 struct CondWaitLink;
00074
00075 struct CondWaitList {
00076 int link_cnt;
00077 CondWaitLink *first;
00078 CondWaitLink *last;
00079 };
00080
00081 #define MUTEX_LOCK_TRACE(MP, XID)\
00082 utlog("XLOCK %s, %d\n", __FILE__, __LINE__); \
00083 mutexLock(MP, XID)
00084
00085 #define MUTEX_UNLOCK_TRACE(MP, XID) \
00086 utlog("UNLOCK %s, %d\n", __FILE__, __LINE__); \
00087 mutexUnlock(MP, XID)
00088
00089 #define MUTEX_LOCK(MP, XID) \
00090 do { \
00091 Status se = mutexLock(MP, XID); \
00092 if (se) return se; \
00093 } while(0)
00094
00095 #define MUTEX_UNLOCK(MP, XID) mutexUnlock(MP, XID)
00096 #define MUTEX_LOCK_VOID(MP, XID) mutexLock(MP, XID)
00097
00098 #define COND_WAIT(C, MP, XID, TIMEOUT) condWait(C, MP, XID, TIMEOUT)
00099 #define COND_WAIT_R(C, MP, XID, TIMEOUT) condWait_r(C, MP, XID, TIMEOUT)
00100 #define COND_SIGNAL(C) condSignal(C)
00101
00102 struct DbDescription;
00103
00104 extern void mutexLightInit(DbDescription *vd, Mutex *mp, MutexP *pmp);
00105
00106
00107 extern int mutexInit(DbDescription *vd, Mutex *, MutexP *, const char *),
00108
00109 mutexCheckNotLock(Mutex *_mp, unsigned int xid);
00110
00111 extern Status mutexLock(Mutex *, unsigned int),
00112 mutexUnlock(Mutex *, unsigned int);
00113
00114 extern int condInit(DbDescription *, CondWait *, CondWaitP *),
00115
00116 condSignal(CondWait *),
00117 condWait(CondWait *, Mutex *, unsigned int, unsigned int timeout),
00118 condWait_r(CondWait *, Mutex *, unsigned int, unsigned int timeout);
00119
00120 extern void condLightInit(DbDescription *, CondWait *, CondWaitP *);
00121
00122
00123 extern XMOffset
00124 condNew(DbDescription *, XMHandle *, CondWait *);
00125
00126 extern void
00127 condDelete(DbDescription *, XMHandle *, XMOffset);
00128
00129 extern CondWait *
00130 condMake(DbDescription *, XMHandle *, XMOffset, CondWait *);
00131
00132 extern void
00133 mutexes_init(),
00134 mutexes_release();
00135
00136
00137 class MutexLocker {
00138
00139 public:
00140 MutexLocker(Mutex &_mut) : mut(&_mut) {
00141 #ifdef HAVE_SEMAPHORE_POLICY_SYSV_IPC
00142 MUTEX_LOCK_VOID(mut, mut->id);
00143 #else
00144 MUTEX_LOCK_VOID(mut, 0);
00145 #endif
00146 locked = true;
00147 }
00148 void lock() {
00149 #ifdef HAVE_SEMAPHORE_POLICY_SYSV_IPC
00150 MUTEX_LOCK_VOID(mut, mut->id);
00151 #else
00152 MUTEX_LOCK_VOID(mut, 0);
00153 #endif
00154 locked = true;
00155 }
00156
00157 void unlock() {
00158 locked = false;
00159 #ifdef HAVE_SEMAPHORE_POLICY_SYSV_IPC
00160 MUTEX_UNLOCK(mut, mut->id);
00161 #else
00162 MUTEX_UNLOCK(mut, 0);
00163 #endif
00164 }
00165
00166 ~MutexLocker() {
00167 if (locked) {
00168 #ifdef HAVE_SEMAPHORE_POLICY_SYSV_IPC
00169 MUTEX_UNLOCK(mut, mut->id);
00170 #else
00171 MUTEX_UNLOCK(mut, 0);
00172 #endif
00173 }
00174 }
00175
00176 private:
00177 Mutex *mut;
00178 bool locked;
00179 };
00180 }
00181
00182 #endif