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 4.4KB

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