Audio plugin host https://kx.studio/carla
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.

CarlaMutex.hpp 8.4KB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * Carla Mutex
  3. * Copyright (C) 2013-2016 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #ifndef CARLA_MUTEX_HPP_INCLUDED
  18. #define CARLA_MUTEX_HPP_INCLUDED
  19. #include "CarlaUtils.hpp"
  20. #include <pthread.h>
  21. class CarlaSignal;
  22. // -----------------------------------------------------------------------
  23. // CarlaMutex class
  24. class CarlaMutex
  25. {
  26. public:
  27. /*
  28. * Constructor.
  29. */
  30. CarlaMutex(const bool inheritPriority = true) noexcept
  31. : fMutex(),
  32. fTryLockWasCalled(false)
  33. {
  34. pthread_mutexattr_t attr;
  35. pthread_mutexattr_init(&attr);
  36. pthread_mutexattr_setprotocol(&attr, inheritPriority ? PTHREAD_PRIO_INHERIT : PTHREAD_PRIO_NONE);
  37. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
  38. pthread_mutex_init(&fMutex, &attr);
  39. pthread_mutexattr_destroy(&attr);
  40. }
  41. /*
  42. * Destructor.
  43. */
  44. ~CarlaMutex() noexcept
  45. {
  46. pthread_mutex_destroy(&fMutex);
  47. }
  48. /*
  49. * Check if "tryLock()" was called before.
  50. */
  51. bool wasTryLockCalled() const noexcept
  52. {
  53. const bool ret(fTryLockWasCalled);
  54. fTryLockWasCalled = false;
  55. return ret;
  56. }
  57. /*
  58. * Lock the mutex.
  59. */
  60. bool lock() const noexcept
  61. {
  62. return (pthread_mutex_lock(&fMutex) == 0);
  63. }
  64. /*
  65. * Try to lock the mutex.
  66. * Returns true if successful.
  67. */
  68. bool tryLock() const noexcept
  69. {
  70. fTryLockWasCalled = true;
  71. return (pthread_mutex_trylock(&fMutex) == 0);
  72. }
  73. /*
  74. * Unlock the mutex, optionally resetting the tryLock check.
  75. */
  76. void unlock(const bool resetTryLock = false) const noexcept
  77. {
  78. if (resetTryLock)
  79. fTryLockWasCalled = false;
  80. pthread_mutex_unlock(&fMutex);
  81. }
  82. private:
  83. mutable pthread_mutex_t fMutex;
  84. mutable volatile bool fTryLockWasCalled; // true if "tryLock()" was called at least once
  85. CARLA_DECLARE_NON_COPY_CLASS(CarlaMutex)
  86. };
  87. // -----------------------------------------------------------------------
  88. // CarlaRecursiveMutex class
  89. class CarlaRecursiveMutex
  90. {
  91. public:
  92. /*
  93. * Constructor.
  94. */
  95. CarlaRecursiveMutex() noexcept
  96. #ifdef CARLA_OS_WIN
  97. : fSection()
  98. #else
  99. : fMutex()
  100. #endif
  101. {
  102. #ifdef CARLA_OS_WIN
  103. InitializeCriticalSection(&fSection);
  104. #else
  105. pthread_mutexattr_t attr;
  106. pthread_mutexattr_init(&attr);
  107. pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
  108. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  109. pthread_mutex_init(&fMutex, &attr);
  110. pthread_mutexattr_destroy(&attr);
  111. #endif
  112. }
  113. /*
  114. * Destructor.
  115. */
  116. ~CarlaRecursiveMutex() noexcept
  117. {
  118. #ifdef CARLA_OS_WIN
  119. DeleteCriticalSection(&fSection);
  120. #else
  121. pthread_mutex_destroy(&fMutex);
  122. #endif
  123. }
  124. /*
  125. * Lock the mutex.
  126. */
  127. void lock() const noexcept
  128. {
  129. #ifdef CARLA_OS_WIN
  130. EnterCriticalSection(&fSection);
  131. #else
  132. pthread_mutex_lock(&fMutex);
  133. #endif
  134. }
  135. /*
  136. * Try to lock the mutex.
  137. * Returns true if successful.
  138. */
  139. bool tryLock() const noexcept
  140. {
  141. #ifdef CARLA_OS_WIN
  142. return (TryEnterCriticalSection(&fSection) != FALSE);
  143. #else
  144. return (pthread_mutex_trylock(&fMutex) == 0);
  145. #endif
  146. }
  147. /*
  148. * Unlock the mutex.
  149. */
  150. void unlock() const noexcept
  151. {
  152. #ifdef CARLA_OS_WIN
  153. LeaveCriticalSection(&fSection);
  154. #else
  155. pthread_mutex_unlock(&fMutex);
  156. #endif
  157. }
  158. private:
  159. #ifdef CARLA_OS_WIN
  160. mutable CRITICAL_SECTION fSection;
  161. #else
  162. mutable pthread_mutex_t fMutex;
  163. #endif
  164. CARLA_DECLARE_NON_COPY_CLASS(CarlaRecursiveMutex)
  165. };
  166. // -----------------------------------------------------------------------
  167. // CarlaSignal class
  168. class CarlaSignal
  169. {
  170. public:
  171. /*
  172. * Constructor.
  173. */
  174. CarlaSignal() noexcept
  175. : fCondition(),
  176. fMutex(),
  177. fTriggered(false)
  178. {
  179. pthread_condattr_t cattr;
  180. pthread_condattr_init(&cattr);
  181. pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_PRIVATE);
  182. pthread_cond_init(&fCondition, &cattr);
  183. pthread_condattr_destroy(&cattr);
  184. pthread_mutexattr_t mattr;
  185. pthread_mutexattr_init(&mattr);
  186. pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
  187. pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_NORMAL);
  188. pthread_mutex_init(&fMutex, &mattr);
  189. pthread_mutexattr_destroy(&mattr);
  190. }
  191. /*
  192. * Destructor.
  193. */
  194. ~CarlaSignal() noexcept
  195. {
  196. pthread_cond_destroy(&fCondition);
  197. pthread_mutex_destroy(&fMutex);
  198. }
  199. /*
  200. * Wait for a signal.
  201. */
  202. void wait() noexcept
  203. {
  204. pthread_mutex_lock(&fMutex);
  205. while (! fTriggered)
  206. {
  207. try {
  208. pthread_cond_wait(&fCondition, &fMutex);
  209. } CARLA_SAFE_EXCEPTION("pthread_cond_wait");
  210. }
  211. fTriggered = false;
  212. pthread_mutex_unlock(&fMutex);
  213. }
  214. /*
  215. * Wake up all waiting threads.
  216. */
  217. void signal() noexcept
  218. {
  219. pthread_mutex_lock(&fMutex);
  220. if (! fTriggered)
  221. {
  222. fTriggered = true;
  223. pthread_cond_broadcast(&fCondition);
  224. }
  225. pthread_mutex_unlock(&fMutex);
  226. }
  227. private:
  228. pthread_cond_t fCondition;
  229. pthread_mutex_t fMutex;
  230. volatile bool fTriggered;
  231. CARLA_PREVENT_HEAP_ALLOCATION
  232. CARLA_DECLARE_NON_COPY_CLASS(CarlaSignal)
  233. };
  234. // -----------------------------------------------------------------------
  235. // Helper class to lock&unlock a mutex during a function scope.
  236. template <class Mutex>
  237. class CarlaScopeLocker
  238. {
  239. public:
  240. CarlaScopeLocker(const Mutex& mutex) noexcept
  241. : fMutex(mutex)
  242. {
  243. fMutex.lock();
  244. }
  245. ~CarlaScopeLocker() noexcept
  246. {
  247. fMutex.unlock();
  248. }
  249. private:
  250. const Mutex& fMutex;
  251. CARLA_PREVENT_HEAP_ALLOCATION
  252. CARLA_DECLARE_NON_COPY_CLASS(CarlaScopeLocker)
  253. };
  254. // -----------------------------------------------------------------------
  255. // Helper class to try-lock&unlock a mutex during a function scope.
  256. template <class Mutex>
  257. class CarlaScopeTryLocker
  258. {
  259. public:
  260. CarlaScopeTryLocker(const Mutex& mutex) noexcept
  261. : fMutex(mutex),
  262. fLocked(mutex.tryLock()) {}
  263. CarlaScopeTryLocker(const Mutex& mutex, const bool forceLock) noexcept
  264. : fMutex(mutex),
  265. fLocked(forceLock ? mutex.lock() : mutex.tryLock()) {}
  266. ~CarlaScopeTryLocker() noexcept
  267. {
  268. if (fLocked)
  269. fMutex.unlock();
  270. }
  271. bool wasLocked() const noexcept
  272. {
  273. return fLocked;
  274. }
  275. bool wasNotLocked() const noexcept
  276. {
  277. return !fLocked;
  278. }
  279. private:
  280. const Mutex& fMutex;
  281. const bool fLocked;
  282. CARLA_PREVENT_HEAP_ALLOCATION
  283. CARLA_DECLARE_NON_COPY_CLASS(CarlaScopeTryLocker)
  284. };
  285. // -----------------------------------------------------------------------
  286. // Helper class to unlock&lock a mutex during a function scope.
  287. template <class Mutex>
  288. class CarlaScopeUnlocker
  289. {
  290. public:
  291. CarlaScopeUnlocker(const Mutex& mutex) noexcept
  292. : fMutex(mutex)
  293. {
  294. fMutex.unlock();
  295. }
  296. ~CarlaScopeUnlocker() noexcept
  297. {
  298. fMutex.lock();
  299. }
  300. private:
  301. const Mutex& fMutex;
  302. CARLA_PREVENT_HEAP_ALLOCATION
  303. CARLA_DECLARE_NON_COPY_CLASS(CarlaScopeUnlocker)
  304. };
  305. // -----------------------------------------------------------------------
  306. // Define types
  307. typedef CarlaScopeLocker<CarlaMutex> CarlaMutexLocker;
  308. typedef CarlaScopeLocker<CarlaRecursiveMutex> CarlaRecursiveMutexLocker;
  309. typedef CarlaScopeTryLocker<CarlaMutex> CarlaMutexTryLocker;
  310. typedef CarlaScopeTryLocker<CarlaRecursiveMutex> CarlaRecursiveMutexTryLocker;
  311. typedef CarlaScopeUnlocker<CarlaMutex> CarlaMutexUnlocker;
  312. typedef CarlaScopeUnlocker<CarlaRecursiveMutex> CarlaRecursiveMutexUnlocker;
  313. // -----------------------------------------------------------------------
  314. #endif // CARLA_MUTEX_HPP_INCLUDED