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.

178 lines
5.4KB

  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 "JackPosixProcessSync.h"
  16. #include "JackError.h"
  17. namespace Jack
  18. {
  19. void JackPosixProcessSync::Signal()
  20. {
  21. int res = pthread_cond_signal(&fCond);
  22. if (res != 0) {
  23. jack_error("JackPosixProcessSync::Signal error err = %s", strerror(res));
  24. }
  25. }
  26. // TO DO : check thread consistency?
  27. void JackPosixProcessSync::LockedSignal()
  28. {
  29. int res = pthread_mutex_lock(&fMutex);
  30. if (res != 0) {
  31. jack_error("JackPosixProcessSync::LockedSignal error err = %s", strerror(res));
  32. }
  33. res = pthread_cond_signal(&fCond);
  34. if (res != 0) {
  35. jack_error("JackPosixProcessSync::LockedSignal error err = %s", strerror(res));
  36. }
  37. res = pthread_mutex_unlock(&fMutex);
  38. if (res != 0) {
  39. jack_error("JackPosixProcessSync::LockedSignal error err = %s", strerror(res));
  40. }
  41. }
  42. void JackPosixProcessSync::SignalAll()
  43. {
  44. int res = pthread_cond_broadcast(&fCond);
  45. if (res != 0) {
  46. jack_error("JackPosixProcessSync::SignalAll error err = %s", strerror(res));
  47. }
  48. }
  49. // TO DO : check thread consistency?
  50. void JackPosixProcessSync::LockedSignalAll()
  51. {
  52. int res = pthread_mutex_lock(&fMutex);
  53. if (res != 0) {
  54. jack_error("JackPosixProcessSync::LockedSignalAll error err = %s", strerror(res));
  55. }
  56. res = pthread_cond_broadcast(&fCond);
  57. if (res != 0) {
  58. jack_error("JackPosixProcessSync::LockedSignalAll error err = %s", strerror(res));
  59. }
  60. res = pthread_mutex_unlock(&fMutex);
  61. if (res != 0) {
  62. jack_error("JackPosixProcessSync::LockedSignalAll error err = %s", strerror(res));
  63. }
  64. }
  65. void JackPosixProcessSync::Wait()
  66. {
  67. ThrowIf(!pthread_equal(pthread_self(), fOwner), JackException("JackPosixProcessSync::Wait: a thread has to have locked a mutex before it can wait"));
  68. fOwner = 0;
  69. int res = pthread_cond_wait(&fCond, &fMutex);
  70. if (res != 0) {
  71. jack_error("JackPosixProcessSync::Wait error err = %s", strerror(res));
  72. } else {
  73. fOwner = pthread_self();
  74. }
  75. }
  76. // TO DO : check thread consistency?
  77. void JackPosixProcessSync::LockedWait()
  78. {
  79. int res;
  80. res = pthread_mutex_lock(&fMutex);
  81. if (res != 0) {
  82. jack_error("JackPosixProcessSync::LockedWait error err = %s", strerror(res));
  83. }
  84. if ((res = pthread_cond_wait(&fCond, &fMutex)) != 0) {
  85. jack_error("JackPosixProcessSync::LockedWait error err = %s", strerror(res));
  86. }
  87. res = pthread_mutex_unlock(&fMutex);
  88. if (res != 0) {
  89. jack_error("JackPosixProcessSync::LockedWait error err = %s", strerror(res));
  90. }
  91. }
  92. bool JackPosixProcessSync::TimedWait(long usec)
  93. {
  94. ThrowIf(!pthread_equal(pthread_self(), fOwner), JackException("JackPosixProcessSync::TimedWait: a thread has to have locked a mutex before it can wait"));
  95. fOwner = 0;
  96. struct timeval T0, T1;
  97. timespec time;
  98. struct timeval now;
  99. int res;
  100. jack_log("JackPosixProcessSync::TimedWait time out = %ld", usec);
  101. gettimeofday(&T0, 0);
  102. gettimeofday(&now, 0);
  103. unsigned int next_date_usec = now.tv_usec + usec;
  104. time.tv_sec = now.tv_sec + (next_date_usec / 1000000);
  105. time.tv_nsec = (next_date_usec % 1000000) * 1000;
  106. res = pthread_cond_timedwait(&fCond, &fMutex, &time);
  107. if (res != 0) {
  108. jack_error("JackPosixProcessSync::TimedWait error usec = %ld err = %s", usec, strerror(res));
  109. } else {
  110. fOwner = pthread_self();
  111. }
  112. gettimeofday(&T1, 0);
  113. jack_log("JackPosixProcessSync::TimedWait finished delta = %5.1lf",
  114. (1e6 * T1.tv_sec - 1e6 * T0.tv_sec + T1.tv_usec - T0.tv_usec));
  115. return (res == 0);
  116. }
  117. // TO DO : check thread consistency?
  118. bool JackPosixProcessSync::LockedTimedWait(long usec)
  119. {
  120. struct timeval T0, T1;
  121. timespec time;
  122. struct timeval now;
  123. int res1, res2;
  124. res1 = pthread_mutex_lock(&fMutex);
  125. if (res1 != 0) {
  126. jack_error("JackPosixProcessSync::LockedTimedWait error err = %s", usec, strerror(res1));
  127. }
  128. jack_log("JackPosixProcessSync::TimedWait time out = %ld", usec);
  129. gettimeofday(&T0, 0);
  130. gettimeofday(&now, 0);
  131. unsigned int next_date_usec = now.tv_usec + usec;
  132. time.tv_sec = now.tv_sec + (next_date_usec / 1000000);
  133. time.tv_nsec = (next_date_usec % 1000000) * 1000;
  134. res2 = pthread_cond_timedwait(&fCond, &fMutex, &time);
  135. if (res2 != 0) {
  136. jack_error("JackPosixProcessSync::LockedTimedWait error usec = %ld err = %s", usec, strerror(res2));
  137. }
  138. gettimeofday(&T1, 0);
  139. res1 = pthread_mutex_unlock(&fMutex);
  140. if (res1 != 0) {
  141. jack_error("JackPosixProcessSync::LockedTimedWait error err = %s", usec, strerror(res1));
  142. }
  143. jack_log("JackPosixProcessSync::TimedWait finished delta = %5.1lf",
  144. (1e6 * T1.tv_sec - 1e6 * T0.tv_sec + T1.tv_usec - T0.tv_usec));
  145. return (res2 == 0);
  146. }
  147. } // end of namespace