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.

381 lines
8.5KB

  1. /*
  2. * Carla Mutex
  3. * Copyright (C) 2013-2022 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_COPYABLE(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. bool lock() const noexcept
  128. {
  129. #ifdef CARLA_OS_WIN
  130. EnterCriticalSection(&fSection);
  131. return true;
  132. #else
  133. return (pthread_mutex_lock(&fMutex) == 0);
  134. #endif
  135. }
  136. /*
  137. * Try to lock the mutex.
  138. * Returns true if successful.
  139. */
  140. bool tryLock() const noexcept
  141. {
  142. #ifdef CARLA_OS_WIN
  143. return (TryEnterCriticalSection(&fSection) != FALSE);
  144. #else
  145. return (pthread_mutex_trylock(&fMutex) == 0);
  146. #endif
  147. }
  148. /*
  149. * Unlock the mutex.
  150. */
  151. void unlock() const noexcept
  152. {
  153. #ifdef CARLA_OS_WIN
  154. LeaveCriticalSection(&fSection);
  155. #else
  156. pthread_mutex_unlock(&fMutex);
  157. #endif
  158. }
  159. private:
  160. #ifdef CARLA_OS_WIN
  161. mutable CRITICAL_SECTION fSection;
  162. #else
  163. mutable pthread_mutex_t fMutex;
  164. #endif
  165. CARLA_DECLARE_NON_COPYABLE(CarlaRecursiveMutex)
  166. };
  167. // -----------------------------------------------------------------------
  168. // CarlaSignal class
  169. class CarlaSignal
  170. {
  171. public:
  172. /*
  173. * Constructor.
  174. */
  175. CarlaSignal() noexcept
  176. : fCondition(),
  177. fMutex(),
  178. fTriggered(false)
  179. {
  180. pthread_condattr_t cattr;
  181. pthread_condattr_init(&cattr);
  182. pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_PRIVATE);
  183. pthread_cond_init(&fCondition, &cattr);
  184. pthread_condattr_destroy(&cattr);
  185. pthread_mutexattr_t mattr;
  186. pthread_mutexattr_init(&mattr);
  187. pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
  188. pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_NORMAL);
  189. pthread_mutex_init(&fMutex, &mattr);
  190. pthread_mutexattr_destroy(&mattr);
  191. }
  192. /*
  193. * Destructor.
  194. */
  195. ~CarlaSignal() noexcept
  196. {
  197. pthread_cond_destroy(&fCondition);
  198. pthread_mutex_destroy(&fMutex);
  199. }
  200. /*
  201. * Wait for a signal.
  202. */
  203. void wait() noexcept
  204. {
  205. pthread_mutex_lock(&fMutex);
  206. while (! fTriggered)
  207. {
  208. try {
  209. pthread_cond_wait(&fCondition, &fMutex);
  210. } CARLA_SAFE_EXCEPTION("pthread_cond_wait");
  211. }
  212. fTriggered = false;
  213. pthread_mutex_unlock(&fMutex);
  214. }
  215. /*
  216. * Wake up all waiting threads.
  217. */
  218. void signal() noexcept
  219. {
  220. pthread_mutex_lock(&fMutex);
  221. if (! fTriggered)
  222. {
  223. fTriggered = true;
  224. pthread_cond_broadcast(&fCondition);
  225. }
  226. pthread_mutex_unlock(&fMutex);
  227. }
  228. private:
  229. pthread_cond_t fCondition;
  230. pthread_mutex_t fMutex;
  231. volatile bool fTriggered;
  232. CARLA_PREVENT_HEAP_ALLOCATION
  233. CARLA_DECLARE_NON_COPYABLE(CarlaSignal)
  234. };
  235. // -----------------------------------------------------------------------
  236. // Helper class to lock&unlock a mutex during a function scope.
  237. template <class Mutex>
  238. class CarlaScopeLocker
  239. {
  240. public:
  241. CarlaScopeLocker(const Mutex& mutex) noexcept
  242. : fMutex(mutex)
  243. {
  244. fMutex.lock();
  245. }
  246. ~CarlaScopeLocker() noexcept
  247. {
  248. fMutex.unlock();
  249. }
  250. private:
  251. const Mutex& fMutex;
  252. CARLA_PREVENT_HEAP_ALLOCATION
  253. CARLA_DECLARE_NON_COPYABLE(CarlaScopeLocker)
  254. };
  255. // -----------------------------------------------------------------------
  256. // Helper class to try-lock&unlock a mutex during a function scope.
  257. template <class Mutex>
  258. class CarlaScopeTryLocker
  259. {
  260. public:
  261. CarlaScopeTryLocker(const Mutex& mutex) noexcept
  262. : fMutex(mutex),
  263. fLocked(mutex.tryLock()) {}
  264. CarlaScopeTryLocker(const Mutex& mutex, const bool forceLock) noexcept
  265. : fMutex(mutex),
  266. fLocked(forceLock ? mutex.lock() : mutex.tryLock()) {}
  267. ~CarlaScopeTryLocker() noexcept
  268. {
  269. if (fLocked)
  270. fMutex.unlock();
  271. }
  272. bool wasLocked() const noexcept
  273. {
  274. return fLocked;
  275. }
  276. bool wasNotLocked() const noexcept
  277. {
  278. return !fLocked;
  279. }
  280. bool tryAgain() const noexcept
  281. {
  282. return fMutex.tryLock();
  283. }
  284. private:
  285. const Mutex& fMutex;
  286. const bool fLocked;
  287. CARLA_PREVENT_HEAP_ALLOCATION
  288. CARLA_DECLARE_NON_COPYABLE(CarlaScopeTryLocker)
  289. };
  290. // -----------------------------------------------------------------------
  291. // Helper class to unlock&lock a mutex during a function scope.
  292. template <class Mutex>
  293. class CarlaScopeUnlocker
  294. {
  295. public:
  296. CarlaScopeUnlocker(const Mutex& mutex) noexcept
  297. : fMutex(mutex)
  298. {
  299. fMutex.unlock();
  300. }
  301. ~CarlaScopeUnlocker() noexcept
  302. {
  303. fMutex.lock();
  304. }
  305. private:
  306. const Mutex& fMutex;
  307. CARLA_PREVENT_HEAP_ALLOCATION
  308. CARLA_DECLARE_NON_COPYABLE(CarlaScopeUnlocker)
  309. };
  310. // -----------------------------------------------------------------------
  311. // Define types
  312. typedef CarlaScopeLocker<CarlaMutex> CarlaMutexLocker;
  313. typedef CarlaScopeLocker<CarlaRecursiveMutex> CarlaRecursiveMutexLocker;
  314. typedef CarlaScopeTryLocker<CarlaMutex> CarlaMutexTryLocker;
  315. typedef CarlaScopeTryLocker<CarlaRecursiveMutex> CarlaRecursiveMutexTryLocker;
  316. typedef CarlaScopeUnlocker<CarlaMutex> CarlaMutexUnlocker;
  317. typedef CarlaScopeUnlocker<CarlaRecursiveMutex> CarlaRecursiveMutexUnlocker;
  318. // -----------------------------------------------------------------------
  319. #endif // CARLA_MUTEX_HPP_INCLUDED