jack2 codebase
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.

179 lines
3.4KB

  1. /*
  2. Copyright (C) 2006 Grame
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with this library; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. Grame Research Laboratory, 9 rue du Garet, 69001 Lyon - France
  15. grame@grame.fr
  16. */
  17. #ifndef __JackMutex__
  18. #define __JackMutex__
  19. #ifdef WIN32
  20. #include <windows.h>
  21. #else
  22. #include <pthread.h>
  23. #endif
  24. #include <assert.h>
  25. #include "JackError.h"
  26. namespace Jack
  27. {
  28. class JackMutex
  29. {
  30. private:
  31. #ifdef WIN32
  32. HANDLE fMutex;
  33. #else
  34. pthread_mutex_t fMutex;
  35. #endif
  36. public:
  37. #ifdef WIN32
  38. JackMutex()
  39. {
  40. // In recursive mode by default
  41. fMutex = CreateMutex(0, FALSE, 0);
  42. }
  43. virtual ~JackMutex()
  44. {
  45. CloseHandle(fMutex);
  46. }
  47. void Lock()
  48. {
  49. DWORD dwWaitResult = WaitForSingleObject(fMutex, INFINITE);
  50. }
  51. bool Trylock()
  52. {
  53. return (WAIT_OBJECT_0 == WaitForSingleObject(fMutex, 0));
  54. }
  55. void Unlock()
  56. {
  57. ReleaseMutex(fMutex);
  58. }
  59. #else
  60. JackMutex()
  61. {
  62. // Use recursive mutex
  63. pthread_mutexattr_t mutex_attr;
  64. int res;
  65. res = pthread_mutexattr_init(&mutex_attr);
  66. assert(res == 0);
  67. res = pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
  68. assert(res == 0);
  69. res = pthread_mutex_init(&fMutex, &mutex_attr);
  70. assert(res == 0);
  71. res = pthread_mutexattr_destroy(&mutex_attr);
  72. assert(res == 0);
  73. }
  74. virtual ~JackMutex()
  75. {
  76. pthread_mutex_destroy(&fMutex);
  77. }
  78. void Lock()
  79. {
  80. int res = pthread_mutex_lock(&fMutex);
  81. if (res != 0)
  82. jack_error("JackMutex::Lock res = %d", res);
  83. }
  84. bool Trylock()
  85. {
  86. return (pthread_mutex_trylock(&fMutex) == 0);
  87. }
  88. void Unlock()
  89. {
  90. int res = pthread_mutex_unlock(&fMutex);
  91. if (res != 0)
  92. jack_error("JackMutex::Unlock res = %d", res);
  93. }
  94. #endif
  95. };
  96. class JackLockAble
  97. {
  98. private:
  99. JackMutex fMutex;
  100. public:
  101. JackLockAble()
  102. {}
  103. virtual ~JackLockAble()
  104. {}
  105. void Lock()
  106. {
  107. fMutex.Lock();
  108. }
  109. bool Trylock()
  110. {
  111. return fMutex.Trylock();
  112. }
  113. void Unlock()
  114. {
  115. fMutex.Unlock();
  116. }
  117. };
  118. class JackLock
  119. {
  120. private:
  121. JackLockAble* fObj;
  122. public:
  123. JackLock(JackLockAble* obj): fObj(obj)
  124. {
  125. fObj->Lock();
  126. }
  127. JackLock(const JackLockAble* obj): fObj((JackLockAble*)obj)
  128. {
  129. fObj->Lock();
  130. }
  131. virtual ~JackLock()
  132. {
  133. fObj->Unlock();
  134. }
  135. };
  136. } // namespace
  137. #endif