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.

249 lines
6.4KB

  1. /*
  2. * Carla semaphore utils
  3. * Copyright (C) 2013-2015 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. #ifdef CARLA_OS_LINUX
  22. # define CARLA_USE_FUTEXES
  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. */
  123. static inline
  124. bool carla_sem_connect(carla_sem_t& sem) noexcept
  125. {
  126. #ifdef CARLA_OS_MAC
  127. mach_port_t bootport;
  128. CARLA_SAFE_ASSERT_RETURN(task_get_bootstrap_port(mach_task_self(), &bootport) == KERN_SUCCESS, false);
  129. try {
  130. return (::bootstrap_look_up(bootport, sem.bootname, &sem.sem2) == KERN_SUCCESS);
  131. } CARLA_SAFE_EXCEPTION_RETURN("carla_sem_connect", false);
  132. #else
  133. // nothing to do
  134. return true;
  135. // unused
  136. (void)sem;
  137. #endif
  138. }
  139. /*
  140. * Post semaphore (unlock).
  141. */
  142. static inline
  143. void carla_sem_post(carla_sem_t& sem, const bool server) noexcept
  144. {
  145. #ifdef CARLA_OS_WIN
  146. ::ReleaseSemaphore(sem.handle, 1, nullptr);
  147. #elif defined(CARLA_OS_MAC)
  148. try {
  149. ::semaphore_signal(server ? sem.sem : sem.sem2);
  150. } CARLA_SAFE_EXCEPTION_RETURN("carla_sem_post",);
  151. #elif defined(CARLA_USE_FUTEXES)
  152. const bool unlocked = __sync_bool_compare_and_swap(&sem.count, 0, 1);
  153. CARLA_SAFE_ASSERT_RETURN(unlocked,);
  154. ::syscall(__NR_futex, &sem.count, FUTEX_WAKE, 1, nullptr, nullptr, 0);
  155. #else
  156. ::sem_post(&sem.sem);
  157. #endif
  158. // may be unused
  159. return; (void)server;
  160. }
  161. /*
  162. * Wait for a semaphore (lock).
  163. */
  164. static inline
  165. bool carla_sem_timedwait(carla_sem_t& sem, const uint msecs, const bool server) noexcept
  166. {
  167. CARLA_SAFE_ASSERT_RETURN(msecs > 0, false);
  168. #if defined(CARLA_OS_WIN)
  169. return (::WaitForSingleObject(sem.handle, msecs) == WAIT_OBJECT_0);
  170. #else
  171. const uint secs = msecs / 1000;
  172. const uint nsecs = (msecs % 1000) * 1000000;
  173. # if defined(CARLA_OS_MAC)
  174. const mach_timespec timeout = { secs, static_cast<int>(nsecs) };
  175. try {
  176. return (::semaphore_timedwait(server ? sem.sem : sem.sem2, timeout) == KERN_SUCCESS);
  177. } CARLA_SAFE_EXCEPTION_RETURN("carla_sem_timedwait", false);
  178. # elif defined(CARLA_USE_FUTEXES)
  179. const timespec timeout = { static_cast<time_t>(secs), static_cast<long>(nsecs) };
  180. for (;;)
  181. {
  182. if (__sync_bool_compare_and_swap(&sem.count, 1, 0))
  183. return true;
  184. if (::syscall(__NR_futex, &sem.count, FUTEX_WAIT, 0, &timeout, nullptr, 0) != 0)
  185. if (errno != EAGAIN && errno != EINTR)
  186. return false;
  187. }
  188. # else
  189. if (::sem_trywait(&sem.sem) == 0)
  190. return true;
  191. timespec now;
  192. ::clock_gettime(CLOCK_REALTIME, &now);
  193. const timespec delta = { static_cast<time_t>(secs), static_cast<long>(nsecs) };
  194. /* */ timespec end = { now.tv_sec + delta.tv_sec, now.tv_nsec + delta.tv_nsec };
  195. if (end.tv_nsec >= 1000000000L) {
  196. ++end.tv_sec;
  197. end.tv_nsec -= 1000000000L;
  198. }
  199. for (int ret;;)
  200. {
  201. try {
  202. ret = sem_timedwait(&sem.sem, &end);
  203. } CARLA_SAFE_EXCEPTION_RETURN("carla_sem_timedwait", false);
  204. if (ret == 0)
  205. return true;
  206. if (errno != EINTR)
  207. return false;
  208. }
  209. # endif
  210. #endif
  211. // may be unused
  212. (void)server;
  213. }
  214. // -----------------------------------------------------------------------
  215. #endif // CARLA_SEM_UTILS_HPP_INCLUDED