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.

156 lines
3.6KB

  1. /*
  2. * JackBridge (Part 2, Semaphore functions)
  3. * Copyright (C) 2013 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);
  25. CARLA_EXPORT bool jackbridge_shm_is_valid(void* shm);
  26. CARLA_EXPORT void jackbridge_shm_init(void* shm);
  27. CARLA_EXPORT void jackbridge_shm_attach(void* shm, const char* name);
  28. CARLA_EXPORT void jackbridge_shm_close(void* shm);
  29. CARLA_EXPORT void* jackbridge_shm_map(void* shm, size_t size);
  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)
  46. {
  47. return false;
  48. }
  49. bool jackbridge_shm_is_valid(void*)
  50. {
  51. return false;
  52. }
  53. void jackbridge_shm_init(void*)
  54. {
  55. }
  56. void jackbridge_shm_attach(void*, const char*)
  57. {
  58. }
  59. void jackbridge_shm_close(void*)
  60. {
  61. }
  62. void* jackbridge_shm_map(void*, size_t)
  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)
  84. {
  85. # ifdef CARLA_OS_MAC
  86. alarm(secs);
  87. return (sem_wait((sem_t*)sem) == 0);
  88. # else
  89. timespec timeout;
  90. # ifdef CARLA_OS_WIN
  91. timeval now;
  92. gettimeofday(&now, nullptr);
  93. timeout.tv_sec = now.tv_sec;
  94. timeout.tv_nsec = now.tv_usec * 1000;
  95. # else
  96. clock_gettime(CLOCK_REALTIME, &timeout);
  97. # endif
  98. timeout.tv_sec += secs;
  99. return (sem_timedwait((sem_t*)sem, &timeout) == 0);
  100. # endif
  101. }
  102. bool jackbridge_shm_is_valid(void* shm)
  103. {
  104. shm_t* t = (shm_t*)shm;
  105. return carla_is_shm_valid(*t);
  106. }
  107. void jackbridge_shm_init(void* shm)
  108. {
  109. shm_t* t = (shm_t*)shm;
  110. carla_shm_init(*t);
  111. }
  112. void jackbridge_shm_attach(void* shm, const char* name)
  113. {
  114. shm_t* t = (shm_t*)shm;
  115. *t = carla_shm_attach(name);
  116. }
  117. void jackbridge_shm_close(void* shm)
  118. {
  119. shm_t* t = (shm_t*)shm;
  120. carla_shm_close(*t);
  121. }
  122. void* jackbridge_shm_map(void* shm, size_t size)
  123. {
  124. shm_t* t = (shm_t*)shm;
  125. return carla_shm_map(*t, size);
  126. }
  127. #endif // ! JACKBRIDGE_DUMMY
  128. // -----------------------------------------------------------------------------