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.

158 lines
3.9KB

  1. /*
  2. * JackBridge (Part 2, Semaphore functions)
  3. * Copyright (C) 2013-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "CarlaDefines.h"
  17. #include "JackBridge.hpp"
  18. #include "CarlaShmUtils.hpp"
  19. #ifndef JACKBRIDGE_HPP_INCLUDED
  20. // don't include the whole JACK API in this file
  21. CARLA_EXPORT bool jackbridge_sem_init(void* sem) noexcept;
  22. CARLA_EXPORT bool jackbridge_sem_destroy(void* sem) noexcept;
  23. CARLA_EXPORT bool jackbridge_sem_post(void* sem) noexcept;
  24. CARLA_EXPORT bool jackbridge_sem_timedwait(void* sem, int secs) noexcept;
  25. CARLA_EXPORT bool jackbridge_shm_is_valid(const void* shm) noexcept;
  26. CARLA_EXPORT void jackbridge_shm_init(void* shm) noexcept;
  27. CARLA_EXPORT void jackbridge_shm_attach(void* shm, const char* name) noexcept;
  28. CARLA_EXPORT void jackbridge_shm_close(void* shm) noexcept;
  29. CARLA_EXPORT void* jackbridge_shm_map(void* shm, size_t size) noexcept;
  30. #endif
  31. // -----------------------------------------------------------------------------
  32. #ifdef JACKBRIDGE_DUMMY
  33. bool jackbridge_sem_init(void*) noexcept
  34. {
  35. return false;
  36. }
  37. bool jackbridge_sem_destroy(void*) noexcept
  38. {
  39. return false;
  40. }
  41. bool jackbridge_sem_post(void*) noexcept
  42. {
  43. return false;
  44. }
  45. bool jackbridge_sem_timedwait(void*, int) noexcept
  46. {
  47. return false;
  48. }
  49. bool jackbridge_shm_is_valid(const void*) noexcept
  50. {
  51. return false;
  52. }
  53. void jackbridge_shm_init(void*) noexcept
  54. {
  55. }
  56. void jackbridge_shm_attach(void*, const char*) noexcept
  57. {
  58. }
  59. void jackbridge_shm_close(void*) noexcept
  60. {
  61. }
  62. void* jackbridge_shm_map(void*, size_t) noexcept
  63. {
  64. return nullptr;
  65. }
  66. #else //JACKBRIDGE_DUMMY
  67. #include <ctime>
  68. #include <sys/time.h>
  69. #include <sys/types.h>
  70. #include <semaphore.h>
  71. bool jackbridge_sem_init(void* sem) noexcept
  72. {
  73. return (sem_init((sem_t*)sem, 1, 0) == 0);
  74. }
  75. bool jackbridge_sem_destroy(void* sem) noexcept
  76. {
  77. return (sem_destroy((sem_t*)sem) == 0);
  78. }
  79. bool jackbridge_sem_post(void* sem) noexcept
  80. {
  81. return (sem_post((sem_t*)sem) == 0);
  82. }
  83. bool jackbridge_sem_timedwait(void* sem, int secs) noexcept
  84. {
  85. CARLA_SAFE_ASSERT_RETURN(secs > 0, false);
  86. #ifdef CARLA_OS_MAC
  87. alarm(static_cast<uint>(secs));
  88. try {
  89. return (sem_wait((sem_t*)sem) == 0);
  90. } CARLA_SAFE_EXCEPTION_RETURN("sem_wait", false);
  91. #else
  92. timespec timeout;
  93. # ifdef CARLA_OS_WIN
  94. timeval now;
  95. gettimeofday(&now, nullptr);
  96. timeout.tv_sec = now.tv_sec;
  97. timeout.tv_nsec = now.tv_usec * 1000;
  98. # else
  99. clock_gettime(CLOCK_REALTIME, &timeout);
  100. # endif
  101. timeout.tv_sec += secs;
  102. try {
  103. return (sem_timedwait((sem_t*)sem, &timeout) == 0);
  104. } CARLA_SAFE_EXCEPTION_RETURN("sem_timedwait", false);
  105. #endif
  106. }
  107. bool jackbridge_shm_is_valid(const void* shm) noexcept
  108. {
  109. return carla_is_shm_valid(*(const shm_t*)shm);
  110. }
  111. void jackbridge_shm_init(void* shm) noexcept
  112. {
  113. carla_shm_init(*(shm_t*)shm);
  114. }
  115. void jackbridge_shm_attach(void* shm, const char* name) noexcept
  116. {
  117. *(shm_t*)shm = carla_shm_attach(name);
  118. }
  119. void jackbridge_shm_close(void* shm) noexcept
  120. {
  121. carla_shm_close(*(shm_t*)shm);
  122. }
  123. void* jackbridge_shm_map(void* shm, size_t size) noexcept
  124. {
  125. return carla_shm_map(*(shm_t*)shm, size);
  126. }
  127. #endif // ! JACKBRIDGE_DUMMY
  128. // -----------------------------------------------------------------------------