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.

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