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.

180 lines
4.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. #ifdef CARLA_OS_WIN
  22. struct carla_sem_t { HANDLE handle; };
  23. #elif defined(CARLA_OS_MAC)
  24. // TODO
  25. struct carla_sem_t { char dummy; };
  26. #else
  27. # include <sys/time.h>
  28. # include <sys/types.h>
  29. # include <semaphore.h>
  30. struct carla_sem_t { sem_t sem; };
  31. #endif
  32. /*
  33. * Create a new semaphore, pre-allocated version.
  34. */
  35. static inline
  36. bool carla_sem_create2(carla_sem_t& sem) noexcept
  37. {
  38. #if defined(CARLA_OS_WIN)
  39. SECURITY_ATTRIBUTES sa;
  40. carla_zeroStruct(sa);
  41. sa.nLength = sizeof(SECURITY_ATTRIBUTES);
  42. sa.bInheritHandle = TRUE;
  43. sem.handle = ::CreateSemaphore(&sa, 0, 1, nullptr);
  44. return (sem.handle != INVALID_HANDLE_VALUE);
  45. #elif defined(CARLA_OS_MAC)
  46. return false; // TODO
  47. #else
  48. return (::sem_init(&sem.sem, 1, 0) == 0);
  49. #endif
  50. }
  51. /*
  52. * Create a new semaphore.
  53. */
  54. static inline
  55. carla_sem_t* carla_sem_create() noexcept
  56. {
  57. if (carla_sem_t* const sem = (carla_sem_t*)std::malloc(sizeof(carla_sem_t)))
  58. {
  59. if (carla_sem_create2(*sem))
  60. return sem;
  61. std::free(sem);
  62. }
  63. return nullptr;
  64. }
  65. /*
  66. * Destroy a semaphore, pre-allocated version.
  67. */
  68. static inline
  69. void carla_sem_destroy2(carla_sem_t& sem) noexcept
  70. {
  71. #if defined(CARLA_OS_WIN)
  72. ::CloseHandle(sem.handle);
  73. #elif defined(CARLA_OS_MAC)
  74. // TODO
  75. #else
  76. ::sem_destroy(&sem.sem);
  77. #endif
  78. }
  79. /*
  80. * Destroy a semaphore.
  81. */
  82. static inline
  83. void carla_sem_destroy(carla_sem_t* const sem) noexcept
  84. {
  85. CARLA_SAFE_ASSERT_RETURN(sem != nullptr,);
  86. carla_sem_destroy2(*sem);
  87. std::free(sem);
  88. }
  89. /*
  90. * Post semaphore (unlock).
  91. */
  92. static inline
  93. void carla_sem_post(carla_sem_t& sem) noexcept
  94. {
  95. #ifdef CARLA_OS_WIN
  96. ::ReleaseSemaphore(sem.handle, 1, nullptr);
  97. #elif defined(CARLA_OS_MAC)
  98. // TODO
  99. #else
  100. ::sem_post(&sem.sem);
  101. #endif
  102. }
  103. /*
  104. * Wait for a semaphore (lock).
  105. */
  106. static inline
  107. bool carla_sem_timedwait(carla_sem_t& sem, const uint secs) noexcept
  108. {
  109. CARLA_SAFE_ASSERT_RETURN(secs > 0, false);
  110. #if defined(CARLA_OS_WIN)
  111. return (::WaitForSingleObject(sem.handle, secs*1000) == WAIT_OBJECT_0);
  112. #elif defined(CARLA_OS_MAC)
  113. // TODO
  114. #else
  115. if (::sem_trywait(&sem.sem) == 0)
  116. return true;
  117. # if 1
  118. /* NOTE:
  119. * This is very ugly!
  120. * While trying to fix 32bit-on-64bit issues I noticed this somehow gets a signal,
  121. * while that timedwait does not.
  122. * I'll keep this code for the time being, but only for TESTING!
  123. * It should revert to the proper code (below) when we know more info about the bug.
  124. */
  125. timespec timeout, now, step;
  126. step.tv_sec = 0;
  127. step.tv_nsec = 2;
  128. ::clock_gettime(CLOCK_REALTIME, &timeout);
  129. timeout.tv_sec += static_cast<time_t>(secs);
  130. for (;;)
  131. {
  132. ::nanosleep(&step, NULL);
  133. ::clock_gettime(CLOCK_REALTIME, &now);
  134. if (now.tv_sec >= timeout.tv_sec)
  135. return false;
  136. if (::sem_trywait(&sem.sem) == 0)
  137. return true;
  138. }
  139. //timeval now;
  140. //::gettimeofday(&now, nullptr);
  141. //timeout.tv_sec = now.tv_sec;
  142. //timeout.tv_nsec = now.tv_usec * 1000;
  143. # else
  144. timespec timeout;
  145. ::clock_gettime(CLOCK_REALTIME, &timeout);
  146. timeout.tv_sec += static_cast<time_t>(secs);
  147. try {
  148. return (::sem_timedwait(&sem.sem, &timeout) == 0);
  149. } CARLA_SAFE_EXCEPTION_RETURN("sem_timedwait", false);
  150. # endif
  151. #endif
  152. }
  153. // -----------------------------------------------------------------------
  154. #endif // CARLA_SEM_UTILS_HPP_INCLUDED