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.5KB

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