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.

262 lines
7.0KB

  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 <mach/mach.h>
  31. # include <mach/semaphore.h>
  32. # include <servers/bootstrap.h>
  33. struct carla_sem_t { char bootname[32]; semaphore_t sem; semaphore_t sem2; };
  34. #elif defined(CARLA_USE_FUTEXES)
  35. # include <cerrno>
  36. # include <syscall.h>
  37. # include <sys/time.h>
  38. # include <linux/futex.h>
  39. struct carla_sem_t { int count; bool external; };
  40. #else
  41. # include <cerrno>
  42. # include <semaphore.h>
  43. # include <sys/time.h>
  44. # include <sys/types.h>
  45. struct carla_sem_t { sem_t sem; };
  46. #endif
  47. /*
  48. * Create a new semaphore, pre-allocated version.
  49. */
  50. static inline
  51. bool carla_sem_create2(carla_sem_t& sem, const bool externalIPC) noexcept
  52. {
  53. carla_zeroStruct(sem);
  54. #if defined(CARLA_OS_WIN)
  55. SECURITY_ATTRIBUTES sa;
  56. carla_zeroStruct(sa);
  57. sa.nLength = sizeof(SECURITY_ATTRIBUTES);
  58. sa.bInheritHandle = TRUE;
  59. sem.handle = ::CreateSemaphoreA(externalIPC ? &sa : nullptr, 0, 1, nullptr);
  60. return (sem.handle != INVALID_HANDLE_VALUE);
  61. #elif defined(CARLA_OS_MAC)
  62. mach_port_t bootport;
  63. const mach_port_t task = ::mach_task_self();
  64. if (externalIPC) {
  65. CARLA_SAFE_ASSERT_RETURN(task_get_bootstrap_port(task, &bootport) == KERN_SUCCESS, false);
  66. }
  67. CARLA_SAFE_ASSERT_RETURN(::semaphore_create(task, &sem.sem, SYNC_POLICY_FIFO, 0) == KERN_SUCCESS, false);
  68. if (! externalIPC)
  69. return true;
  70. static int bootcounter = 0;
  71. std::snprintf(sem.bootname, 31, "crlsm_%i_%i_%p", ++bootcounter, ::getpid(), &sem);
  72. sem.bootname[31] = '\0';
  73. #pragma clang diagnostic push
  74. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  75. if (::bootstrap_register(bootport, sem.bootname, sem.sem) == KERN_SUCCESS)
  76. #pragma clang diagnostic pop
  77. return true;
  78. sem.bootname[0] = '\0';
  79. ::semaphore_destroy(task, sem.sem);
  80. return false;
  81. #elif defined(CARLA_USE_FUTEXES)
  82. sem.external = externalIPC;
  83. return true;
  84. #else
  85. return (::sem_init(&sem.sem, externalIPC, 0) == 0);
  86. #endif
  87. }
  88. /*
  89. * Create a new semaphore.
  90. */
  91. static inline
  92. carla_sem_t* carla_sem_create(const bool externalIPC) noexcept
  93. {
  94. if (carla_sem_t* const sem = (carla_sem_t*)std::malloc(sizeof(carla_sem_t)))
  95. {
  96. if (carla_sem_create2(*sem, externalIPC))
  97. return sem;
  98. std::free(sem);
  99. }
  100. return nullptr;
  101. }
  102. /*
  103. * Destroy a semaphore, pre-allocated version.
  104. */
  105. static inline
  106. void carla_sem_destroy2(carla_sem_t& sem) noexcept
  107. {
  108. #if defined(CARLA_OS_WIN)
  109. ::CloseHandle(sem.handle);
  110. #elif defined(CARLA_OS_MAC)
  111. ::semaphore_destroy(mach_task_self(), sem.sem);
  112. #elif defined(CARLA_USE_FUTEXES)
  113. // nothing to do
  114. #else
  115. ::sem_destroy(&sem.sem);
  116. #endif
  117. carla_zeroStruct(sem);
  118. }
  119. /*
  120. * Destroy a semaphore.
  121. */
  122. static inline
  123. void carla_sem_destroy(carla_sem_t* const sem) noexcept
  124. {
  125. CARLA_SAFE_ASSERT_RETURN(sem != nullptr,);
  126. carla_sem_destroy2(*sem);
  127. std::free(sem);
  128. }
  129. /*
  130. * Connect to semaphore.
  131. * Used only on macOS for a client to connect to a server.
  132. */
  133. static inline
  134. bool carla_sem_connect(carla_sem_t& sem) noexcept
  135. {
  136. #ifdef CARLA_OS_MAC
  137. mach_port_t bootport;
  138. CARLA_SAFE_ASSERT_RETURN(task_get_bootstrap_port(mach_task_self(), &bootport) == KERN_SUCCESS, false);
  139. try {
  140. return (::bootstrap_look_up(bootport, sem.bootname, &sem.sem2) == KERN_SUCCESS);
  141. } CARLA_SAFE_EXCEPTION_RETURN("carla_sem_connect", false);
  142. #else
  143. // nothing to do
  144. return true;
  145. // unused
  146. (void)sem;
  147. #endif
  148. }
  149. /*
  150. * Post semaphore (unlock).
  151. */
  152. static inline
  153. void carla_sem_post(carla_sem_t& sem, const bool server = true) noexcept
  154. {
  155. #ifdef CARLA_OS_WIN
  156. ::ReleaseSemaphore(sem.handle, 1, nullptr);
  157. #elif defined(CARLA_OS_MAC)
  158. try {
  159. ::semaphore_signal(server ? sem.sem : sem.sem2);
  160. } CARLA_SAFE_EXCEPTION_RETURN("carla_sem_post",);
  161. #elif defined(CARLA_USE_FUTEXES)
  162. const bool unlocked = __sync_bool_compare_and_swap(&sem.count, 0, 1);
  163. CARLA_SAFE_ASSERT_RETURN(unlocked,);
  164. ::syscall(__NR_futex, &sem.count, sem.external ? FUTEX_WAKE : FUTEX_WAKE_PRIVATE, 1, nullptr, nullptr, 0);
  165. #else
  166. ::sem_post(&sem.sem);
  167. #endif
  168. // may be unused
  169. return; (void)server;
  170. }
  171. #ifndef CARLA_OS_WASM
  172. /*
  173. * Wait for a semaphore (lock).
  174. */
  175. static inline
  176. bool carla_sem_timedwait(carla_sem_t& sem, const uint msecs, const bool server = true) noexcept
  177. {
  178. CARLA_SAFE_ASSERT_RETURN(msecs > 0, false);
  179. #if defined(CARLA_OS_WIN)
  180. return (::WaitForSingleObject(sem.handle, msecs) == WAIT_OBJECT_0);
  181. #else
  182. const uint secs = msecs / 1000;
  183. const uint nsecs = (msecs % 1000) * 1000000;
  184. # if defined(CARLA_OS_MAC)
  185. const mach_timespec timeout = { secs, static_cast<int>(nsecs) };
  186. try {
  187. return (::semaphore_timedwait(server ? sem.sem : sem.sem2, timeout) == KERN_SUCCESS);
  188. } CARLA_SAFE_EXCEPTION_RETURN("carla_sem_timedwait", false);
  189. # elif defined(CARLA_USE_FUTEXES)
  190. const timespec timeout = { static_cast<time_t>(secs), static_cast<long>(nsecs) };
  191. for (;;)
  192. {
  193. if (__sync_bool_compare_and_swap(&sem.count, 1, 0))
  194. return true;
  195. if (::syscall(__NR_futex, &sem.count, sem.external ? FUTEX_WAIT : FUTEX_WAIT_PRIVATE, 0, &timeout, nullptr, 0) != 0)
  196. if (errno != EAGAIN && errno != EINTR)
  197. return false;
  198. }
  199. # else
  200. if (::sem_trywait(&sem.sem) == 0)
  201. return true;
  202. timespec now;
  203. ::clock_gettime(CLOCK_REALTIME, &now);
  204. const timespec delta = { static_cast<time_t>(secs), static_cast<long>(nsecs) };
  205. /* */ timespec end = { now.tv_sec + delta.tv_sec, now.tv_nsec + delta.tv_nsec };
  206. if (end.tv_nsec >= 1000000000L) {
  207. ++end.tv_sec;
  208. end.tv_nsec -= 1000000000L;
  209. }
  210. for (int ret;;)
  211. {
  212. try {
  213. ret = sem_timedwait(&sem.sem, &end);
  214. } CARLA_SAFE_EXCEPTION_RETURN("carla_sem_timedwait", false);
  215. if (ret == 0)
  216. return true;
  217. if (errno != EINTR)
  218. return false;
  219. }
  220. # endif
  221. #endif
  222. // may be unused
  223. (void)server;
  224. }
  225. #endif
  226. // -----------------------------------------------------------------------
  227. #endif // CARLA_SEM_UTILS_HPP_INCLUDED