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.

113 lines
2.2KB

  1. /*
  2. * Carla common utils
  3. * Copyright (C) 2011-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 GPL.txt file
  16. */
  17. #ifndef __CARLA_MUTEX_HPP__
  18. #define __CARLA_MUTEX_HPP__
  19. #include "CarlaJuceUtils.hpp"
  20. // #define CPP11_MUTEX
  21. #ifdef CPP11_MUTEX
  22. # include <mutex>
  23. #else
  24. # include <pthread.h>
  25. #endif
  26. // -------------------------------------------------
  27. // CarlaMutex class
  28. class CarlaMutex
  29. {
  30. public:
  31. CarlaMutex()
  32. {
  33. #ifndef CPP11_MUTEX
  34. pthread_mutex_init(&pmutex, nullptr);
  35. #endif
  36. }
  37. ~CarlaMutex()
  38. {
  39. #ifndef CPP11_MUTEX
  40. pthread_mutex_destroy(&pmutex);
  41. #endif
  42. }
  43. void lock()
  44. {
  45. #ifdef CPP11_MUTEX
  46. cmutex.lock();
  47. #else
  48. pthread_mutex_lock(&pmutex);
  49. #endif
  50. }
  51. bool tryLock()
  52. {
  53. #ifdef CPP11_MUTEX
  54. return cmutex.try_lock();
  55. #else
  56. return (pthread_mutex_trylock(&pmutex) == 0);
  57. #endif
  58. }
  59. void unlock()
  60. {
  61. #ifdef CPP11_MUTEX
  62. cmutex.unlock();
  63. #else
  64. pthread_mutex_unlock(&pmutex);
  65. #endif
  66. }
  67. class ScopedLocker
  68. {
  69. public:
  70. ScopedLocker(CarlaMutex* const mutex)
  71. : fMutex(mutex)
  72. {
  73. fMutex->lock();
  74. }
  75. ~ScopedLocker()
  76. {
  77. fMutex->unlock();
  78. }
  79. private:
  80. CarlaMutex* const fMutex;
  81. CARLA_PREVENT_HEAP_ALLOCATION
  82. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ScopedLocker)
  83. };
  84. private:
  85. #ifdef CPP11_MUTEX
  86. std::mutex cmutex;
  87. #else
  88. pthread_mutex_t pmutex;
  89. #endif
  90. CARLA_PREVENT_HEAP_ALLOCATION
  91. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaMutex)
  92. };
  93. // -------------------------------------------------
  94. #endif // __CARLA_MUTEX_HPP__