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.

181 lines
4.6KB

  1. /*
  2. Copyright (C) 2004-2006 Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 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 General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #ifndef __JackProcessSync__
  16. #define __JackProcessSync__
  17. #include "JackSyncInterface.h"
  18. #include "JackSynchro.h"
  19. #include <pthread.h>
  20. #include <sys/time.h>
  21. #include <unistd.h>
  22. namespace Jack
  23. {
  24. /*!
  25. \brief A synchronization primitive built using a condition variable.
  26. */
  27. class JackProcessSync : public JackSyncInterface
  28. {
  29. private:
  30. pthread_mutex_t fLock; // Mutex
  31. pthread_cond_t fCond; // Condition variable
  32. public:
  33. JackProcessSync(): JackSyncInterface()
  34. {
  35. pthread_mutex_init(&fLock, NULL);
  36. pthread_cond_init(&fCond, NULL);
  37. }
  38. virtual ~JackProcessSync()
  39. {
  40. pthread_mutex_destroy(&fLock);
  41. pthread_cond_destroy(&fCond);
  42. }
  43. bool Allocate(const char* name)
  44. {
  45. return true;
  46. }
  47. bool Connect(const char* name)
  48. {
  49. return true;
  50. }
  51. void Destroy()
  52. {}
  53. bool TimedWait(long usec)
  54. {
  55. struct timeval T0, T1;
  56. timespec time;
  57. struct timeval now;
  58. int res;
  59. pthread_mutex_lock(&fLock);
  60. JackLog("JackProcessSync::Wait time out = %ld\n", usec);
  61. gettimeofday(&T0, 0);
  62. static const UInt64 kNanosPerSec = 1000000000ULL;
  63. static const UInt64 kNanosPerUsec = 1000ULL;
  64. gettimeofday(&now, 0);
  65. UInt64 nextDateNanos = now.tv_sec * kNanosPerSec + (now.tv_usec + usec) * kNanosPerUsec;
  66. time.tv_sec = nextDateNanos / kNanosPerSec;
  67. time.tv_nsec = nextDateNanos % kNanosPerSec;
  68. res = pthread_cond_timedwait(&fCond, &fLock, &time);
  69. if (res != 0)
  70. jack_error("pthread_cond_timedwait error usec = %ld err = %s", usec, strerror(res));
  71. gettimeofday(&T1, 0);
  72. pthread_mutex_unlock(&fLock);
  73. JackLog("JackProcessSync::Wait finished delta = %5.1lf\n",
  74. (1e6 * T1.tv_sec - 1e6 * T0.tv_sec + T1.tv_usec - T0.tv_usec));
  75. return (res == 0);
  76. }
  77. void Wait()
  78. {
  79. int res;
  80. pthread_mutex_lock(&fLock);
  81. JackLog("JackProcessSync::Wait...\n");
  82. if ((res = pthread_cond_wait(&fCond, &fLock)) != 0)
  83. jack_error("pthread_cond_wait error err = %s", strerror(errno));
  84. pthread_mutex_unlock(&fLock);
  85. JackLog("JackProcessSync::Wait finished\n");
  86. }
  87. void SignalAll()
  88. {
  89. //pthread_mutex_lock(&fLock);
  90. pthread_cond_broadcast(&fCond);
  91. //pthread_mutex_unlock(&fLock);
  92. }
  93. };
  94. /*!
  95. \brief A synchronization primitive built using an inter-process synchronization object.
  96. */
  97. class JackInterProcessSync : public JackSyncInterface
  98. {
  99. private:
  100. JackSynchro* fSynchro;
  101. public:
  102. JackInterProcessSync(JackSynchro* synchro): fSynchro(synchro)
  103. {}
  104. virtual ~JackInterProcessSync()
  105. {
  106. delete fSynchro;
  107. }
  108. bool Allocate(const char* name)
  109. {
  110. return fSynchro->Allocate(name, 0);
  111. }
  112. void Destroy()
  113. {
  114. fSynchro->Destroy();
  115. }
  116. bool Connect(const char* name)
  117. {
  118. return fSynchro->Connect(name);
  119. }
  120. bool TimedWait(long usec)
  121. {
  122. struct timeval T0, T1;
  123. JackLog("JackInterProcessSync::Wait...\n");
  124. gettimeofday(&T0, 0);
  125. bool res = fSynchro->TimedWait(usec);
  126. gettimeofday(&T1, 0);
  127. JackLog("JackInterProcessSync::Wait finished delta = %5.1lf\n",
  128. (1e6 * T1.tv_sec - 1e6 * T0.tv_sec + T1.tv_usec - T0.tv_usec));
  129. return res;
  130. }
  131. void Wait()
  132. {
  133. fSynchro->Wait();
  134. }
  135. void SignalAll()
  136. {
  137. fSynchro->SignalAll();
  138. }
  139. };
  140. } // end of namespace
  141. #endif