mutex.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "config.h"
  25. #include "types.h"
  26. #include "assert.h"
  27. #include "macros.h"
  28. #if CROWN_PLATFORM_POSIX
  29. #include <pthread.h>
  30. #elif CROWN_PLATFORM_WINDOWS
  31. #include "win_headers.h"
  32. #endif
  33. namespace crown
  34. {
  35. struct Mutex
  36. {
  37. Mutex()
  38. {
  39. #if CROWN_PLATFORM_POSIX
  40. int result = pthread_mutexattr_init(&m_attr);
  41. CE_ASSERT(result == 0, "pthread_mutexattr_init: errno = %d", result);
  42. result = pthread_mutexattr_settype(&m_attr, PTHREAD_MUTEX_ERRORCHECK);
  43. CE_ASSERT(result == 0, "pthread_mutexattr_settype: errno = %d", result);
  44. result = pthread_mutex_init(&m_mutex, &m_attr);
  45. CE_ASSERT(result == 0, "pthread_mutex_init: errno = %d", result);
  46. CE_UNUSED(result);
  47. #elif CROWN_PLATFORM_WINDOWS
  48. InitializeCriticalSection(&m_cs);
  49. #endif
  50. }
  51. ~Mutex()
  52. {
  53. #if CROWN_PLATFORM_POSIX
  54. int result = pthread_mutex_destroy(&m_mutex);
  55. CE_ASSERT(result == 0, "pthread_mutex_destroy: errno = %d", result);
  56. result = pthread_mutexattr_destroy(&m_attr);
  57. CE_ASSERT(result == 0, "pthread_mutexattr_destroy: errno = %d", result);
  58. CE_UNUSED(result);
  59. #elif CROWN_PLATFORM_WINDOWS
  60. DeleteCriticalSection(&m_cs);
  61. #endif
  62. }
  63. void lock()
  64. {
  65. #if CROWN_PLATFORM_POSIX
  66. int result = pthread_mutex_lock(&m_mutex);
  67. CE_ASSERT(result == 0, "pthread_mutex_lock: errno = %d", result);
  68. CE_UNUSED(result);
  69. #elif CROWN_PLATFORM_WINDOWS
  70. EnterCriticalSection(&m_cs);
  71. #endif
  72. }
  73. void unlock()
  74. {
  75. #if CROWN_PLATFORM_POSIX
  76. int result = pthread_mutex_unlock(&m_mutex);
  77. CE_ASSERT(result == 0, "pthread_mutex_unlock: errno = %d", result);
  78. CE_UNUSED(result);
  79. #elif CROWN_PLATFORM_WINDOWS
  80. LeaveCriticalSection(&m_cs);
  81. #endif
  82. }
  83. public:
  84. #if CROWN_PLATFORM_POSIX
  85. pthread_mutex_t m_mutex;
  86. pthread_mutexattr_t m_attr;
  87. #elif CROWN_PLATFORM_WINDOWS
  88. CRITICAL_SECTION m_cs;
  89. #endif
  90. private:
  91. // Disable copying.
  92. Mutex(const Mutex&);
  93. Mutex& operator=(const Mutex&);
  94. };
  95. /// Automatically locks a mutex when created and unlocks when destroyed.
  96. class ScopedMutex
  97. {
  98. public:
  99. /// Locks the given @a m mutex.
  100. ScopedMutex(Mutex& m)
  101. : m_mutex(m)
  102. {
  103. m_mutex.lock();
  104. }
  105. /// Unlocks the mutex passed to ScopedMutex::ScopedMutex()
  106. ~ScopedMutex()
  107. {
  108. m_mutex.unlock();
  109. }
  110. private:
  111. Mutex& m_mutex;
  112. private:
  113. // Disable copying
  114. ScopedMutex(const ScopedMutex&);
  115. ScopedMutex& operator=(const ScopedMutex&);
  116. };
  117. } // namespace crown