mutex.h

00001 /* 
00002    EyeDB Object Database Management System
00003    Copyright (C) 1994-2008 SYSRA
00004    
00005    EyeDB is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009    
00010    EyeDB is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014    
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with this library; if not, write to the Free Software
00017    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA 
00018 */
00019 
00020 /*
00021    Author: Eric Viara <viara@sysra.com>
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; /* semaphore version */
00042       pthread_cond_t cond; /* mutex version */
00043     } u;
00044   };
00045 
00046   struct CondWait {
00047     CondWaitP *pcond;
00048     int id; /* semaphore version */
00049   };
00050 
00051   struct MutexP {
00052     eyedblib::uint32 magic;
00053     union {
00054       int key; /* semaphore version */
00055       pthread_mutex_t mp; /* mutex version */
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   //  mutexLightInit(int semkeys[], int *plocked, Mutex *mp, MutexP *pmp);
00106 
00107   extern int mutexInit(DbDescription *vd, Mutex *, MutexP *, const char *),
00108   //mutexInit(int semkeys[], int *plocked, Mutex *, MutexP *, const char *),
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     //condInit(int semkeys[], CondWait *, CondWaitP *),
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     //condLightInit(int semkeys[], CondWait *, CondWaitP *);
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

Generated on Mon Dec 22 18:15:58 2008 for eyedb by  doxygen 1.5.3