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.

177 lines
4.2KB

  1. /*
  2. * Carla semaphore utils
  3. * Copyright (C) 2013-2014 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 sem_t { HANDLE handle; };
  23. #else
  24. # include <sys/time.h>
  25. # include <sys/types.h>
  26. # include <semaphore.h>
  27. # ifdef CARLA_OS_MAC
  28. # include <fcntl.h>
  29. extern "C" {
  30. # include "osx_sem_timedwait.c"
  31. };
  32. # endif
  33. #endif
  34. /*
  35. * Create a new semaphore.
  36. */
  37. static inline
  38. sem_t* carla_sem_create() noexcept
  39. {
  40. #if defined(CARLA_OS_WIN)
  41. sem_t* const sem = (sem_t*)std::malloc(sizeof(sem_t));
  42. CARLA_SAFE_ASSERT_RETURN(sem != nullptr, nullptr);
  43. SECURITY_ATTRIBUTES sa;
  44. carla_zeroStruct(sa);
  45. sa.nLength = sizeof(SECURITY_ATTRIBUTES);
  46. sa.bInheritHandle = TRUE;
  47. sem->handle = ::CreateSemaphore(&sa, 0, 1, nullptr);
  48. return sem;
  49. #elif defined(CARLA_OS_MAC)
  50. static ulong sCounter = 0;
  51. ++sCounter;
  52. std::srand(static_cast<uint>(std::time(nullptr)));
  53. char strBuf[0xff+1];
  54. carla_zeroChar(strBuf, 0xff+1);
  55. std::snprintf(strBuf, 0xff, "carla-sem-%lu-%lu-%i", static_cast<ulong>(::getpid()), sCounter, std::rand());
  56. ::sem_unlink(strBuf);
  57. return ::sem_open(strBuf, O_CREAT, O_RDWR, 0);
  58. #else
  59. sem_t sem;
  60. if (::sem_init(&sem, 1, 0) != 0)
  61. return nullptr;
  62. // can't return temporary variable, so allocate a new one
  63. if (sem_t* const sem2 = (sem_t*)std::malloc(sizeof(sem_t)))
  64. {
  65. std::memcpy(sem2, &sem, sizeof(sem_t));
  66. return sem2;
  67. }
  68. ::sem_destroy(&sem);
  69. return nullptr;
  70. #endif
  71. }
  72. /*
  73. * Destroy a semaphore.
  74. */
  75. static inline
  76. void carla_sem_destroy(sem_t* const sem) noexcept
  77. {
  78. CARLA_SAFE_ASSERT_RETURN(sem != nullptr,);
  79. #if defined(CARLA_OS_WIN)
  80. ::CloseHandle(sem->handle);
  81. std::free(sem);
  82. #elif defined(CARLA_OS_MAC)
  83. ::sem_close(sem);
  84. #else
  85. // we can't call "sem_destroy(sem)" directly because it will free memory which we allocated during carla_sem_create()
  86. // so we create a temp variable, free our memory, and finally pass the temp variable to sem_destroy()
  87. // temp var
  88. sem_t sem2;
  89. std::memcpy(&sem2, sem, sizeof(sem_t));
  90. // destroy semaphore
  91. ::sem_destroy(&sem2);
  92. // free memory allocated in carla_sem_create()
  93. // FIXME
  94. //std::free(sem);
  95. #endif
  96. }
  97. /*
  98. * Post semaphore (unlock).
  99. */
  100. static inline
  101. bool carla_sem_post(sem_t* const sem) noexcept
  102. {
  103. CARLA_SAFE_ASSERT_RETURN(sem != nullptr, false);
  104. #ifdef CARLA_OS_WIN
  105. return (::ReleaseSemaphore(sem->handle, 1, nullptr) != FALSE);
  106. #else
  107. return (::sem_post(sem) == 0);
  108. #endif
  109. }
  110. /*
  111. * Wait for a semaphore (lock).
  112. */
  113. static inline
  114. bool carla_sem_timedwait(sem_t* const sem, const uint secs) noexcept
  115. {
  116. CARLA_SAFE_ASSERT_RETURN(sem != nullptr, false);
  117. CARLA_SAFE_ASSERT_RETURN(secs > 0, false);
  118. #if defined(CARLA_OS_WIN)
  119. const DWORD result = ::WaitForSingleObject(sem->handle, secs*1000);
  120. switch (result)
  121. {
  122. case WAIT_OBJECT_0:
  123. return true;
  124. case WAIT_TIMEOUT:
  125. errno = ETIMEDOUT;
  126. return false;
  127. default:
  128. errno = EINTR;
  129. return false;
  130. }
  131. #else
  132. timespec timeout;
  133. # ifdef CARLA_OS_LINUX
  134. ::clock_gettime(CLOCK_REALTIME, &timeout);
  135. # else
  136. timeval now;
  137. ::gettimeofday(&now, nullptr);
  138. timeout.tv_sec = now.tv_sec;
  139. timeout.tv_nsec = now.tv_usec * 1000;
  140. # endif
  141. timeout.tv_sec += static_cast<time_t>(secs);
  142. try {
  143. return (::sem_timedwait(sem, &timeout) == 0);
  144. } CARLA_SAFE_EXCEPTION_RETURN("sem_timedwait", false);
  145. #endif
  146. }
  147. // -----------------------------------------------------------------------
  148. #endif // CARLA_SEM_UTILS_HPP_INCLUDED