sharelib/MutexLock.h

Go to the documentation of this file.
00001 #ifndef MUTEX_LOCK_H
00002 #define MUTEX_LOCK_H
00003 
00012 #if !defined _LINUX && !defined _WIN32
00013 #  error "Platform was not specified. Add macro _LINUX or _WIN32 to compiler options."
00014 #endif
00015 
00016 #include "sharelib.h"
00017 
00018 #ifdef _WIN32
00019 #    include <windows.h>
00020 #    include <limits.h>
00021 #endif // _WIN32
00022 
00023 #ifdef _LINUX
00024 #    include <unistd.h>
00025 #    include <sys/types.h>
00026 #    include <sys/time.h>
00027 #    include <sys/ipc.h>
00028 #    include <sys/sem.h>
00029 #    include <sys/stat.h>
00030 #    include <errno.h>
00031 #endif //_LINUX
00032 
00033 namespace Share {
00034 
00040   class Mutex {
00041     public:
00048       Mutex (RelocPtr<char> name) throw (ShareException);
00049 
00053       ~Mutex() throw ();
00054 
00060       struct LockData {
00061 #ifdef _WIN32
00062         HANDLE m_hSemaphore;
00063 #endif
00064       };
00065 
00072       void lock(LockData *data) throw (ShareException);
00073 
00079       void unlock(LockData *data) throw (ShareException);
00080 
00081     protected:
00082 #ifdef _WIN32
00083       RelocPtr<char> name_;
00084 #endif // _WIN32
00085 
00086 #ifdef _LINUX
00087       int m_iSemaphore;
00088 #endif // _LINUX
00089 
00090     private:
00091       Mutex (const Mutex&);                     
00092       void operator=(const Mutex&);             
00093   };
00094 
00100   class MutexLock {
00101     public:
00106       MutexLock (Mutex *mutex):
00107         mutex_ (mutex)
00108       {
00109         mutex_-> lock (&lockData);
00110       }
00111 
00115       ~MutexLock()
00116       {
00117         mutex_-> unlock (&lockData);
00118       }
00119 
00120     private:
00121       MutexLock (const MutexLock&);
00122       void operator= (const MutexLock&);
00123 
00124     private:
00125       Mutex *mutex_;
00126       Mutex::LockData lockData;
00127   };
00128 
00129 }
00130 
00131 #endif // MUTEX_LOCK_H
00132 

Generated on Sun Aug 26 17:42:59 2007 for ShareLibrary by  doxygen 1.5.2