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.

124 lines
2.8KB

  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* const sem((sem_t*)std::malloc(sizeof(sem_t)));
  48. CARLA_SAFE_ASSERT_RETURN(sem != nullptr, nullptr);
  49. if (::sem_init(sem, 1, 0) != 0)
  50. {
  51. std::free(sem);
  52. return nullptr;
  53. }
  54. return sem;
  55. #endif
  56. }
  57. /*
  58. * Destroy a semaphore.
  59. */
  60. static inline
  61. void carla_sem_destroy(sem_t* const sem) noexcept
  62. {
  63. CARLA_SAFE_ASSERT_RETURN(sem != nullptr,);
  64. #if defined(CARLA_OS_MAC)
  65. ::sem_close(sem);
  66. #else
  67. ::sem_destroy(sem);
  68. std::free(sem);
  69. #endif
  70. }
  71. /*
  72. * Post semaphore (unlock).
  73. */
  74. static inline
  75. bool carla_sem_post(sem_t* const sem) noexcept
  76. {
  77. CARLA_SAFE_ASSERT_RETURN(sem != nullptr, false);
  78. return (::sem_post(sem) == 0);
  79. }
  80. /*
  81. * Wait for a semaphore (lock).
  82. */
  83. static inline
  84. bool carla_sem_timedwait(sem_t* const sem, const uint secs) noexcept
  85. {
  86. CARLA_SAFE_ASSERT_RETURN(sem != nullptr, false);
  87. CARLA_SAFE_ASSERT_RETURN(secs > 0, false);
  88. timespec timeout;
  89. #ifdef CARLA_OS_LINUX
  90. ::clock_gettime(CLOCK_REALTIME, &timeout);
  91. #else
  92. timeval now;
  93. ::gettimeofday(&now, nullptr);
  94. timeout.tv_sec = now.tv_sec;
  95. timeout.tv_nsec = now.tv_usec * 1000;
  96. #endif
  97. timeout.tv_sec += secs;
  98. try {
  99. return (::sem_timedwait(sem, &timeout) == 0);
  100. } CARLA_SAFE_EXCEPTION_RETURN("sem_timedwait", false);
  101. }
  102. // -----------------------------------------------------------------------
  103. #endif // CARLA_SEM_UTILS_HPP_INCLUDED