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 6.8KB

10 years ago
10 years ago
10 years ago
10 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Carla semaphore utils
  3. * Copyright (C) 2013-2018 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. if (::bootstrap_register(bootport, sem.bootname, sem.sem) == KERN_SUCCESS)
  74. return true;
  75. sem.bootname[0] = '\0';
  76. ::semaphore_destroy(task, sem.sem);
  77. return false;
  78. #elif defined(CARLA_USE_FUTEXES)
  79. sem.external = externalIPC;
  80. return true;
  81. #else
  82. return (::sem_init(&sem.sem, externalIPC, 0) == 0);
  83. #endif
  84. }
  85. /*
  86. * Create a new semaphore.
  87. */
  88. static inline
  89. carla_sem_t* carla_sem_create(const bool externalIPC) noexcept
  90. {
  91. if (carla_sem_t* const sem = (carla_sem_t*)std::malloc(sizeof(carla_sem_t)))
  92. {
  93. if (carla_sem_create2(*sem, externalIPC))
  94. return sem;
  95. std::free(sem);
  96. }
  97. return nullptr;
  98. }
  99. /*
  100. * Destroy a semaphore, pre-allocated version.
  101. */
  102. static inline
  103. void carla_sem_destroy2(carla_sem_t& sem) noexcept
  104. {
  105. #if defined(CARLA_OS_WIN)
  106. ::CloseHandle(sem.handle);
  107. #elif defined(CARLA_OS_MAC)
  108. ::semaphore_destroy(mach_task_self(), sem.sem);
  109. #elif defined(CARLA_USE_FUTEXES)
  110. // nothing to do
  111. #else
  112. ::sem_destroy(&sem.sem);
  113. #endif
  114. carla_zeroStruct(sem);
  115. }
  116. /*
  117. * Destroy a semaphore.
  118. */
  119. static inline
  120. void carla_sem_destroy(carla_sem_t* const sem) noexcept
  121. {
  122. CARLA_SAFE_ASSERT_RETURN(sem != nullptr,);
  123. carla_sem_destroy2(*sem);
  124. std::free(sem);
  125. }
  126. /*
  127. * Connect to semaphore.
  128. * Used only on macOS for a client to connect to a server.
  129. */
  130. static inline
  131. bool carla_sem_connect(carla_sem_t& sem) noexcept
  132. {
  133. #ifdef CARLA_OS_MAC
  134. mach_port_t bootport;
  135. CARLA_SAFE_ASSERT_RETURN(task_get_bootstrap_port(mach_task_self(), &bootport) == KERN_SUCCESS, false);
  136. try {
  137. return (::bootstrap_look_up(bootport, sem.bootname, &sem.sem2) == KERN_SUCCESS);
  138. } CARLA_SAFE_EXCEPTION_RETURN("carla_sem_connect", false);
  139. #else
  140. // nothing to do
  141. return true;
  142. // unused
  143. (void)sem;
  144. #endif
  145. }
  146. /*
  147. * Post semaphore (unlock).
  148. */
  149. static inline
  150. void carla_sem_post(carla_sem_t& sem, const bool server = true) noexcept
  151. {
  152. #ifdef CARLA_OS_WIN
  153. ::ReleaseSemaphore(sem.handle, 1, nullptr);
  154. #elif defined(CARLA_OS_MAC)
  155. try {
  156. ::semaphore_signal(server ? sem.sem : sem.sem2);
  157. } CARLA_SAFE_EXCEPTION_RETURN("carla_sem_post",);
  158. #elif defined(CARLA_USE_FUTEXES)
  159. const bool unlocked = __sync_bool_compare_and_swap(&sem.count, 0, 1);
  160. CARLA_SAFE_ASSERT_RETURN(unlocked,);
  161. ::syscall(__NR_futex, &sem.count, sem.external ? FUTEX_WAKE : FUTEX_WAKE_PRIVATE, 1, nullptr, nullptr, 0);
  162. #else
  163. ::sem_post(&sem.sem);
  164. #endif
  165. // may be unused
  166. return; (void)server;
  167. }
  168. /*
  169. * Wait for a semaphore (lock).
  170. */
  171. static inline
  172. bool carla_sem_timedwait(carla_sem_t& sem, const uint msecs, const bool server = true) noexcept
  173. {
  174. CARLA_SAFE_ASSERT_RETURN(msecs > 0, false);
  175. #if defined(CARLA_OS_WIN)
  176. return (::WaitForSingleObject(sem.handle, msecs) == WAIT_OBJECT_0);
  177. #else
  178. const uint secs = msecs / 1000;
  179. const uint nsecs = (msecs % 1000) * 1000000;
  180. # if defined(CARLA_OS_MAC)
  181. const mach_timespec timeout = { secs, static_cast<int>(nsecs) };
  182. try {
  183. return (::semaphore_timedwait(server ? sem.sem : sem.sem2, timeout) == KERN_SUCCESS);
  184. } CARLA_SAFE_EXCEPTION_RETURN("carla_sem_timedwait", false);
  185. # elif defined(CARLA_USE_FUTEXES)
  186. const timespec timeout = { static_cast<time_t>(secs), static_cast<long>(nsecs) };
  187. for (;;)
  188. {
  189. if (__sync_bool_compare_and_swap(&sem.count, 1, 0))
  190. return true;
  191. if (::syscall(__NR_futex, &sem.count, sem.external ? FUTEX_WAIT : FUTEX_WAIT_PRIVATE, 0, &timeout, nullptr, 0) != 0)
  192. if (errno != EAGAIN && errno != EINTR)
  193. return false;
  194. }
  195. # else
  196. if (::sem_trywait(&sem.sem) == 0)
  197. return true;
  198. timespec now;
  199. ::clock_gettime(CLOCK_REALTIME, &now);
  200. const timespec delta = { static_cast<time_t>(secs), static_cast<long>(nsecs) };
  201. /* */ timespec end = { now.tv_sec + delta.tv_sec, now.tv_nsec + delta.tv_nsec };
  202. if (end.tv_nsec >= 1000000000L) {
  203. ++end.tv_sec;
  204. end.tv_nsec -= 1000000000L;
  205. }
  206. for (int ret;;)
  207. {
  208. try {
  209. ret = sem_timedwait(&sem.sem, &end);
  210. } CARLA_SAFE_EXCEPTION_RETURN("carla_sem_timedwait", false);
  211. if (ret == 0)
  212. return true;
  213. if (errno != EINTR)
  214. return false;
  215. }
  216. # endif
  217. #endif
  218. // may be unused
  219. (void)server;
  220. }
  221. // -----------------------------------------------------------------------
  222. #endif // CARLA_SEM_UTILS_HPP_INCLUDED