jack2 codebase
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.

148 lines
4.5KB

  1. /*
  2. Copyright (C) 2004-2008 Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2.1 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #include "JackProcessSync.h"
  16. #include "JackError.h"
  17. namespace Jack
  18. {
  19. void JackProcessSync::Signal()
  20. {
  21. int res = pthread_cond_signal(&fCond);
  22. if (res != 0)
  23. jack_error("pthread_cond_signal error err = %s", strerror(res));
  24. }
  25. void JackProcessSync::LockedSignal()
  26. {
  27. int res;
  28. res = pthread_mutex_lock(&fMutex);
  29. if (res != 0)
  30. jack_error("pthread_mutex_lock error err = %s", strerror(res));
  31. res = pthread_cond_signal(&fCond);
  32. if (res != 0)
  33. jack_error("pthread_cond_signal error err = %s", strerror(res));
  34. res = pthread_mutex_unlock(&fMutex);
  35. if (res != 0)
  36. jack_error("pthread_mutex_unlock error err = %s", strerror(res));
  37. }
  38. void JackProcessSync::SignalAll()
  39. {
  40. int res = pthread_cond_broadcast(&fCond);
  41. if (res != 0)
  42. jack_error("pthread_cond_broadcast error err = %s", strerror(res));
  43. }
  44. void JackProcessSync::LockedSignalAll()
  45. {
  46. int res;
  47. res = pthread_mutex_lock(&fMutex);
  48. if (res != 0)
  49. jack_error("pthread_mutex_lock error err = %s", strerror(res));
  50. res = pthread_cond_broadcast(&fCond);
  51. if (res != 0)
  52. jack_error("pthread_cond_broadcast error err = %s", strerror(res));
  53. res = pthread_mutex_unlock(&fMutex);
  54. if (res != 0)
  55. jack_error("pthread_mutex_unlock error err = %s", strerror(res));
  56. }
  57. void JackProcessSync::Wait()
  58. {
  59. int res;
  60. if ((res = pthread_cond_wait(&fCond, &fMutex)) != 0)
  61. jack_error("pthread_cond_wait error err = %s", strerror(errno));
  62. }
  63. void JackProcessSync::LockedWait()
  64. {
  65. int res;
  66. res = pthread_mutex_lock(&fMutex);
  67. if (res != 0)
  68. jack_error("pthread_mutex_lock error err = %s", strerror(res));
  69. if ((res = pthread_cond_wait(&fCond, &fMutex)) != 0)
  70. jack_error("pthread_cond_wait error err = %s", strerror(errno));
  71. res = pthread_mutex_unlock(&fMutex);
  72. if (res != 0)
  73. jack_error("pthread_mutex_unlock error err = %s", strerror(res));
  74. }
  75. bool JackProcessSync::TimedWait(long usec)
  76. {
  77. struct timeval T0, T1;
  78. timespec time;
  79. struct timeval now;
  80. int res;
  81. jack_log("JackProcessSync::TimedWait time out = %ld", usec);
  82. gettimeofday(&T0, 0);
  83. gettimeofday(&now, 0);
  84. unsigned int next_date_usec = now.tv_usec + usec;
  85. time.tv_sec = now.tv_sec + (next_date_usec / 1000000);
  86. time.tv_nsec = (next_date_usec % 1000000) * 1000;
  87. res = pthread_cond_timedwait(&fCond, &fMutex, &time);
  88. if (res != 0)
  89. jack_error("pthread_cond_timedwait error usec = %ld err = %s", usec, strerror(res));
  90. gettimeofday(&T1, 0);
  91. jack_log("JackProcessSync::TimedWait finished delta = %5.1lf",
  92. (1e6 * T1.tv_sec - 1e6 * T0.tv_sec + T1.tv_usec - T0.tv_usec));
  93. return (res == 0);
  94. }
  95. bool JackProcessSync::LockedTimedWait(long usec)
  96. {
  97. struct timeval T0, T1;
  98. timespec time;
  99. struct timeval now;
  100. int res;
  101. res = pthread_mutex_lock(&fMutex);
  102. if (res != 0)
  103. jack_error("pthread_mutex_lock error err = %s", usec, strerror(res));
  104. jack_log("JackProcessSync::TimedWait time out = %ld", usec);
  105. gettimeofday(&T0, 0);
  106. gettimeofday(&now, 0);
  107. unsigned int next_date_usec = now.tv_usec + usec;
  108. time.tv_sec = now.tv_sec + (next_date_usec / 1000000);
  109. time.tv_nsec = (next_date_usec % 1000000) * 1000;
  110. res = pthread_cond_timedwait(&fCond, &fMutex, &time);
  111. if (res != 0)
  112. jack_error("pthread_cond_timedwait error usec = %ld err = %s", usec, strerror(res));
  113. gettimeofday(&T1, 0);
  114. pthread_mutex_unlock(&fMutex);
  115. if (res != 0)
  116. jack_error("pthread_mutex_unlock error err = %s", usec, strerror(res));
  117. jack_log("JackProcessSync::TimedWait finished delta = %5.1lf",
  118. (1e6 * T1.tv_sec - 1e6 * T0.tv_sec + T1.tv_usec - T0.tv_usec));
  119. return (res == 0);
  120. }
  121. } // end of namespace