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.

137 lines
3.3KB

  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. #include <sys/time.h>
  22. #include <sys/types.h>
  23. #include <semaphore.h>
  24. #if defined(CARLA_OS_MAC)
  25. extern "C" {
  26. # include "osx_sem_timedwait.c"
  27. };
  28. #endif
  29. /*
  30. * Create a new semaphore.
  31. */
  32. static inline
  33. sem_t* carla_sem_create() noexcept
  34. {
  35. #if defined(CARLA_OS_MAC)
  36. static ulong sCounter = 0;
  37. ++sCounter;
  38. std::srand(static_cast<uint>(std::time(nullptr)));
  39. char strBuf[0xff+1];
  40. carla_zeroChar(strBuf, 0xff+1);
  41. std::snprintf(strBuf, 0xff, "carla-sem-%lu-%lu-%i", static_cast<ulong>(::getpid()), sCounter, std::rand());
  42. ::sem_unlink(strBuf);
  43. return ::sem_open(strBuf, O_CREAT, O_RDWR, 0);
  44. #else
  45. sem_t sem;
  46. if (::sem_init(&sem, 1, 0) != 0)
  47. return nullptr;
  48. // can't return temporary variable, so allocate a new one
  49. if (sem_t* const sem2 = (sem_t*)std::malloc(sizeof(sem_t)))
  50. {
  51. std::memcpy(sem2, &sem, sizeof(sem_t));
  52. return sem2;
  53. }
  54. ::sem_destroy(&sem);
  55. return nullptr;
  56. #endif
  57. }
  58. /*
  59. * Destroy a semaphore.
  60. */
  61. static inline
  62. void carla_sem_destroy(sem_t* const sem) noexcept
  63. {
  64. CARLA_SAFE_ASSERT_RETURN(sem != nullptr,);
  65. #if defined(CARLA_OS_MAC)
  66. ::sem_close(sem);
  67. #else
  68. // we can't call "sem_destroy(sem)" directly because it will free memory which we allocated during carla_sem_create()
  69. // so we create a temp variable, free our memory, and finally pass the temp variable to sem_destroy()
  70. // temp var
  71. sem_t sem2;
  72. std::memcpy(&sem2, sem, sizeof(sem_t));
  73. // free memory allocated in carla_sem_create()
  74. // FIXME
  75. //std::free(sem);
  76. // destroy semaphore
  77. ::sem_destroy(&sem2);
  78. #endif
  79. }
  80. /*
  81. * Post semaphore (unlock).
  82. */
  83. static inline
  84. bool carla_sem_post(sem_t* const sem) noexcept
  85. {
  86. CARLA_SAFE_ASSERT_RETURN(sem != nullptr, false);
  87. return (::sem_post(sem) == 0);
  88. }
  89. /*
  90. * Wait for a semaphore (lock).
  91. */
  92. static inline
  93. bool carla_sem_timedwait(sem_t* const sem, const uint secs) noexcept
  94. {
  95. CARLA_SAFE_ASSERT_RETURN(sem != nullptr, false);
  96. CARLA_SAFE_ASSERT_RETURN(secs > 0, false);
  97. timespec timeout;
  98. #ifdef CARLA_OS_LINUX
  99. ::clock_gettime(CLOCK_REALTIME, &timeout);
  100. #else
  101. timeval now;
  102. ::gettimeofday(&now, nullptr);
  103. timeout.tv_sec = now.tv_sec;
  104. timeout.tv_nsec = now.tv_usec * 1000;
  105. #endif
  106. timeout.tv_sec += static_cast<time_t>(secs);
  107. try {
  108. return (::sem_timedwait(sem, &timeout) == 0);
  109. } CARLA_SAFE_EXCEPTION_RETURN("sem_timedwait", false);
  110. }
  111. // -----------------------------------------------------------------------
  112. #endif // CARLA_SEM_UTILS_HPP_INCLUDED