mutex.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // mutex.h :
  2. //
  3. #if !defined(AGD_MUTEX_H__307A751D_EF2D_45D6_BBA8_2EB4B79B548D__INCLUDED_)
  4. #define AGD_MUTEX_H__307A751D_EF2D_45D6_BBA8_2EB4B79B548D__INCLUDED_
  5. #include <inttypes.h>
  6. #include <stdlib.h>
  7. #include <pthread.h>
  8. #include "uuid.h"
  9. /////////////////////////////////////////////////////////////////////////////
  10. // mutex.h - Declarations:
  11. #ifdef _WIN32
  12. #define _DEF_MUTEX_DIR "."
  13. #endif // _WIN32
  14. #ifdef __linux__
  15. #define _DEF_MUTEX_DIR "/tmp"
  16. #endif // __linux__
  17. /////////////////////////////////////////////////////////////////////////////
  18. class CGlobalMutex
  19. {
  20. public:
  21. CGlobalMutex(void);
  22. virtual ~CGlobalMutex(void);
  23. int Create(const uuid_t &ruuid, const char *pszDir = _DEF_MUTEX_DIR);
  24. long Release(void);
  25. bool Lock(void);
  26. bool TryLock(void);
  27. bool Unlock(void);
  28. private:
  29. int m_nShmID;
  30. pthread_mutexattr_t m_mutexAttr;
  31. pthread_mutex_t *m_pMutex;
  32. };
  33. /////////////////////////////////////////////////////////////////////////////
  34. class CLocalMutex
  35. {
  36. public:
  37. CLocalMutex(void);
  38. virtual ~CLocalMutex(void);
  39. bool Create(bool bRecursive = false);
  40. void Release(void);
  41. bool Lock(void);
  42. bool TryLock(void);
  43. bool Unlock(void);
  44. private:
  45. bool m_bInit;
  46. pthread_mutexattr_t m_mutexAttr;
  47. pthread_mutex_t m_mutex;
  48. };
  49. /////////////////////////////////////////////////////////////////////////////
  50. #endif // !defined(AGD_MUTEX_H__307A751D_EF2D_45D6_BBA8_2EB4B79B548D__INCLUDED_)