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.

CarlaSemUtils.hpp 5.9KB

10 years ago
10 years ago
10 years ago
10 years ago
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Carla semaphore utils
  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_SEM_UTILS_HPP_INCLUDED
  18. #define CARLA_SEM_UTILS_HPP_INCLUDED
  19. #include "CarlaUtils.hpp"
  20. #include <ctime>
  21. #if defined(CARLA_OS_LINUX) && ! defined(STOAT_TEST_BUILD)
  22. # define CARLA_USE_FUTEXES 1
  23. #endif
  24. #if defined(CARLA_OS_WIN)
  25. # ifdef __WINE__
  26. # error Wine is not supposed to use this!
  27. # endif
  28. struct carla_sem_t { HANDLE handle; };
  29. #elif defined(CARLA_OS_MAC)
  30. # include <cerrno>
  31. #define UL_COMPARE_AND_WAIT 1
  32. #define UL_COMPARE_AND_WAIT_SHARED 3
  33. #define ULF_NO_ERRNO 0x01000000
  34. extern "C" {
  35. int __ulock_wait(uint32_t operation, void* addr, uint64_t value, uint32_t timeout_us);
  36. int __ulock_wake(uint32_t operation, void* addr, uint64_t value);
  37. }
  38. struct carla_sem_t { int count; bool external; };
  39. #elif defined(CARLA_USE_FUTEXES)
  40. # include <cerrno>
  41. # include <syscall.h>
  42. # include <sys/time.h>
  43. # include <linux/futex.h>
  44. struct carla_sem_t { int count; bool external; };
  45. #else
  46. # include <cerrno>
  47. # include <semaphore.h>
  48. # include <sys/time.h>
  49. # include <sys/types.h>
  50. struct carla_sem_t { sem_t sem; };
  51. #endif
  52. /*
  53. * Create a new semaphore, pre-allocated version.
  54. */
  55. static inline
  56. bool carla_sem_create2(carla_sem_t& sem, const bool externalIPC) noexcept
  57. {
  58. carla_zeroStruct(sem);
  59. #if defined(CARLA_OS_WIN)
  60. SECURITY_ATTRIBUTES sa;
  61. carla_zeroStruct(sa);
  62. sa.nLength = sizeof(SECURITY_ATTRIBUTES);
  63. sa.bInheritHandle = TRUE;
  64. sem.handle = ::CreateSemaphoreA(externalIPC ? &sa : nullptr, 0, 1, nullptr);
  65. return (sem.handle != INVALID_HANDLE_VALUE);
  66. #elif defined(CARLA_OS_MAC) || defined(CARLA_USE_FUTEXES)
  67. sem.external = externalIPC;
  68. return true;
  69. #else
  70. return (::sem_init(&sem.sem, externalIPC, 0) == 0);
  71. #endif
  72. }
  73. /*
  74. * Create a new semaphore.
  75. */
  76. static inline
  77. carla_sem_t* carla_sem_create(const bool externalIPC) noexcept
  78. {
  79. if (carla_sem_t* const sem = (carla_sem_t*)std::malloc(sizeof(carla_sem_t)))
  80. {
  81. if (carla_sem_create2(*sem, externalIPC))
  82. return sem;
  83. std::free(sem);
  84. }
  85. return nullptr;
  86. }
  87. /*
  88. * Destroy a semaphore, pre-allocated version.
  89. */
  90. static inline
  91. void carla_sem_destroy2(carla_sem_t& sem) noexcept
  92. {
  93. #if defined(CARLA_OS_WIN)
  94. ::CloseHandle(sem.handle);
  95. #elif defined(CARLA_OS_MAC) || defined(CARLA_USE_FUTEXES)
  96. // nothing to do
  97. #else
  98. ::sem_destroy(&sem.sem);
  99. #endif
  100. carla_zeroStruct(sem);
  101. }
  102. /*
  103. * Destroy a semaphore.
  104. */
  105. static inline
  106. void carla_sem_destroy(carla_sem_t* const sem) noexcept
  107. {
  108. CARLA_SAFE_ASSERT_RETURN(sem != nullptr,);
  109. carla_sem_destroy2(*sem);
  110. std::free(sem);
  111. }
  112. /*
  113. * Post semaphore (unlock).
  114. */
  115. static inline
  116. void carla_sem_post(carla_sem_t& sem) noexcept
  117. {
  118. #ifdef CARLA_OS_WIN
  119. ::ReleaseSemaphore(sem.handle, 1, nullptr);
  120. #elif defined(CARLA_OS_MAC)
  121. const bool unlocked = __sync_bool_compare_and_swap(&sem.count, 0, 1);
  122. CARLA_SAFE_ASSERT_RETURN(unlocked,);
  123. __ulock_wake(ULF_NO_ERRNO | (sem.external ? UL_COMPARE_AND_WAIT_SHARED : UL_COMPARE_AND_WAIT), &sem.count, 0);
  124. #elif defined(CARLA_USE_FUTEXES)
  125. const bool unlocked = __sync_bool_compare_and_swap(&sem.count, 0, 1);
  126. CARLA_SAFE_ASSERT_RETURN(unlocked,);
  127. ::syscall(__NR_futex, &sem.count, sem.external ? FUTEX_WAKE : FUTEX_WAKE_PRIVATE, 1, nullptr, nullptr, 0);
  128. #else
  129. ::sem_post(&sem.sem);
  130. #endif
  131. }
  132. #ifndef CARLA_OS_WASM
  133. /*
  134. * Wait for a semaphore (lock).
  135. */
  136. static inline
  137. bool carla_sem_timedwait(carla_sem_t& sem, const uint msecs) noexcept
  138. {
  139. CARLA_SAFE_ASSERT_RETURN(msecs > 0, false);
  140. #if defined(CARLA_OS_WIN)
  141. return (::WaitForSingleObject(sem.handle, msecs) == WAIT_OBJECT_0);
  142. #elif defined(CARLA_OS_MAC)
  143. const uint32_t timeout = msecs * 1000;
  144. for (;;)
  145. {
  146. if (__sync_bool_compare_and_swap(&sem.count, 1, 0))
  147. return true;
  148. if (__ulock_wait(sem.external ? UL_COMPARE_AND_WAIT_SHARED : UL_COMPARE_AND_WAIT, &sem.count, 0, timeout) != 0)
  149. if (errno != EAGAIN && errno != EINTR)
  150. return false;
  151. }
  152. #elif defined(CARLA_USE_FUTEXES)
  153. const uint secs = msecs / 1000;
  154. const uint nsecs = (msecs % 1000) * 1000000;
  155. const timespec timeout = { static_cast<time_t>(secs), static_cast<long>(nsecs) };
  156. for (;;)
  157. {
  158. if (__sync_bool_compare_and_swap(&sem.count, 1, 0))
  159. return true;
  160. if (::syscall(__NR_futex, &sem.count, sem.external ? FUTEX_WAIT : FUTEX_WAIT_PRIVATE, 0, &timeout, nullptr, 0) != 0)
  161. if (errno != EAGAIN && errno != EINTR)
  162. return false;
  163. }
  164. #else
  165. if (::sem_trywait(&sem.sem) == 0)
  166. return true;
  167. timespec now;
  168. ::clock_gettime(CLOCK_REALTIME, &now);
  169. const uint secs = msecs / 1000;
  170. const uint nsecs = (msecs % 1000) * 1000000;
  171. const timespec delta = { static_cast<time_t>(secs), static_cast<long>(nsecs) };
  172. /* */ timespec end = { now.tv_sec + delta.tv_sec, now.tv_nsec + delta.tv_nsec };
  173. if (end.tv_nsec >= 1000000000L) {
  174. ++end.tv_sec;
  175. end.tv_nsec -= 1000000000L;
  176. }
  177. for (int ret;;)
  178. {
  179. try {
  180. ret = sem_timedwait(&sem.sem, &end);
  181. } CARLA_SAFE_EXCEPTION_RETURN("carla_sem_timedwait", false);
  182. if (ret == 0)
  183. return true;
  184. if (errno != EAGAIN && errno != EINTR)
  185. return false;
  186. }
  187. #endif
  188. }
  189. #endif
  190. // -----------------------------------------------------------------------
  191. #endif // CARLA_SEM_UTILS_HPP_INCLUDED