mutex.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #ifndef WIN32_LEAN_AND_MEAN
  32. #define WIN32_LEAN_AND_MEAN 1
  33. #endif
  34. #include <windows.h>
  35. #endif
  36. namespace crown
  37. {
  38. struct Mutex
  39. {
  40. Mutex()
  41. {
  42. #if CROWN_PLATFORM_POSIX
  43. int result = pthread_mutexattr_init(&m_attr);
  44. CE_ASSERT(result == 0, "pthread_mutexattr_init: errno = %d", result);
  45. result = pthread_mutexattr_settype(&m_attr, PTHREAD_MUTEX_ERRORCHECK);
  46. CE_ASSERT(result == 0, "pthread_mutexattr_settype: errno = %d", result);
  47. result = pthread_mutex_init(&m_mutex, &m_attr);
  48. CE_ASSERT(result == 0, "pthread_mutex_init: errno = %d", result);
  49. CE_UNUSED(result);
  50. #elif CROWN_PLATFORM_WINDOWS
  51. InitializeCriticalSection(&m_cs);
  52. #endif
  53. }
  54. ~Mutex()
  55. {
  56. #if CROWN_PLATFORM_POSIX
  57. int result = pthread_mutex_destroy(&m_mutex);
  58. CE_ASSERT(result == 0, "pthread_mutex_destroy: errno = %d", result);
  59. result = pthread_mutexattr_destroy(&m_attr);
  60. CE_ASSERT(result == 0, "pthread_mutexattr_destroy: errno = %d", result);
  61. CE_UNUSED(result);
  62. #elif CROWN_PLATFORM_WINDOWS
  63. DeleteCriticalSection(&m_cs);
  64. #endif
  65. }
  66. void lock()
  67. {
  68. #if CROWN_PLATFORM_POSIX
  69. int result = pthread_mutex_lock(&m_mutex);
  70. CE_ASSERT(result == 0, "pthread_mutex_lock: errno = %d", result);
  71. CE_UNUSED(result);
  72. #elif CROWN_PLATFORM_WINDOWS
  73. EnterCriticalSection(&m_cs);
  74. #endif
  75. }
  76. void unlock()
  77. {
  78. #if CROWN_PLATFORM_POSIX
  79. int result = pthread_mutex_unlock(&m_mutex);
  80. CE_ASSERT(result == 0, "pthread_mutex_unlock: errno = %d", result);
  81. CE_UNUSED(result);
  82. #elif CROWN_PLATFORM_WINDOWS
  83. LeaveCriticalSection(&m_cs);
  84. #endif
  85. }
  86. public:
  87. #if CROWN_PLATFORM_POSIX
  88. pthread_mutex_t m_mutex;
  89. pthread_mutexattr_t m_attr;
  90. #elif CROWN_PLATFORM_WINDOWS
  91. CRITICAL_SECTION m_cs;
  92. #endif
  93. private:
  94. // Disable copying.
  95. Mutex(const Mutex&);
  96. Mutex& operator=(const Mutex&);
  97. };
  98. /// Automatically locks a mutex when created and unlocks when destroyed.
  99. class ScopedMutex
  100. {
  101. public:
  102. /// Locks the given @a m mutex.
  103. ScopedMutex(Mutex& m)
  104. : m_mutex(m)
  105. {
  106. m_mutex.lock();
  107. }
  108. /// Unlocks the mutex passed to ScopedMutex::ScopedMutex()
  109. ~ScopedMutex()
  110. {
  111. m_mutex.unlock();
  112. }
  113. private:
  114. Mutex& m_mutex;
  115. private:
  116. // Disable copying
  117. ScopedMutex(const ScopedMutex&);
  118. ScopedMutex& operator=(const ScopedMutex&);
  119. };
  120. } // namespace crown