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.

191 lines
4.5KB

  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. // TODO
  29. struct carla_sem_t { char dummy; };
  30. #elif defined(CARLA_USE_FUTEXES)
  31. # include <cerrno>
  32. # include <syscall.h>
  33. # include <sys/time.h>
  34. # include <linux/futex.h>
  35. struct carla_sem_t { int count; };
  36. #else
  37. # include <cerrno>
  38. # include <semaphore.h>
  39. # include <sys/time.h>
  40. # include <sys/types.h>
  41. struct carla_sem_t { sem_t sem; };
  42. #endif
  43. /*
  44. * Create a new semaphore, pre-allocated version.
  45. */
  46. static inline
  47. bool carla_sem_create2(carla_sem_t& sem) noexcept
  48. {
  49. carla_zeroStruct(sem);
  50. #if defined(CARLA_OS_WIN)
  51. SECURITY_ATTRIBUTES sa;
  52. carla_zeroStruct(sa);
  53. sa.nLength = sizeof(SECURITY_ATTRIBUTES);
  54. sa.bInheritHandle = TRUE;
  55. sem.handle = ::CreateSemaphore(&sa, 0, 1, nullptr);
  56. return (sem.handle != INVALID_HANDLE_VALUE);
  57. #elif defined(CARLA_OS_MAC)
  58. return false; // TODO
  59. #elif defined(CARLA_USE_FUTEXES)
  60. sem.count = 0;
  61. return true;
  62. #else
  63. return (::sem_init(&sem.sem, 1, 0) == 0);
  64. #endif
  65. }
  66. /*
  67. * Create a new semaphore.
  68. */
  69. static inline
  70. carla_sem_t* carla_sem_create() noexcept
  71. {
  72. if (carla_sem_t* const sem = (carla_sem_t*)std::malloc(sizeof(carla_sem_t)))
  73. {
  74. if (carla_sem_create2(*sem))
  75. return sem;
  76. std::free(sem);
  77. }
  78. return nullptr;
  79. }
  80. /*
  81. * Destroy a semaphore, pre-allocated version.
  82. */
  83. static inline
  84. void carla_sem_destroy2(carla_sem_t& sem) noexcept
  85. {
  86. #if defined(CARLA_OS_WIN)
  87. ::CloseHandle(sem.handle);
  88. #elif defined(CARLA_OS_MAC)
  89. // TODO
  90. #elif defined(CARLA_USE_FUTEXES)
  91. // nothing to do
  92. (void)sem;
  93. #else
  94. ::sem_destroy(&sem.sem);
  95. #endif
  96. }
  97. /*
  98. * Destroy a semaphore.
  99. */
  100. static inline
  101. void carla_sem_destroy(carla_sem_t* const sem) noexcept
  102. {
  103. CARLA_SAFE_ASSERT_RETURN(sem != nullptr,);
  104. carla_sem_destroy2(*sem);
  105. std::free(sem);
  106. }
  107. /*
  108. * Post semaphore (unlock).
  109. */
  110. static inline
  111. void carla_sem_post(carla_sem_t& sem) noexcept
  112. {
  113. #ifdef CARLA_OS_WIN
  114. ::ReleaseSemaphore(sem.handle, 1, nullptr);
  115. #elif defined(CARLA_OS_MAC)
  116. // TODO
  117. #elif defined(CARLA_USE_FUTEXES)
  118. const bool unlocked = __sync_bool_compare_and_swap(&sem.count, 0, 1);
  119. CARLA_SAFE_ASSERT_RETURN(unlocked,);
  120. ::syscall(__NR_futex, &sem.count, FUTEX_WAKE, 1, nullptr, nullptr, 0);
  121. #else
  122. ::sem_post(&sem.sem);
  123. #endif
  124. }
  125. /*
  126. * Wait for a semaphore (lock).
  127. */
  128. static inline
  129. bool carla_sem_timedwait(carla_sem_t& sem, const uint msecs) noexcept
  130. {
  131. CARLA_SAFE_ASSERT_RETURN(msecs > 0, false);
  132. #if defined(CARLA_OS_WIN)
  133. return (::WaitForSingleObject(sem.handle, secs*1000 + msecs) == WAIT_OBJECT_0);
  134. #elif defined(CARLA_OS_MAC)
  135. return false; // TODO
  136. #elif defined(CARLA_USE_FUTEXES)
  137. timespec timeout;
  138. timeout.tv_sec = static_cast<time_t>(msecs / 1000);
  139. timeout.tv_nsec = static_cast<time_t>(msecs % 1000);
  140. for (; ! __sync_bool_compare_and_swap(&sem.count, 1, 0);)
  141. {
  142. if (::syscall(__NR_futex, &sem.count, FUTEX_WAIT, 0, &timeout, nullptr, 0) == 0)
  143. {
  144. __sync_sub_and_fetch(&sem.count, 1);
  145. return true;
  146. }
  147. if (errno == EWOULDBLOCK)
  148. continue;
  149. return false;
  150. }
  151. return true;
  152. #else
  153. if (::sem_trywait(&sem.sem) == 0)
  154. return true;
  155. timespec timeout;
  156. ::clock_gettime(CLOCK_REALTIME, &timeout);
  157. timeout.tv_sec += static_cast<time_t>(msecs / 1000);
  158. timeout.tv_nsec += static_cast<time_t>(msecs % 1000);
  159. try {
  160. return (::sem_timedwait(&sem.sem, &timeout) == 0);
  161. } CARLA_SAFE_EXCEPTION_RETURN("sem_timedwait", false);
  162. #endif
  163. }
  164. // -----------------------------------------------------------------------
  165. #endif // CARLA_SEM_UTILS_HPP_INCLUDED