DISTRHO Plugin Framework
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.

348 lines
7.5KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2016 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifndef DISTRHO_MUTEX_HPP_INCLUDED
  17. #define DISTRHO_MUTEX_HPP_INCLUDED
  18. #include "../DistrhoUtils.hpp"
  19. #ifdef DISTRHO_OS_WINDOWS
  20. # include <winsock2.h>
  21. # include <windows.h>
  22. #endif
  23. #include <pthread.h>
  24. START_NAMESPACE_DISTRHO
  25. class Signal;
  26. // -----------------------------------------------------------------------
  27. // Mutex class
  28. class Mutex
  29. {
  30. public:
  31. /*
  32. * Constructor.
  33. */
  34. Mutex(bool inheritPriority = true) noexcept
  35. : fMutex()
  36. {
  37. pthread_mutexattr_t attr;
  38. pthread_mutexattr_init(&attr);
  39. pthread_mutexattr_setprotocol(&attr, inheritPriority ? PTHREAD_PRIO_INHERIT : PTHREAD_PRIO_NONE);
  40. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
  41. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
  42. pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST);
  43. pthread_mutex_init(&fMutex, &attr);
  44. pthread_mutexattr_destroy(&attr);
  45. }
  46. /*
  47. * Destructor.
  48. */
  49. ~Mutex() noexcept
  50. {
  51. pthread_mutex_destroy(&fMutex);
  52. }
  53. /*
  54. * Lock the mutex.
  55. */
  56. void lock() const noexcept
  57. {
  58. pthread_mutex_lock(&fMutex);
  59. }
  60. /*
  61. * Try to lock the mutex.
  62. * Returns true if successful.
  63. */
  64. bool tryLock() const noexcept
  65. {
  66. return (pthread_mutex_trylock(&fMutex) == 0);
  67. }
  68. /*
  69. * Unlock the mutex.
  70. */
  71. void unlock() const noexcept
  72. {
  73. pthread_mutex_unlock(&fMutex);
  74. }
  75. private:
  76. mutable pthread_mutex_t fMutex;
  77. friend class Signal;
  78. DISTRHO_PREVENT_HEAP_ALLOCATION
  79. DISTRHO_DECLARE_NON_COPY_CLASS(Mutex)
  80. };
  81. // -----------------------------------------------------------------------
  82. // RecursiveMutex class
  83. class RecursiveMutex
  84. {
  85. public:
  86. /*
  87. * Constructor.
  88. */
  89. RecursiveMutex() noexcept
  90. #ifdef DISTRHO_OS_WINDOWS
  91. : fSection()
  92. #else
  93. : fMutex()
  94. #endif
  95. {
  96. #ifdef DISTRHO_OS_WINDOWS
  97. InitializeCriticalSection(&fSection);
  98. #else
  99. pthread_mutexattr_t attr;
  100. pthread_mutexattr_init(&attr);
  101. pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
  102. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  103. pthread_mutex_init(&fMutex, &attr);
  104. pthread_mutexattr_destroy(&attr);
  105. #endif
  106. }
  107. /*
  108. * Destructor.
  109. */
  110. ~RecursiveMutex() noexcept
  111. {
  112. #ifdef DISTRHO_OS_WINDOWS
  113. DeleteCriticalSection(&fSection);
  114. #else
  115. pthread_mutex_destroy(&fMutex);
  116. #endif
  117. }
  118. /*
  119. * Lock the mutex.
  120. */
  121. void lock() const noexcept
  122. {
  123. #ifdef DISTRHO_OS_WINDOWS
  124. EnterCriticalSection(&fSection);
  125. #else
  126. pthread_mutex_lock(&fMutex);
  127. #endif
  128. }
  129. /*
  130. * Try to lock the mutex.
  131. * Returns true if successful.
  132. */
  133. bool tryLock() const noexcept
  134. {
  135. #ifdef DISTRHO_OS_WINDOWS
  136. return (TryEnterCriticalSection(&fSection) != FALSE);
  137. #else
  138. return (pthread_mutex_trylock(&fMutex) == 0);
  139. #endif
  140. }
  141. /*
  142. * Unlock the mutex.
  143. */
  144. void unlock() const noexcept
  145. {
  146. #ifdef DISTRHO_OS_WINDOWS
  147. LeaveCriticalSection(&fSection);
  148. #else
  149. pthread_mutex_unlock(&fMutex);
  150. #endif
  151. }
  152. private:
  153. #ifdef DISTRHO_OS_WINDOWS
  154. mutable CRITICAL_SECTION fSection;
  155. #else
  156. mutable pthread_mutex_t fMutex;
  157. #endif
  158. DISTRHO_PREVENT_HEAP_ALLOCATION
  159. DISTRHO_DECLARE_NON_COPY_CLASS(RecursiveMutex)
  160. };
  161. // -----------------------------------------------------------------------
  162. // Signal class
  163. class Signal
  164. {
  165. public:
  166. /*
  167. * Constructor.
  168. */
  169. Signal(Mutex& mutex) noexcept
  170. : fCondition(),
  171. fMutex(mutex.fMutex)
  172. {
  173. pthread_condattr_t attr;
  174. pthread_condattr_init(&attr);
  175. pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE);
  176. pthread_cond_init(&fCondition, &attr);
  177. pthread_condattr_destroy(&attr);
  178. }
  179. /*
  180. * Destructor.
  181. */
  182. ~Signal() noexcept
  183. {
  184. pthread_cond_destroy(&fCondition);
  185. }
  186. /*
  187. * Wait for a broadcast.
  188. */
  189. bool wait() noexcept
  190. {
  191. try {
  192. return (pthread_cond_wait(&fCondition, &fMutex) == 0);
  193. } DISTRHO_SAFE_EXCEPTION_RETURN("pthread_cond_wait", false);
  194. }
  195. /*
  196. * Wake up one waiting thread.
  197. */
  198. void signal() noexcept
  199. {
  200. pthread_cond_signal(&fCondition);
  201. }
  202. /*
  203. * Wake up all waiting threads.
  204. */
  205. void broadcast() noexcept
  206. {
  207. pthread_cond_broadcast(&fCondition);
  208. }
  209. private:
  210. pthread_cond_t fCondition;
  211. pthread_mutex_t& fMutex;
  212. DISTRHO_PREVENT_HEAP_ALLOCATION
  213. DISTRHO_DECLARE_NON_COPY_CLASS(Signal)
  214. };
  215. // -----------------------------------------------------------------------
  216. // Helper class to lock&unlock a mutex during a function scope.
  217. template <class Mutex>
  218. class ScopeLocker
  219. {
  220. public:
  221. ScopeLocker(const Mutex& mutex) noexcept
  222. : fMutex(mutex)
  223. {
  224. fMutex.lock();
  225. }
  226. ~ScopeLocker() noexcept
  227. {
  228. fMutex.unlock();
  229. }
  230. private:
  231. const Mutex& fMutex;
  232. DISTRHO_PREVENT_HEAP_ALLOCATION
  233. DISTRHO_DECLARE_NON_COPY_CLASS(ScopeLocker)
  234. };
  235. // -----------------------------------------------------------------------
  236. // Helper class to try-lock&unlock a mutex during a function scope.
  237. template <class Mutex>
  238. class ScopeTryLocker
  239. {
  240. public:
  241. ScopeTryLocker(const Mutex& mutex) noexcept
  242. : fMutex(mutex),
  243. fLocked(mutex.tryLock()) {}
  244. ~ScopeTryLocker() noexcept
  245. {
  246. if (fLocked)
  247. fMutex.unlock();
  248. }
  249. bool wasLocked() const noexcept
  250. {
  251. return fLocked;
  252. }
  253. bool wasNotLocked() const noexcept
  254. {
  255. return !fLocked;
  256. }
  257. private:
  258. const Mutex& fMutex;
  259. const bool fLocked;
  260. DISTRHO_PREVENT_HEAP_ALLOCATION
  261. DISTRHO_DECLARE_NON_COPY_CLASS(ScopeTryLocker)
  262. };
  263. // -----------------------------------------------------------------------
  264. // Helper class to unlock&lock a mutex during a function scope.
  265. template <class Mutex>
  266. class ScopeUnlocker
  267. {
  268. public:
  269. ScopeUnlocker(const Mutex& mutex) noexcept
  270. : fMutex(mutex)
  271. {
  272. fMutex.unlock();
  273. }
  274. ~ScopeUnlocker() noexcept
  275. {
  276. fMutex.lock();
  277. }
  278. private:
  279. const Mutex& fMutex;
  280. DISTRHO_PREVENT_HEAP_ALLOCATION
  281. DISTRHO_DECLARE_NON_COPY_CLASS(ScopeUnlocker)
  282. };
  283. // -----------------------------------------------------------------------
  284. // Define types
  285. typedef ScopeLocker<Mutex> MutexLocker;
  286. typedef ScopeLocker<RecursiveMutex> RecursiveMutexLocker;
  287. typedef ScopeTryLocker<Mutex> MutexTryLocker;
  288. typedef ScopeTryLocker<RecursiveMutex> RecursiveMutexTryLocker;
  289. typedef ScopeUnlocker<Mutex> MutexUnlocker;
  290. typedef ScopeUnlocker<RecursiveMutex> RecursiveMutexUnlocker;
  291. // -----------------------------------------------------------------------
  292. END_NAMESPACE_DISTRHO
  293. #endif // DISTRHO_MUTEX_HPP_INCLUDED