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.

125 lines
3.1KB

  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 <cerrno>
  21. # include "CarlaSemUtils.hpp"
  22. # include "CarlaShmUtils.hpp"
  23. #endif // ! JACKBRIDGE_DUMMY
  24. // -----------------------------------------------------------------------------
  25. bool jackbridge_sem_init(void* sem) noexcept
  26. {
  27. #if defined(JACKBRIDGE_DUMMY)
  28. return false;
  29. #else
  30. sem_t* const sema(carla_sem_create());
  31. CARLA_SAFE_ASSERT_RETURN(sema != nullptr, false);
  32. std::memcpy(sem, sema, sizeof(sem_t));
  33. return (sema != nullptr);
  34. #endif
  35. }
  36. void jackbridge_sem_destroy(void* sem) noexcept
  37. {
  38. #ifndef JACKBRIDGE_DUMMY
  39. carla_sem_destroy((sem_t*)sem);
  40. #endif
  41. }
  42. bool jackbridge_sem_post(void* sem) noexcept
  43. {
  44. #ifdef JACKBRIDGE_DUMMY
  45. return false;
  46. #else
  47. return carla_sem_post((sem_t*)sem);
  48. #endif
  49. }
  50. bool jackbridge_sem_timedwait(void* sem, uint secs, bool* timedOut) noexcept
  51. {
  52. CARLA_SAFE_ASSERT_RETURN(timedOut != nullptr, false);
  53. #ifdef JACKBRIDGE_DUMMY
  54. return false;
  55. #else
  56. if (carla_sem_timedwait((sem_t*)sem, secs))
  57. {
  58. *timedOut = false;
  59. return true;
  60. }
  61. *timedOut = (errno == ETIMEDOUT);
  62. return false;
  63. #endif
  64. }
  65. // -----------------------------------------------------------------------------
  66. bool jackbridge_shm_is_valid(const void* shm) noexcept
  67. {
  68. CARLA_SAFE_ASSERT_RETURN(shm != nullptr, false);
  69. #ifdef JACKBRIDGE_DUMMY
  70. return false;
  71. #else
  72. return carla_is_shm_valid(*(const shm_t*)shm);
  73. #endif
  74. }
  75. void jackbridge_shm_init(void* shm) noexcept
  76. {
  77. CARLA_SAFE_ASSERT_RETURN(shm != nullptr,);
  78. #ifndef JACKBRIDGE_DUMMY
  79. carla_shm_init(*(shm_t*)shm);
  80. #endif
  81. }
  82. void jackbridge_shm_attach(void* shm, const char* name) noexcept
  83. {
  84. CARLA_SAFE_ASSERT_RETURN(shm != nullptr,);
  85. #ifndef JACKBRIDGE_DUMMY
  86. *(shm_t*)shm = carla_shm_attach(name);
  87. #endif
  88. }
  89. void jackbridge_shm_close(void* shm) noexcept
  90. {
  91. CARLA_SAFE_ASSERT_RETURN(shm != nullptr,);
  92. #ifndef JACKBRIDGE_DUMMY
  93. carla_shm_close(*(shm_t*)shm);
  94. #endif
  95. }
  96. void* jackbridge_shm_map(void* shm, size_t size) noexcept
  97. {
  98. CARLA_SAFE_ASSERT_RETURN(shm != nullptr, nullptr);
  99. #ifdef JACKBRIDGE_DUMMY
  100. return nullptr;
  101. #else
  102. return carla_shm_map(*(shm_t*)shm, size);
  103. #endif
  104. }
  105. // -----------------------------------------------------------------------------