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.

95 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
  43. #include <ctime>
  44. #include <semaphore.h>
  45. #include <sys/time.h>
  46. bool jackbridge_sem_init(void* sem)
  47. {
  48. return (sem_init((sem_t*)sem, 1, 0) == 0);
  49. }
  50. bool jackbridge_sem_destroy(void* sem)
  51. {
  52. return (sem_destroy((sem_t*)sem) == 0);
  53. }
  54. bool jackbridge_sem_post(void* sem)
  55. {
  56. return (sem_post((sem_t*)sem) == 0);
  57. }
  58. bool jackbridge_sem_timedwait(void* sem, int secs)
  59. {
  60. # ifdef CARLA_OS_MAC
  61. alarm(secs);
  62. return (sem_wait((sem_t*)sem) == 0);
  63. # else
  64. timespec timeout;
  65. # ifdef CARLA_OS_WIN
  66. timeval now;
  67. gettimeofday(&now, nullptr);
  68. timeout.tv_sec = now.tv_sec;
  69. timeout.tv_nsec = now.tv_usec * 1000;
  70. # else
  71. clock_gettime(CLOCK_REALTIME, &timeout);
  72. # endif
  73. timeout.tv_sec += secs;
  74. return (sem_timedwait((sem_t*)sem, &timeout) == 0);
  75. # endif
  76. }
  77. #endif
  78. // -----------------------------------------------------------------------------