mutex.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright 2010-2013 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #ifndef BX_MUTEX_H_HEADER_GUARD
  6. #define BX_MUTEX_H_HEADER_GUARD
  7. #include "bx.h"
  8. #include "cpu.h"
  9. #include "sem.h"
  10. #if BX_CONFIG_SUPPORTS_THREADING
  11. #if BX_PLATFORM_NACL || BX_PLATFORM_LINUX || BX_PLATFORM_ANDROID || BX_PLATFORM_OSX
  12. # include <pthread.h>
  13. #elif BX_PLATFORM_WINDOWS || BX_PLATFORM_XBOX360
  14. # include <errno.h>
  15. #endif // BX_PLATFORM_
  16. namespace bx
  17. {
  18. #if BX_PLATFORM_WINDOWS || BX_PLATFORM_XBOX360
  19. typedef CRITICAL_SECTION pthread_mutex_t;
  20. typedef unsigned pthread_mutexattr_t;
  21. inline int pthread_mutex_lock(pthread_mutex_t* _mutex)
  22. {
  23. EnterCriticalSection(_mutex);
  24. return 0;
  25. }
  26. inline int pthread_mutex_unlock(pthread_mutex_t* _mutex)
  27. {
  28. LeaveCriticalSection(_mutex);
  29. return 0;
  30. }
  31. inline int pthread_mutex_trylock(pthread_mutex_t* _mutex)
  32. {
  33. return TryEnterCriticalSection(_mutex) ? 0 : EBUSY;
  34. }
  35. inline int pthread_mutex_init(pthread_mutex_t* _mutex, pthread_mutexattr_t* /*_attr*/)
  36. {
  37. InitializeCriticalSection(_mutex);
  38. return 0;
  39. }
  40. inline int pthread_mutex_destroy(pthread_mutex_t* _mutex)
  41. {
  42. DeleteCriticalSection(_mutex);
  43. return 0;
  44. }
  45. #endif // BX_PLATFORM_
  46. class Mutex
  47. {
  48. public:
  49. Mutex()
  50. {
  51. pthread_mutex_init(&m_handle, NULL);
  52. }
  53. ~Mutex()
  54. {
  55. pthread_mutex_destroy(&m_handle);
  56. }
  57. void lock()
  58. {
  59. pthread_mutex_lock(&m_handle);
  60. }
  61. void unlock()
  62. {
  63. pthread_mutex_unlock(&m_handle);
  64. }
  65. private:
  66. Mutex(const Mutex& _rhs); // no copy constructor
  67. Mutex& operator=(const Mutex& _rhs); // no assignment operator
  68. pthread_mutex_t m_handle;
  69. };
  70. class MutexScope
  71. {
  72. public:
  73. MutexScope(Mutex& _mutex)
  74. : m_mutex(_mutex)
  75. {
  76. m_mutex.lock();
  77. }
  78. ~MutexScope()
  79. {
  80. m_mutex.unlock();
  81. }
  82. private:
  83. MutexScope(); // no default constructor
  84. MutexScope(const MutexScope& _rhs); // no copy constructor
  85. MutexScope& operator=(const MutexScope& _rhs); // no assignment operator
  86. Mutex& m_mutex;
  87. };
  88. typedef Mutex LwMutex;
  89. class LwMutexScope
  90. {
  91. public:
  92. LwMutexScope(LwMutex& _mutex)
  93. : m_mutex(_mutex)
  94. {
  95. m_mutex.lock();
  96. }
  97. ~LwMutexScope()
  98. {
  99. m_mutex.unlock();
  100. }
  101. private:
  102. LwMutexScope(); // no default constructor
  103. LwMutexScope(const LwMutexScope& _rhs); // no copy constructor
  104. LwMutexScope& operator=(const LwMutexScope& _rhs); // no assignment operator
  105. LwMutex& m_mutex;
  106. };
  107. } // namespace bx
  108. #endif // BX_CONFIG_SUPPORTS_THREADING
  109. #endif // BX_MUTEX_H_HEADER_GUARD