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.

147 lines
4.6KB

  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("JackProcessSync::Signal error err = %s", strerror(res));
  24. }
  25. void JackProcessSync::LockedSignal()
  26. {
  27. int res = pthread_mutex_lock(&fMutex);
  28. if (res != 0)
  29. jack_error("JackProcessSync::LockedSignal error err = %s", strerror(res));
  30. res = pthread_cond_signal(&fCond);
  31. if (res != 0)
  32. jack_error("JackProcessSync::LockedSignal error err = %s", strerror(res));
  33. res = pthread_mutex_unlock(&fMutex);
  34. if (res != 0)
  35. jack_error("JackProcessSync::LockedSignal error err = %s", strerror(res));
  36. }
  37. void JackProcessSync::SignalAll()
  38. {
  39. int res = pthread_cond_broadcast(&fCond);
  40. if (res != 0)
  41. jack_error("JackProcessSync::SignalAll error err = %s", strerror(res));
  42. }
  43. void JackProcessSync::LockedSignalAll()
  44. {
  45. int res = pthread_mutex_lock(&fMutex);
  46. if (res != 0)
  47. jack_error("JackProcessSync::LockedSignalAll error err = %s", strerror(res));
  48. res = pthread_cond_broadcast(&fCond);
  49. if (res != 0)
  50. jack_error("JackProcessSync::LockedSignalAll error err = %s", strerror(res));
  51. res = pthread_mutex_unlock(&fMutex);
  52. if (res != 0)
  53. jack_error("JackProcessSync::LockedSignalAll error err = %s", strerror(res));
  54. }
  55. void JackProcessSync::Wait()
  56. {
  57. int res;
  58. if ((res = pthread_cond_wait(&fCond, &fMutex)) != 0)
  59. jack_error("JackProcessSync::Wait error err = %s", strerror(res));
  60. }
  61. void JackProcessSync::LockedWait()
  62. {
  63. int res;
  64. res = pthread_mutex_lock(&fMutex);
  65. if (res != 0)
  66. jack_error("JackProcessSync::LockedWait error err = %s", strerror(res));
  67. if ((res = pthread_cond_wait(&fCond, &fMutex)) != 0)
  68. jack_error("JackProcessSync::LockedWait error err = %s", strerror(res));
  69. res = pthread_mutex_unlock(&fMutex);
  70. if (res != 0)
  71. jack_error("JackProcessSync::LockedWait error err = %s", strerror(res));
  72. }
  73. bool JackProcessSync::TimedWait(long usec)
  74. {
  75. struct timeval T0, T1;
  76. timespec time;
  77. struct timeval now;
  78. int res;
  79. jack_log("JackProcessSync::TimedWait time out = %ld", usec);
  80. gettimeofday(&T0, 0);
  81. gettimeofday(&now, 0);
  82. unsigned int next_date_usec = now.tv_usec + usec;
  83. time.tv_sec = now.tv_sec + (next_date_usec / 1000000);
  84. time.tv_nsec = (next_date_usec % 1000000) * 1000;
  85. res = pthread_cond_timedwait(&fCond, &fMutex, &time);
  86. if (res != 0)
  87. jack_error("JackProcessSync::TimedWait error usec = %ld err = %s", usec, strerror(res));
  88. gettimeofday(&T1, 0);
  89. jack_log("JackProcessSync::TimedWait finished delta = %5.1lf",
  90. (1e6 * T1.tv_sec - 1e6 * T0.tv_sec + T1.tv_usec - T0.tv_usec));
  91. return (res == 0);
  92. }
  93. bool JackProcessSync::LockedTimedWait(long usec)
  94. {
  95. struct timeval T0, T1;
  96. timespec time;
  97. struct timeval now;
  98. int res1, res2;
  99. res1 = pthread_mutex_lock(&fMutex);
  100. if (res1 != 0)
  101. jack_error("JackProcessSync::LockedTimedWait error err = %s", usec, strerror(res1));
  102. jack_log("JackProcessSync::TimedWait time out = %ld", usec);
  103. gettimeofday(&T0, 0);
  104. gettimeofday(&now, 0);
  105. unsigned int next_date_usec = now.tv_usec + usec;
  106. time.tv_sec = now.tv_sec + (next_date_usec / 1000000);
  107. time.tv_nsec = (next_date_usec % 1000000) * 1000;
  108. res2 = pthread_cond_timedwait(&fCond, &fMutex, &time);
  109. if (res2 != 0)
  110. jack_error("JackProcessSync::LockedTimedWait error usec = %ld err = %s", usec, strerror(res2));
  111. gettimeofday(&T1, 0);
  112. res1 = pthread_mutex_unlock(&fMutex);
  113. if (res1 != 0)
  114. jack_error("JackProcessSync::LockedTimedWait error err = %s", usec, strerror(res1));
  115. jack_log("JackProcessSync::TimedWait finished delta = %5.1lf",
  116. (1e6 * T1.tv_sec - 1e6 * T0.tv_sec + T1.tv_usec - T0.tv_usec));
  117. return (res2 == 0);
  118. }
  119. } // end of namespace