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.

106 lines
2.6 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. private:
  39. MUTEX_HANDLE_TYPE m_sMutexHandle;
  40. public:
  41. // Constructor
  42. basicMutex();
  43. // Destructor
  44. virtual ~basicMutex();
  45. void lock( void );
  46. bool try_lock( void );
  47. void unlock( void );
  48. // Copy constructor
  49. // TODO: Uncomment the copy constructor when you need it.
  50. //basicMutex(const basicMutex& src);
  51. // Assignment operator
  52. // TODO: Uncomment the assignment operator when you need it.
  53. //basicMutex& operator=(const basicMutex& src);
  54. };
  55. // Constructor implementation
  56. inline basicMutex::basicMutex(){
  57. MUTEX_INIT( m_sMutexHandle );
  58. };
  59. // Destructor implementation
  60. inline basicMutex::~basicMutex(){
  61. MUTEX_UNLOCK( m_sMutexHandle );
  62. MUTEX_DESTROY( m_sMutexHandle );
  63. };
  64. inline void basicMutex::lock( void ){
  65. MUTEX_LOCK( m_sMutexHandle );
  66. };
  67. inline bool basicMutex::try_lock( void ){
  68. return ( MUTEX_TRY_LOCK( m_sMutexHandle ) != 0 );
  69. };
  70. inline void basicMutex::unlock( void ){
  71. MUTEX_UNLOCK( m_sMutexHandle );
  72. };
  73. // TODO: Uncomment the copy constructor when you need it.
  74. //inline basicMutex::basicMutex(const basicMutex& src)
  75. //{
  76. // // TODO: copy
  77. //}
  78. // TODO: Uncomment the assignment operator when you need it.
  79. //inline basicMutex& basicMutex::operator=(const basicMutex& rhs)
  80. //{
  81. // if (this == &rhs) {
  82. // return *this;
  83. // }
  84. //
  85. // // TODO: assignment
  86. //
  87. // return *this;
  88. //}
  89. #endif // __BASICMUTEX_H_INCL__