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.

96 lines
2.5KB

  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. #ifndef JACKBRIDGE_HPP_INCLUDED
  19. // don't include the whole JACK API in this file
  20. CARLA_EXPORT bool jackbridge_sem_init(void* sem);
  21. CARLA_EXPORT bool jackbridge_sem_destroy(void* sem);
  22. CARLA_EXPORT bool jackbridge_sem_post(void* sem);
  23. CARLA_EXPORT bool jackbridge_sem_timedwait(void* sem, int secs);
  24. #endif
  25. // -----------------------------------------------------------------------------
  26. #if JACKBRIDGE_DUMMY
  27. bool jackbridge_sem_init(void*)
  28. {
  29. return false;
  30. }
  31. bool jackbridge_sem_destroy(void*)
  32. {
  33. return false;
  34. }
  35. bool jackbridge_sem_post(void*)
  36. {
  37. return false;
  38. }
  39. bool jackbridge_sem_timedwait(void*, int)
  40. {
  41. return false;
  42. }
  43. #else //JACKBRIDGE_DUMMY
  44. #include <ctime>
  45. #include <sys/time.h>
  46. #include <sys/types.h>
  47. #include <semaphore.h>
  48. bool jackbridge_sem_init(void* sem)
  49. {
  50. return (sem_init((sem_t*)sem, 1, 0) == 0);
  51. }
  52. bool jackbridge_sem_destroy(void* sem)
  53. {
  54. return (sem_destroy((sem_t*)sem) == 0);
  55. }
  56. bool jackbridge_sem_post(void* sem)
  57. {
  58. return (sem_post((sem_t*)sem) == 0);
  59. }
  60. bool jackbridge_sem_timedwait(void* sem, int secs)
  61. {
  62. # ifdef CARLA_OS_MAC
  63. alarm(secs);
  64. return (sem_wait((sem_t*)sem) == 0);
  65. # else
  66. timespec timeout;
  67. # ifdef CARLA_OS_WIN
  68. timeval now;
  69. gettimeofday(&now, nullptr);
  70. timeout.tv_sec = now.tv_sec;
  71. timeout.tv_nsec = now.tv_usec * 1000;
  72. # else
  73. clock_gettime(CLOCK_REALTIME, &timeout);
  74. # endif
  75. timeout.tv_sec += secs;
  76. return (sem_timedwait((sem_t*)sem, &timeout) == 0);
  77. # endif
  78. }
  79. #endif // JACKBRIDGE_DUMMY
  80. // -----------------------------------------------------------------------------