You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
2.4 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #ifndef __BASICMUTEX_H_INCL__
  2. #define __BASICMUTEX_H_INCL__
  3. #ifndef __linux__
  4. #ifndef WIN_PTHREAD
  5. // #define WIN_PTHREAD
  6. #endif
  7. #endif
  8. #ifdef WIN_PTHREAD
  9. #pragma comment(lib, "pthreadVC2.lib")
  10. #endif
  11. #ifndef MUTEX_HANDLE_TYPE
  12. #if defined(__linux__) || defined(WIN_PTHREAD)
  13. // Linux impl.
  14. #include <pthread.h>
  15. #define MUTEX_HANDLE_TYPE pthread_mutex_t
  16. #define MUTEX_INIT(X) pthread_mutex_init(&X, NULL)
  17. #define MUTEX_LOCK(X) pthread_mutex_lock(&X)
  18. #define MUTEX_TRY_LOCK(X) pthread_mutex_trylock(&X)
  19. #define MUTEX_UNLOCK(X) pthread_mutex_unlock(&X)
  20. #define MUTEX_DESTROY(X) pthread_mutex_destroy(&X)
  21. #else
  22. // Windows impl.
  23. #include "Windows.h"
  24. #define MUTEX_HANDLE_TYPE HANDLE
  25. #define MUTEX_INIT(X) X = CreateMutex(NULL, FALSE, NULL)
  26. #define MUTEX_LOCK(X) WaitForSingleObject(X, INFINITE)
  27. #define MUTEX_TRY_LOCK(X) (WaitForSingleObject(X, 0) == WAIT_OBJECT_0)
  28. #define MUTEX_UNLOCK(X) ReleaseMutex(X)
  29. #define MUTEX_DESTROY(X) CloseHandle(X)
  30. #endif
  31. #endif
  32. /**
  33. * Mutex wraper
  34. *
  35. * @author Gagua
  36. */
  37. class basicMutex
  38. {
  39. private:
  40. MUTEX_HANDLE_TYPE m_sMutexHandle;
  41. public:
  42. // Constructor
  43. basicMutex();
  44. // Destructor
  45. virtual ~basicMutex();
  46. void lock(void);
  47. bool try_lock(void);
  48. void unlock(void);
  49. // Copy constructor
  50. // TODO: Uncomment the copy constructor when you need it.
  51. // basicMutex(const basicMutex& src);
  52. // Assignment operator
  53. // TODO: Uncomment the assignment operator when you need it.
  54. // basicMutex& operator=(const basicMutex& src);
  55. };
  56. // Constructor implementation
  57. inline basicMutex::basicMutex()
  58. {
  59. MUTEX_INIT(m_sMutexHandle);
  60. }
  61. // Destructor implementation
  62. inline basicMutex::~basicMutex()
  63. {
  64. MUTEX_UNLOCK(m_sMutexHandle);
  65. MUTEX_DESTROY(m_sMutexHandle);
  66. }
  67. inline void basicMutex::lock(void)
  68. {
  69. MUTEX_LOCK(m_sMutexHandle);
  70. }
  71. inline bool basicMutex::try_lock(void)
  72. {
  73. return (MUTEX_TRY_LOCK(m_sMutexHandle) != 0);
  74. }
  75. inline void basicMutex::unlock(void)
  76. {
  77. MUTEX_UNLOCK(m_sMutexHandle);
  78. }
  79. // TODO: Uncomment the copy constructor when you need it.
  80. // inline basicMutex::basicMutex(const basicMutex& src)
  81. //{
  82. // // TODO: copy
  83. //}
  84. // TODO: Uncomment the assignment operator when you need it.
  85. // inline basicMutex& basicMutex::operator=(const basicMutex& rhs)
  86. //{
  87. // if (this == &rhs) {
  88. // return *this;
  89. // }
  90. //
  91. // // TODO: assignment
  92. //
  93. // return *this;
  94. //}
  95. #endif // __BASICMUTEX_H_INCL__