IBR-DTNSuite
0.8
|
00001 #ifndef IBRCOMMON_MUTEX_H_ 00002 #define IBRCOMMON_MUTEX_H_ 00003 00004 #include <pthread.h> 00005 #include "ibrcommon/Exceptions.h" 00006 00007 namespace ibrcommon 00008 { 00009 class MutexException : public ibrcommon::Exception 00010 { 00011 public: 00012 MutexException(string what = "An error occured during mutex operation.") throw() : ibrcommon::Exception(what) 00013 { 00014 }; 00015 }; 00016 00017 class MutexInterface 00018 { 00019 public: 00020 virtual ~MutexInterface() = 0; 00021 00022 virtual void trylock() throw (MutexException) = 0; 00023 virtual void enter() throw (MutexException) = 0; 00024 virtual void leave() throw (MutexException) = 0; 00025 }; 00026 00027 class MutexMock : public MutexInterface 00028 { 00029 public: 00030 MutexMock() {}; 00031 virtual ~MutexMock() {}; 00032 00033 virtual void trylock() throw (MutexException) {}; 00034 virtual void enter() throw (MutexException) {}; 00035 virtual void leave() throw (MutexException) {}; 00036 }; 00037 00038 class Mutex : public MutexInterface 00039 { 00040 public: 00041 enum MUTEX_TYPE 00042 { 00043 MUTEX_NORMAL = PTHREAD_MUTEX_NORMAL, 00044 MUTEX_RECURSIVE = PTHREAD_MUTEX_RECURSIVE, 00045 MUTEX_ERRORCHECK = PTHREAD_MUTEX_ERRORCHECK 00046 }; 00047 00048 Mutex(MUTEX_TYPE type = MUTEX_NORMAL); 00049 virtual ~Mutex(); 00050 00051 virtual void trylock() throw (MutexException); 00052 virtual void enter() throw (MutexException); 00053 virtual void leave() throw (MutexException); 00054 00055 static MutexInterface& dummy(); 00056 00057 protected: 00058 pthread_mutex_t m_mutex; 00059 pthread_mutexattr_t m_attr; 00060 00061 private: 00062 static MutexMock _dummy; 00063 }; 00064 } 00065 00066 #endif /*MUTEX_H_*/