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.

386 lines
8.7KB

  1. /*
  2. * Carla Mutex
  3. * Copyright (C) 2013-2023 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. #ifdef __GNUC__
  37. pthread_mutexattr_setprotocol(&attr, inheritPriority ? PTHREAD_PRIO_INHERIT : PTHREAD_PRIO_NONE);
  38. #else
  39. // unsupported?
  40. (void)inheritPriority;
  41. #endif
  42. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
  43. pthread_mutex_init(&fMutex, &attr);
  44. pthread_mutexattr_destroy(&attr);
  45. }
  46. /*
  47. * Destructor.
  48. */
  49. ~CarlaMutex() noexcept
  50. {
  51. pthread_mutex_destroy(&fMutex);
  52. }
  53. /*
  54. * Check if "tryLock()" was called before.
  55. */
  56. bool wasTryLockCalled() const noexcept
  57. {
  58. const bool ret(fTryLockWasCalled);
  59. fTryLockWasCalled = false;
  60. return ret;
  61. }
  62. /*
  63. * Lock the mutex.
  64. */
  65. bool lock() const noexcept
  66. {
  67. return (pthread_mutex_lock(&fMutex) == 0);
  68. }
  69. /*
  70. * Try to lock the mutex.
  71. * Returns true if successful.
  72. */
  73. bool tryLock() const noexcept
  74. {
  75. fTryLockWasCalled = true;
  76. return (pthread_mutex_trylock(&fMutex) == 0);
  77. }
  78. /*
  79. * Unlock the mutex, optionally resetting the tryLock check.
  80. */
  81. void unlock(const bool resetTryLock = false) const noexcept
  82. {
  83. if (resetTryLock)
  84. fTryLockWasCalled = false;
  85. pthread_mutex_unlock(&fMutex);
  86. }
  87. private:
  88. mutable pthread_mutex_t fMutex;
  89. mutable volatile bool fTryLockWasCalled; // true if "tryLock()" was called at least once
  90. CARLA_DECLARE_NON_COPYABLE(CarlaMutex)
  91. };
  92. // -----------------------------------------------------------------------
  93. // CarlaRecursiveMutex class
  94. class CarlaRecursiveMutex
  95. {
  96. public:
  97. /*
  98. * Constructor.
  99. */
  100. CarlaRecursiveMutex() noexcept
  101. #ifdef CARLA_OS_WIN
  102. : fSection()
  103. #else
  104. : fMutex()
  105. #endif
  106. {
  107. #ifdef CARLA_OS_WIN
  108. InitializeCriticalSection(&fSection);
  109. #else
  110. pthread_mutexattr_t attr;
  111. pthread_mutexattr_init(&attr);
  112. pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
  113. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  114. pthread_mutex_init(&fMutex, &attr);
  115. pthread_mutexattr_destroy(&attr);
  116. #endif
  117. }
  118. /*
  119. * Destructor.
  120. */
  121. ~CarlaRecursiveMutex() noexcept
  122. {
  123. #ifdef CARLA_OS_WIN
  124. DeleteCriticalSection(&fSection);
  125. #else
  126. pthread_mutex_destroy(&fMutex);
  127. #endif
  128. }
  129. /*
  130. * Lock the mutex.
  131. */
  132. bool lock() const noexcept
  133. {
  134. #ifdef CARLA_OS_WIN
  135. EnterCriticalSection(&fSection);
  136. return true;
  137. #else
  138. return (pthread_mutex_lock(&fMutex) == 0);
  139. #endif
  140. }
  141. /*
  142. * Try to lock the mutex.
  143. * Returns true if successful.
  144. */
  145. bool tryLock() const noexcept
  146. {
  147. #ifdef CARLA_OS_WIN
  148. return (TryEnterCriticalSection(&fSection) != FALSE);
  149. #else
  150. return (pthread_mutex_trylock(&fMutex) == 0);
  151. #endif
  152. }
  153. /*
  154. * Unlock the mutex.
  155. */
  156. void unlock() const noexcept
  157. {
  158. #ifdef CARLA_OS_WIN
  159. LeaveCriticalSection(&fSection);
  160. #else
  161. pthread_mutex_unlock(&fMutex);
  162. #endif
  163. }
  164. private:
  165. #ifdef CARLA_OS_WIN
  166. mutable CRITICAL_SECTION fSection;
  167. #else
  168. mutable pthread_mutex_t fMutex;
  169. #endif
  170. CARLA_DECLARE_NON_COPYABLE(CarlaRecursiveMutex)
  171. };
  172. // -----------------------------------------------------------------------
  173. // CarlaSignal class
  174. class CarlaSignal
  175. {
  176. public:
  177. /*
  178. * Constructor.
  179. */
  180. CarlaSignal() noexcept
  181. : fCondition(),
  182. fMutex(),
  183. fTriggered(false)
  184. {
  185. pthread_condattr_t cattr;
  186. pthread_condattr_init(&cattr);
  187. pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_PRIVATE);
  188. pthread_cond_init(&fCondition, &cattr);
  189. pthread_condattr_destroy(&cattr);
  190. pthread_mutexattr_t mattr;
  191. pthread_mutexattr_init(&mattr);
  192. pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
  193. pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_NORMAL);
  194. pthread_mutex_init(&fMutex, &mattr);
  195. pthread_mutexattr_destroy(&mattr);
  196. }
  197. /*
  198. * Destructor.
  199. */
  200. ~CarlaSignal() noexcept
  201. {
  202. pthread_cond_destroy(&fCondition);
  203. pthread_mutex_destroy(&fMutex);
  204. }
  205. /*
  206. * Wait for a signal.
  207. */
  208. void wait() noexcept
  209. {
  210. pthread_mutex_lock(&fMutex);
  211. while (! fTriggered)
  212. {
  213. try {
  214. pthread_cond_wait(&fCondition, &fMutex);
  215. } CARLA_SAFE_EXCEPTION("pthread_cond_wait");
  216. }
  217. fTriggered = false;
  218. pthread_mutex_unlock(&fMutex);
  219. }
  220. /*
  221. * Wake up all waiting threads.
  222. */
  223. void signal() noexcept
  224. {
  225. pthread_mutex_lock(&fMutex);
  226. if (! fTriggered)
  227. {
  228. fTriggered = true;
  229. pthread_cond_broadcast(&fCondition);
  230. }
  231. pthread_mutex_unlock(&fMutex);
  232. }
  233. private:
  234. pthread_cond_t fCondition;
  235. pthread_mutex_t fMutex;
  236. volatile bool fTriggered;
  237. CARLA_PREVENT_HEAP_ALLOCATION
  238. CARLA_DECLARE_NON_COPYABLE(CarlaSignal)
  239. };
  240. // -----------------------------------------------------------------------
  241. // Helper class to lock&unlock a mutex during a function scope.
  242. template <class Mutex>
  243. class CarlaScopeLocker
  244. {
  245. public:
  246. CarlaScopeLocker(const Mutex& mutex) noexcept
  247. : fMutex(mutex)
  248. {
  249. fMutex.lock();
  250. }
  251. ~CarlaScopeLocker() noexcept
  252. {
  253. fMutex.unlock();
  254. }
  255. private:
  256. const Mutex& fMutex;
  257. CARLA_PREVENT_HEAP_ALLOCATION
  258. CARLA_DECLARE_NON_COPYABLE(CarlaScopeLocker)
  259. };
  260. // -----------------------------------------------------------------------
  261. // Helper class to try-lock&unlock a mutex during a function scope.
  262. template <class Mutex>
  263. class CarlaScopeTryLocker
  264. {
  265. public:
  266. CarlaScopeTryLocker(const Mutex& mutex) noexcept
  267. : fMutex(mutex),
  268. fLocked(mutex.tryLock()) {}
  269. CarlaScopeTryLocker(const Mutex& mutex, const bool forceLock) noexcept
  270. : fMutex(mutex),
  271. fLocked(forceLock ? mutex.lock() : mutex.tryLock()) {}
  272. ~CarlaScopeTryLocker() noexcept
  273. {
  274. if (fLocked)
  275. fMutex.unlock();
  276. }
  277. bool wasLocked() const noexcept
  278. {
  279. return fLocked;
  280. }
  281. bool wasNotLocked() const noexcept
  282. {
  283. return !fLocked;
  284. }
  285. bool tryAgain() const noexcept
  286. {
  287. return fMutex.tryLock();
  288. }
  289. private:
  290. const Mutex& fMutex;
  291. const bool fLocked;
  292. CARLA_PREVENT_HEAP_ALLOCATION
  293. CARLA_DECLARE_NON_COPYABLE(CarlaScopeTryLocker)
  294. };
  295. // -----------------------------------------------------------------------
  296. // Helper class to unlock&lock a mutex during a function scope.
  297. template <class Mutex>
  298. class CarlaScopeUnlocker
  299. {
  300. public:
  301. CarlaScopeUnlocker(const Mutex& mutex) noexcept
  302. : fMutex(mutex)
  303. {
  304. fMutex.unlock();
  305. }
  306. ~CarlaScopeUnlocker() noexcept
  307. {
  308. fMutex.lock();
  309. }
  310. private:
  311. const Mutex& fMutex;
  312. CARLA_PREVENT_HEAP_ALLOCATION
  313. CARLA_DECLARE_NON_COPYABLE(CarlaScopeUnlocker)
  314. };
  315. // -----------------------------------------------------------------------
  316. // Define types
  317. typedef CarlaScopeLocker<CarlaMutex> CarlaMutexLocker;
  318. typedef CarlaScopeLocker<CarlaRecursiveMutex> CarlaRecursiveMutexLocker;
  319. typedef CarlaScopeTryLocker<CarlaMutex> CarlaMutexTryLocker;
  320. typedef CarlaScopeTryLocker<CarlaRecursiveMutex> CarlaRecursiveMutexTryLocker;
  321. typedef CarlaScopeUnlocker<CarlaMutex> CarlaMutexUnlocker;
  322. typedef CarlaScopeUnlocker<CarlaRecursiveMutex> CarlaRecursiveMutexUnlocker;
  323. // -----------------------------------------------------------------------
  324. #endif // CARLA_MUTEX_HPP_INCLUDED