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.

147 lines
3.2KB

  1. /*
  2. * Carla Mutex
  3. * Copyright (C) 2013-2014 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. // -----------------------------------------------------------------------
  22. // CarlaMutex class
  23. class CarlaMutex
  24. {
  25. public:
  26. /*
  27. * Constructor.
  28. */
  29. CarlaMutex() noexcept
  30. : fTryLockWasCalled(false)
  31. {
  32. pthread_mutex_init(&fMutex, nullptr);
  33. }
  34. /*
  35. * Destructor.
  36. */
  37. ~CarlaMutex() noexcept
  38. {
  39. pthread_mutex_destroy(&fMutex);
  40. }
  41. /*
  42. * Check if "tryLock()" was called before.
  43. */
  44. bool wasTryLockCalled() const noexcept
  45. {
  46. const bool ret(fTryLockWasCalled);
  47. fTryLockWasCalled = false;
  48. return ret;
  49. }
  50. /*
  51. * Lock the mutex.
  52. */
  53. void lock() const noexcept
  54. {
  55. pthread_mutex_lock(&fMutex);
  56. }
  57. /*
  58. * Try to lock the mutex.
  59. * Returns true if successful.
  60. */
  61. bool tryLock() const noexcept
  62. {
  63. fTryLockWasCalled = true;
  64. return (pthread_mutex_trylock(&fMutex) == 0);
  65. }
  66. /*
  67. * Unlock the mutex, optionally resetting the tryLock check.
  68. */
  69. void unlock(const bool resetTryLock = false) const noexcept
  70. {
  71. if (resetTryLock)
  72. fTryLockWasCalled = false;
  73. pthread_mutex_unlock(&fMutex);
  74. }
  75. /*
  76. * Helper class to lock&unlock a mutex during a function scope.
  77. */
  78. class ScopedLocker
  79. {
  80. public:
  81. ScopedLocker(CarlaMutex& mutex) noexcept
  82. : fMutex(mutex)
  83. {
  84. fMutex.lock();
  85. }
  86. ~ScopedLocker() noexcept
  87. {
  88. fMutex.unlock();
  89. }
  90. private:
  91. CarlaMutex& fMutex;
  92. CARLA_PREVENT_HEAP_ALLOCATION
  93. CARLA_DECLARE_NON_COPY_CLASS(ScopedLocker)
  94. };
  95. /*
  96. * Helper class to unlock&lock a mutex during a function scope.
  97. */
  98. class ScopedUnlocker
  99. {
  100. public:
  101. ScopedUnlocker(CarlaMutex& mutex) noexcept
  102. : fMutex(mutex)
  103. {
  104. fMutex.unlock();
  105. }
  106. ~ScopedUnlocker() noexcept
  107. {
  108. fMutex.lock();
  109. }
  110. private:
  111. CarlaMutex& fMutex;
  112. CARLA_PREVENT_HEAP_ALLOCATION
  113. CARLA_DECLARE_NON_COPY_CLASS(ScopedUnlocker)
  114. };
  115. private:
  116. mutable pthread_mutex_t fMutex; // The mutex
  117. mutable volatile bool fTryLockWasCalled; // true if "tryLock()" was called at least once
  118. CARLA_PREVENT_HEAP_ALLOCATION
  119. CARLA_DECLARE_NON_COPY_CLASS(CarlaMutex)
  120. };
  121. // -----------------------------------------------------------------------
  122. #endif // CARLA_MUTEX_HPP_INCLUDED