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.

169 lines
3.0KB

  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. namespace Jack
  26. {
  27. class JackMutex
  28. {
  29. private:
  30. #ifdef WIN32
  31. HANDLE fMutex;
  32. #else
  33. pthread_mutex_t fMutex;
  34. #endif
  35. public:
  36. #ifdef WIN32
  37. JackMutex()
  38. {
  39. fMutex = CreateMutex(0, FALSE, 0);
  40. }
  41. virtual ~JackMutex()
  42. {
  43. CloseHandle(fMutex);
  44. }
  45. void Lock()
  46. {
  47. DWORD dwWaitResult = WaitForSingleObject(fMutex, INFINITE);
  48. }
  49. bool Trylock()
  50. {
  51. return (WAIT_OBJECT_0 == WaitForSingleObject(fMutex, 0));
  52. }
  53. void Unlock()
  54. {
  55. ReleaseMutex(fMutex);
  56. }
  57. #else
  58. JackMutex()
  59. {
  60. // Use recursive mutex
  61. pthread_mutexattr_t mutex_attr;
  62. assert(pthread_mutexattr_init(&mutex_attr) == 0);
  63. assert(pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE) == 0);
  64. assert(pthread_mutex_init(&fMutex, &mutex_attr) == 0);
  65. }
  66. virtual ~JackMutex()
  67. {
  68. pthread_mutex_destroy(&fMutex);
  69. }
  70. void Lock()
  71. {
  72. pthread_mutex_lock(&fMutex);
  73. }
  74. bool Trylock()
  75. {
  76. return (pthread_mutex_trylock(&fMutex) == 0);
  77. }
  78. void Unlock()
  79. {
  80. pthread_mutex_unlock(&fMutex);
  81. }
  82. #endif
  83. };
  84. class JackLockAble
  85. {
  86. private:
  87. JackMutex fMutex;
  88. public:
  89. JackLockAble()
  90. {}
  91. virtual ~JackLockAble()
  92. {}
  93. void Lock()
  94. {
  95. fMutex.Lock();
  96. }
  97. bool Trylock()
  98. {
  99. return fMutex.Trylock();
  100. }
  101. void Unlock()
  102. {
  103. fMutex.Unlock();
  104. }
  105. };
  106. class JackLock
  107. {
  108. private:
  109. JackLockAble* fObj;
  110. public:
  111. JackLock(JackLockAble* obj): fObj(obj)
  112. {
  113. fObj->Lock();
  114. }
  115. JackLock(const JackLockAble* obj): fObj((JackLockAble*)obj)
  116. {
  117. fObj->Lock();
  118. }
  119. virtual ~JackLock()
  120. {
  121. fObj->Unlock();
  122. }
  123. };
  124. } // namespace
  125. #endif