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.

101 lines
2.2KB

  1. /*
  2. * Carla Mutex
  3. * Copyright (C) 2013 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 "CarlaJuceUtils.hpp"
  20. #include <pthread.h>
  21. // -----------------------------------------------------------------------
  22. // CarlaMutex class
  23. class CarlaMutex
  24. {
  25. public:
  26. CarlaMutex()
  27. : fTryLockWasCalled(false)
  28. {
  29. pthread_mutex_init(&fMutex, nullptr);
  30. }
  31. ~CarlaMutex()
  32. {
  33. pthread_mutex_destroy(&fMutex);
  34. }
  35. void lock()
  36. {
  37. pthread_mutex_lock(&fMutex);
  38. }
  39. bool tryLock()
  40. {
  41. fTryLockWasCalled = true;
  42. return (pthread_mutex_trylock(&fMutex) == 0);
  43. }
  44. void unlock(/*const bool resetTryLock*/)
  45. {
  46. //if (resetTryLock)
  47. // fTryLockWasCalled = false;
  48. pthread_mutex_unlock(&fMutex);
  49. }
  50. bool wasTryLockCalled()
  51. {
  52. const bool ret(fTryLockWasCalled);
  53. fTryLockWasCalled = false;
  54. return ret;
  55. }
  56. class ScopedLocker
  57. {
  58. public:
  59. ScopedLocker(CarlaMutex& mutex)
  60. : fMutex(mutex)
  61. {
  62. fMutex.lock();
  63. }
  64. ~ScopedLocker()
  65. {
  66. fMutex.unlock();
  67. }
  68. private:
  69. CarlaMutex& fMutex;
  70. CARLA_PREVENT_HEAP_ALLOCATION
  71. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ScopedLocker)
  72. };
  73. private:
  74. pthread_mutex_t fMutex;
  75. volatile bool fTryLockWasCalled;
  76. CARLA_PREVENT_HEAP_ALLOCATION
  77. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaMutex)
  78. };
  79. // -----------------------------------------------------------------------
  80. #endif // CARLA_MUTEX_HPP_INCLUDED