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.

205 lines
5.2KB

  1. /*
  2. * Copyright (C) 2004 Rui Nuno Capela, Steve Harris
  3. * Copyright (C) 2008 Nedko Arnaudov
  4. * Copyright (C) 2008 Grame
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. *
  20. */
  21. #include "JackMessageBuffer.h"
  22. #include "JackGlobals.h"
  23. #include "JackError.h"
  24. #include "JackTime.h"
  25. namespace Jack
  26. {
  27. JackMessageBuffer* JackMessageBuffer::fInstance = NULL;
  28. JackMessageBuffer::JackMessageBuffer()
  29. :fInit(NULL),
  30. fInitArg(NULL),
  31. fThread(this),
  32. fGuard(),
  33. fInBuffer(0),
  34. fOutBuffer(0),
  35. fOverruns(0),
  36. fRunning(false)
  37. {}
  38. JackMessageBuffer::~JackMessageBuffer()
  39. {}
  40. bool JackMessageBuffer::Start()
  41. {
  42. // Before StartSync()...
  43. fRunning = true;
  44. if (fThread.StartSync() == 0) {
  45. return true;
  46. } else {
  47. fRunning = false;
  48. return false;
  49. }
  50. }
  51. bool JackMessageBuffer::Stop()
  52. {
  53. if (fOverruns > 0) {
  54. jack_error("WARNING: %d message buffer overruns!", fOverruns);
  55. } else {
  56. jack_log("no message buffer overruns");
  57. }
  58. if (fGuard.Lock()) {
  59. fRunning = false;
  60. fGuard.Signal();
  61. fGuard.Unlock();
  62. fThread.Stop();
  63. } else {
  64. fThread.Kill();
  65. }
  66. Flush();
  67. return true;
  68. }
  69. void JackMessageBuffer::Flush()
  70. {
  71. while (fOutBuffer != fInBuffer) {
  72. jack_log_function(fBuffers[fOutBuffer].level, fBuffers[fOutBuffer].message);
  73. fOutBuffer = MB_NEXT(fOutBuffer);
  74. }
  75. }
  76. void JackMessageBuffer::AddMessage(int level, const char *message)
  77. {
  78. if (fGuard.Trylock()) {
  79. fBuffers[fInBuffer].level = level;
  80. strncpy(fBuffers[fInBuffer].message, message, MB_BUFFERSIZE);
  81. fInBuffer = MB_NEXT(fInBuffer);
  82. fGuard.Signal();
  83. fGuard.Unlock();
  84. } else { /* lock collision */
  85. INC_ATOMIC(&fOverruns);
  86. }
  87. }
  88. bool JackMessageBuffer::Execute()
  89. {
  90. if (fGuard.Lock()) {
  91. while (fRunning) {
  92. fGuard.Wait();
  93. /* the client asked for all threads to run a thread
  94. initialization callback, which includes us.
  95. */
  96. if (fInit) {
  97. fInit(fInitArg);
  98. fInit = NULL;
  99. /* and we're done */
  100. fGuard.Signal();
  101. }
  102. /* releasing the mutex reduces contention */
  103. fGuard.Unlock();
  104. Flush();
  105. fGuard.Lock();
  106. }
  107. fGuard.Unlock();
  108. } else {
  109. jack_error("JackMessageBuffer::Execute lock cannot be taken");
  110. }
  111. return false;
  112. }
  113. bool JackMessageBuffer::Create()
  114. {
  115. if (fInstance == NULL) {
  116. fInstance = new JackMessageBuffer();
  117. if (!fInstance->Start()) {
  118. jack_error("JackMessageBuffer::Create cannot start thread");
  119. delete fInstance;
  120. fInstance = NULL;
  121. return false;
  122. }
  123. }
  124. return true;
  125. }
  126. bool JackMessageBuffer::Destroy()
  127. {
  128. if (fInstance != NULL) {
  129. fInstance->Stop();
  130. delete fInstance;
  131. fInstance = NULL;
  132. return true;
  133. } else {
  134. return false;
  135. }
  136. }
  137. void JackMessageBufferAdd(int level, const char *message)
  138. {
  139. if (Jack::JackMessageBuffer::fInstance == NULL) {
  140. /* Unable to print message with realtime safety. Complain and print it anyway. */
  141. jack_log_function(LOG_LEVEL_ERROR, "messagebuffer not initialized, skip message");
  142. } else {
  143. Jack::JackMessageBuffer::fInstance->AddMessage(level, message);
  144. }
  145. }
  146. int JackMessageBuffer::SetInitCallback(JackThreadInitCallback callback, void *arg)
  147. {
  148. if (fInstance && callback && fRunning && fGuard.Lock()) {
  149. /* set up the callback */
  150. fInitArg = arg;
  151. fInit = callback;
  152. #ifndef WIN32
  153. // wake msg buffer thread
  154. fGuard.Signal();
  155. // wait for it to be done
  156. fGuard.Wait();
  157. // and we're done
  158. fGuard.Unlock();
  159. #else
  160. /*
  161. The condition variable emulation code does not work reliably on Windows (lost signal).
  162. So use a "hackish" way to signal/wait for the result.
  163. Probaly better in the long term : use pthread-win32 (http://sourceware.org/pthreads-win32/`
  164. */
  165. fGuard.Unlock();
  166. int count = 0;
  167. while (fInit && ++count < 1000) {
  168. /* wake msg buffer thread */
  169. fGuard.Signal();
  170. JackSleep(1000);
  171. }
  172. if (count == 1000) {
  173. jack_error("JackMessageBuffer::SetInitCallback : signal lost");
  174. return -1;
  175. }
  176. #endif
  177. return 0;
  178. }
  179. jack_error("JackMessageBuffer::SetInitCallback : callback could not be executed");
  180. return -1;
  181. }
  182. };