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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21. #ifdef WIN32
  22. #include <windows.h>
  23. #else
  24. #include <pthread.h>
  25. #endif
  26. #include <rdr/Exception.h>
  27. #include <os/Mutex.h>
  28. using namespace os;
  29. Mutex::Mutex()
  30. {
  31. #ifdef WIN32
  32. systemMutex = new CRITICAL_SECTION;
  33. InitializeCriticalSection((CRITICAL_SECTION*)systemMutex);
  34. #else
  35. int ret;
  36. systemMutex = new pthread_mutex_t;
  37. ret = pthread_mutex_init((pthread_mutex_t*)systemMutex, NULL);
  38. if (ret != 0)
  39. throw rdr::SystemException("Failed to create mutex", ret);
  40. #endif
  41. }
  42. Mutex::~Mutex()
  43. {
  44. #ifdef WIN32
  45. DeleteCriticalSection((CRITICAL_SECTION*)systemMutex);
  46. delete (CRITICAL_SECTION*)systemMutex;
  47. #else
  48. pthread_mutex_destroy((pthread_mutex_t*)systemMutex);
  49. delete (pthread_mutex_t*)systemMutex;
  50. #endif
  51. }
  52. void Mutex::lock()
  53. {
  54. #ifdef WIN32
  55. EnterCriticalSection((CRITICAL_SECTION*)systemMutex);
  56. #else
  57. int ret;
  58. ret = pthread_mutex_lock((pthread_mutex_t*)systemMutex);
  59. if (ret != 0)
  60. throw rdr::SystemException("Failed to lock mutex", ret);
  61. #endif
  62. }
  63. void Mutex::unlock()
  64. {
  65. #ifdef WIN32
  66. LeaveCriticalSection((CRITICAL_SECTION*)systemMutex);
  67. #else
  68. int ret;
  69. ret = pthread_mutex_unlock((pthread_mutex_t*)systemMutex);
  70. if (ret != 0)
  71. throw rdr::SystemException("Failed to unlock mutex", ret);
  72. #endif
  73. }
  74. Condition::Condition(Mutex* mutex)
  75. {
  76. this->mutex = mutex;
  77. #ifdef WIN32
  78. systemCondition = new CONDITION_VARIABLE;
  79. InitializeConditionVariable((CONDITION_VARIABLE*)systemCondition);
  80. #else
  81. int ret;
  82. systemCondition = new pthread_cond_t;
  83. ret = pthread_cond_init((pthread_cond_t*)systemCondition, NULL);
  84. if (ret != 0)
  85. throw rdr::SystemException("Failed to create condition variable", ret);
  86. #endif
  87. }
  88. Condition::~Condition()
  89. {
  90. #ifdef WIN32
  91. delete (CONDITION_VARIABLE*)systemCondition;
  92. #else
  93. pthread_cond_destroy((pthread_cond_t*)systemCondition);
  94. delete (pthread_cond_t*)systemCondition;
  95. #endif
  96. }
  97. void Condition::wait()
  98. {
  99. #ifdef WIN32
  100. BOOL ret;
  101. ret = SleepConditionVariableCS((CONDITION_VARIABLE*)systemCondition,
  102. (CRITICAL_SECTION*)mutex->systemMutex,
  103. INFINITE);
  104. if (!ret)
  105. throw rdr::SystemException("Failed to wait on condition variable", GetLastError());
  106. #else
  107. int ret;
  108. ret = pthread_cond_wait((pthread_cond_t*)systemCondition,
  109. (pthread_mutex_t*)mutex->systemMutex);
  110. if (ret != 0)
  111. throw rdr::SystemException("Failed to wait on condition variable", ret);
  112. #endif
  113. }
  114. void Condition::signal()
  115. {
  116. #ifdef WIN32
  117. WakeConditionVariable((CONDITION_VARIABLE*)systemCondition);
  118. #else
  119. int ret;
  120. ret = pthread_cond_signal((pthread_cond_t*)systemCondition);
  121. if (ret != 0)
  122. throw rdr::SystemException("Failed to signal condition variable", ret);
  123. #endif
  124. }
  125. void Condition::broadcast()
  126. {
  127. #ifdef WIN32
  128. WakeAllConditionVariable((CONDITION_VARIABLE*)systemCondition);
  129. #else
  130. int ret;
  131. ret = pthread_cond_broadcast((pthread_cond_t*)systemCondition);
  132. if (ret != 0)
  133. throw rdr::SystemException("Failed to broadcast condition variable", ret);
  134. #endif
  135. }