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.

Mutex.cxx 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* Copyright 2015 Pierre Ossman for Cendio AB
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. #ifdef WIN32
  19. #include <windows.h>
  20. #else
  21. #include <pthread.h>
  22. #endif
  23. #include <rdr/Exception.h>
  24. #include <os/Mutex.h>
  25. using namespace os;
  26. Mutex::Mutex()
  27. {
  28. #ifdef WIN32
  29. systemMutex = new CRITICAL_SECTION;
  30. InitializeCriticalSection((CRITICAL_SECTION*)systemMutex);
  31. #else
  32. int ret;
  33. systemMutex = new pthread_mutex_t;
  34. ret = pthread_mutex_init((pthread_mutex_t*)systemMutex, NULL);
  35. if (ret != 0)
  36. throw rdr::SystemException("Failed to create mutex", ret);
  37. #endif
  38. }
  39. Mutex::~Mutex()
  40. {
  41. #ifdef WIN32
  42. DeleteCriticalSection((CRITICAL_SECTION*)systemMutex);
  43. delete (CRITICAL_SECTION*)systemMutex;
  44. #else
  45. pthread_mutex_destroy((pthread_mutex_t*)systemMutex);
  46. delete (pthread_mutex_t*)systemMutex;
  47. #endif
  48. }
  49. void Mutex::lock()
  50. {
  51. #ifdef WIN32
  52. EnterCriticalSection((CRITICAL_SECTION*)systemMutex);
  53. #else
  54. int ret;
  55. ret = pthread_mutex_lock((pthread_mutex_t*)systemMutex);
  56. if (ret != 0)
  57. throw rdr::SystemException("Failed to lock mutex", ret);
  58. #endif
  59. }
  60. void Mutex::unlock()
  61. {
  62. #ifdef WIN32
  63. LeaveCriticalSection((CRITICAL_SECTION*)systemMutex);
  64. #else
  65. int ret;
  66. ret = pthread_mutex_unlock((pthread_mutex_t*)systemMutex);
  67. if (ret != 0)
  68. throw rdr::SystemException("Failed to unlock mutex", ret);
  69. #endif
  70. }
  71. Condition::Condition(Mutex* mutex)
  72. {
  73. this->mutex = mutex;
  74. #ifdef WIN32
  75. systemCondition = new CONDITION_VARIABLE;
  76. InitializeConditionVariable((CONDITION_VARIABLE*)systemCondition);
  77. #else
  78. int ret;
  79. systemCondition = new pthread_cond_t;
  80. ret = pthread_cond_init((pthread_cond_t*)systemCondition, NULL);
  81. if (ret != 0)
  82. throw rdr::SystemException("Failed to create condition variable", ret);
  83. #endif
  84. }
  85. Condition::~Condition()
  86. {
  87. #ifdef WIN32
  88. delete (CONDITION_VARIABLE*)systemCondition;
  89. #else
  90. pthread_cond_destroy((pthread_cond_t*)systemCondition);
  91. delete (pthread_cond_t*)systemCondition;
  92. #endif
  93. }
  94. void Condition::wait()
  95. {
  96. #ifdef WIN32
  97. BOOL ret;
  98. ret = SleepConditionVariableCS((CONDITION_VARIABLE*)systemCondition,
  99. (CRITICAL_SECTION*)mutex->systemMutex,
  100. INFINITE);
  101. if (!ret)
  102. throw rdr::SystemException("Failed to wait on condition variable", GetLastError());
  103. #else
  104. int ret;
  105. ret = pthread_cond_wait((pthread_cond_t*)systemCondition,
  106. (pthread_mutex_t*)mutex->systemMutex);
  107. if (ret != 0)
  108. throw rdr::SystemException("Failed to wait on condition variable", ret);
  109. #endif
  110. }
  111. void Condition::signal()
  112. {
  113. #ifdef WIN32
  114. WakeConditionVariable((CONDITION_VARIABLE*)systemCondition);
  115. #else
  116. int ret;
  117. ret = pthread_cond_signal((pthread_cond_t*)systemCondition);
  118. if (ret != 0)
  119. throw rdr::SystemException("Failed to signal condition variable", ret);
  120. #endif
  121. }
  122. void Condition::broadcast()
  123. {
  124. #ifdef WIN32
  125. WakeAllConditionVariable((CONDITION_VARIABLE*)systemCondition);
  126. #else
  127. int ret;
  128. ret = pthread_cond_broadcast((pthread_cond_t*)systemCondition);
  129. if (ret != 0)
  130. throw rdr::SystemException("Failed to broadcast condition variable", ret);
  131. #endif
  132. }