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.

151 lines
3.3KB

  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. timespec timeout;
  116. # ifdef CARLA_OS_LINUX
  117. ::clock_gettime(CLOCK_REALTIME, &timeout);
  118. # else
  119. timeval now;
  120. ::gettimeofday(&now, nullptr);
  121. timeout.tv_sec = now.tv_sec;
  122. timeout.tv_nsec = now.tv_usec * 1000;
  123. # endif
  124. timeout.tv_sec += static_cast<time_t>(secs);
  125. try {
  126. return (::sem_timedwait(&sem.sem, &timeout) == 0);
  127. } CARLA_SAFE_EXCEPTION_RETURN("sem_timedwait", false);
  128. #endif
  129. }
  130. // -----------------------------------------------------------------------
  131. #endif // CARLA_SEM_UTILS_HPP_INCLUDED