semaphore.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 "assert.h"
  26. #include "mutex.h"
  27. #if CROWN_PLATFORM_POSIX
  28. #include <pthread.h>
  29. #elif CROWN_PLATFORM_WINDOWS
  30. #include "win_headers.h"
  31. #include <limits.h>
  32. #endif
  33. namespace crown
  34. {
  35. struct Semaphore
  36. {
  37. Semaphore()
  38. #if CROWN_PLATFORM_POSIX
  39. : m_count(0)
  40. #elif CROWN_PLATFORM_WINDOWS
  41. : m_handle(INVALID_HANDLE_VALUE)
  42. #endif
  43. {
  44. #if CROWN_PLATFORM_POSIX
  45. int result = pthread_cond_init(&m_cond, NULL);
  46. CE_ASSERT(result == 0, "pthread_cond_init: errno = %d", result);
  47. CE_UNUSED(result);
  48. #elif CROWN_PLATFORM_WINDOWS
  49. m_handle = CreateSemaphore(NULL, 0, LONG_MAX, NULL);
  50. CE_ASSERT(m_handle != NULL, "Unable to create semaphore!");
  51. CE_UNUSED(m_handle);
  52. #endif
  53. }
  54. ~Semaphore()
  55. {
  56. #if CROWN_PLATFORM_POSIX
  57. int result = pthread_cond_destroy(&m_cond);
  58. CE_ASSERT(result == 0, "pthread_cond_destroy: errno = %d", result);
  59. CE_UNUSED(result);
  60. #elif CROWN_PLATFORM_WINDOWS
  61. CloseHandle(m_handle);
  62. #endif
  63. }
  64. void post(uint32_t count = 1)
  65. {
  66. #if CROWN_PLATFORM_POSIX
  67. m_mutex.lock();
  68. for (uint32_t i = 0; i < count; i++)
  69. {
  70. int result = pthread_cond_signal(&m_cond);
  71. CE_ASSERT(result == 0, "pthread_cond_signal: errno = %d", result);
  72. CE_UNUSED(result);
  73. }
  74. m_count += count;
  75. m_mutex.unlock();
  76. #elif CROWN_PLATFORM_WINDOWS
  77. ReleaseSemaphore(m_handle, count, NULL);
  78. #endif
  79. }
  80. void wait()
  81. {
  82. #if CROWN_PLATFORM_POSIX
  83. m_mutex.lock();
  84. while (m_count <= 0)
  85. {
  86. int result = pthread_cond_wait(&m_cond, &(m_mutex.m_mutex));
  87. CE_ASSERT(result == 0, "pthread_cond_wait: errno = %d", result);
  88. CE_UNUSED(result);
  89. }
  90. m_count--;
  91. m_mutex.unlock();
  92. #elif CROWN_PLATFORM_WINDOWS
  93. DWORD result = WaitForSingleObject(m_handle, INFINITE);
  94. CE_ASSERT(result == WAIT_OBJECT_0, "WaitForSingleObject: GetLastError = %d", GetLastError());
  95. CE_UNUSED(result);
  96. #endif
  97. }
  98. private:
  99. #if CROWN_PLATFORM_POSIX
  100. Mutex m_mutex;
  101. pthread_cond_t m_cond;
  102. int32_t m_count;
  103. #elif CROWN_PLATFORM_WINDOWS
  104. HANDLE m_handle;
  105. #endif
  106. private:
  107. // Disable copying
  108. Semaphore(const Semaphore& s);
  109. Semaphore& operator=(const Semaphore& s);
  110. };
  111. } // namespace crown