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.2KB

  1. /*
  2. * JackBridge (Part 2, Semaphore functions)
  3. * Copyright (C) 2013 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Lesser General Public License for more details.
  13. *
  14. * For a full copy of the license see the LGPL.txt file
  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. // -----------------------------------------------------------------------------