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.

94 lines
2.4KB

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