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.

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