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.

JackBridge2.cpp 4.0KB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 "JackBridge.hpp"
  17. #ifdef JACKBRIDGE_DUMMY
  18. # include "CarlaUtils.hpp"
  19. #else
  20. # include <ctime>
  21. # include <sys/time.h>
  22. # include <sys/types.h>
  23. # include <semaphore.h>
  24. # ifdef CARLA_OS_MAC
  25. extern "C" {
  26. # include "osx_sem_timedwait.c"
  27. }
  28. # endif
  29. # include "CarlaShmUtils.hpp"
  30. #endif // ! JACKBRIDGE_DUMMY
  31. // -----------------------------------------------------------------------------
  32. // TODO - check noexcept on OSX
  33. bool jackbridge_sem_init(void* sem) noexcept
  34. {
  35. #if defined(JACKBRIDGE_DUMMY)
  36. return false;
  37. #elif 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::sprintf(strBuf, "carla-sem-%i-%li", std::rand(), sCounter);
  44. sem_unlink(strBuf);
  45. sem_t* const sema = sem_open(strBuf, O_CREAT, O_RDWR, 0);
  46. std::memcpy(sem, &sema, sizeof(sem_t*));
  47. return (sema != nullptr && sema != SEM_FAILED);
  48. #else
  49. return (sem_init((sem_t*)sem, 1, 0) == 0);
  50. #endif
  51. }
  52. bool jackbridge_sem_destroy(void* sem) noexcept
  53. {
  54. #if defined(JACKBRIDGE_DUMMY)
  55. return false;
  56. #elif defined(CARLA_OS_MAC)
  57. return (sem_close(*(sem_t**)sem) == 0);
  58. #else
  59. return (sem_destroy((sem_t*)sem) == 0);
  60. #endif
  61. }
  62. bool jackbridge_sem_post(void* sem) noexcept
  63. {
  64. #ifdef JACKBRIDGE_DUMMY
  65. return false;
  66. #else
  67. # ifdef CARLA_OS_MAC
  68. sem_t* const sema = *(sem_t**)sem;
  69. # else
  70. sem_t* const sema = (sem_t*)sem;
  71. # endif
  72. return (sem_post(sema) == 0);
  73. #endif
  74. }
  75. bool jackbridge_sem_timedwait(void* sem, int secs) noexcept
  76. {
  77. CARLA_SAFE_ASSERT_RETURN(secs > 0, false);
  78. #ifdef JACKBRIDGE_DUMMY
  79. return false;
  80. #else
  81. # ifdef CARLA_OS_MAC
  82. sem_t* const sema = *(sem_t**)sem;
  83. # else
  84. sem_t* const sema = (sem_t*)sem;
  85. # endif
  86. timespec timeout;
  87. # ifdef CARLA_OS_LINUX
  88. clock_gettime(CLOCK_REALTIME, &timeout);
  89. # else
  90. timeval now;
  91. gettimeofday(&now, nullptr);
  92. timeout.tv_sec = now.tv_sec;
  93. timeout.tv_nsec = now.tv_usec * 1000;
  94. # endif
  95. timeout.tv_sec += secs;
  96. try {
  97. return (sem_timedwait(sema, &timeout) == 0);
  98. } CARLA_SAFE_EXCEPTION_RETURN("sem_timedwait", false);
  99. #endif
  100. }
  101. bool jackbridge_shm_is_valid(const void* shm) noexcept
  102. {
  103. CARLA_SAFE_ASSERT_RETURN(shm != nullptr, false);
  104. #ifdef JACKBRIDGE_DUMMY
  105. return false;
  106. #else
  107. return carla_is_shm_valid(*(const shm_t*)shm);
  108. #endif
  109. }
  110. void jackbridge_shm_init(void* shm) noexcept
  111. {
  112. CARLA_SAFE_ASSERT_RETURN(shm != nullptr,);
  113. #ifndef JACKBRIDGE_DUMMY
  114. carla_shm_init(*(shm_t*)shm);
  115. #endif
  116. }
  117. void jackbridge_shm_attach(void* shm, const char* name) noexcept
  118. {
  119. CARLA_SAFE_ASSERT_RETURN(shm != nullptr,);
  120. #ifndef JACKBRIDGE_DUMMY
  121. *(shm_t*)shm = carla_shm_attach(name);
  122. #endif
  123. }
  124. void jackbridge_shm_close(void* shm) noexcept
  125. {
  126. CARLA_SAFE_ASSERT_RETURN(shm != nullptr,);
  127. #ifndef JACKBRIDGE_DUMMY
  128. carla_shm_close(*(shm_t*)shm);
  129. #endif
  130. }
  131. void* jackbridge_shm_map(void* shm, size_t size) noexcept
  132. {
  133. CARLA_SAFE_ASSERT_RETURN(shm != nullptr, nullptr);
  134. #ifdef JACKBRIDGE_DUMMY
  135. return nullptr;
  136. #else
  137. return carla_shm_map(*(shm_t*)shm, size);
  138. #endif
  139. }
  140. // -----------------------------------------------------------------------------