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.

241 lines
6.1KB

  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. #define CARLA_USE_FUTEXES
  22. #if defined(CARLA_OS_WIN)
  23. # ifdef __WINE__
  24. # error Wine is not supposed to use this!
  25. # endif
  26. struct carla_sem_t { HANDLE handle; };
  27. #elif defined(CARLA_OS_MAC)
  28. # include <mach/mach.h>
  29. # include <mach/semaphore.h>
  30. # include <servers/bootstrap.h>
  31. struct carla_sem_t { char bootname[32]; semaphore_t sem; semaphore_t sem2; };
  32. #elif defined(CARLA_USE_FUTEXES)
  33. # include <cerrno>
  34. # include <syscall.h>
  35. # include <sys/time.h>
  36. # include <linux/futex.h>
  37. struct carla_sem_t { int count; };
  38. #else
  39. # include <cerrno>
  40. # include <semaphore.h>
  41. # include <sys/time.h>
  42. # include <sys/types.h>
  43. struct carla_sem_t { sem_t sem; };
  44. #endif
  45. /*
  46. * Create a new semaphore, pre-allocated version.
  47. */
  48. static inline
  49. bool carla_sem_create2(carla_sem_t& sem) noexcept
  50. {
  51. carla_zeroStruct(sem);
  52. #if defined(CARLA_OS_WIN)
  53. SECURITY_ATTRIBUTES sa;
  54. carla_zeroStruct(sa);
  55. sa.nLength = sizeof(SECURITY_ATTRIBUTES);
  56. sa.bInheritHandle = TRUE;
  57. sem.handle = ::CreateSemaphore(&sa, 0, 1, nullptr);
  58. return (sem.handle != INVALID_HANDLE_VALUE);
  59. #elif defined(CARLA_OS_MAC)
  60. const mach_port_t task = mach_task_self();
  61. mach_port_t bootport;
  62. CARLA_SAFE_ASSERT_RETURN(task_get_bootstrap_port(task, &bootport) == KERN_SUCCESS, false);
  63. if (::semaphore_create(task, &sem.sem, SYNC_POLICY_FIFO, 0) == KERN_SUCCESS)
  64. {
  65. static int bootcounter = 0;
  66. std::snprintf(sem.bootname, 31, "crlsm_%i_%i_%p", ++bootcounter, getpid(), &sem);
  67. sem.bootname[31] = '\0';
  68. if (bootstrap_register(bootport, sem.bootname, sem.sem) == KERN_SUCCESS)
  69. return true;
  70. ::semaphore_destroy(task, sem.sem);
  71. }
  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, 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), 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 && errno != EWOULDBLOCK)
  185. return false;
  186. }
  187. # else
  188. if (::sem_trywait(&sem.sem) == 0)
  189. return true;
  190. timespec now;
  191. ::clock_gettime(CLOCK_REALTIME, &now);
  192. timespec delta = { secs, nsecs };
  193. timespec end = { now.tv_sec + delta.tv_sec, now.tv_nsec + delta.tv_nsec };
  194. if (end.tv_nsec >= 1000000000L) {
  195. ++end.tv_sec;
  196. end.tv_nsec -= 1000000000L;
  197. }
  198. try {
  199. return (::sem_timedwait(&sem.sem, &end) == 0);
  200. } CARLA_SAFE_EXCEPTION_RETURN("carla_sem_timedwait", false);
  201. # endif
  202. #endif
  203. // may be unused
  204. (void)server;
  205. }
  206. // -----------------------------------------------------------------------
  207. #endif // CARLA_SEM_UTILS_HPP_INCLUDED