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

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