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.

208 lines
5.4KB

  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. static_assert(offsetof(JackMessageBuffer, fOverruns) % sizeof(fOverruns) == 0,
  39. "fOverruns must be aligned within JackMessageBuffer");
  40. }
  41. JackMessageBuffer::~JackMessageBuffer()
  42. {}
  43. bool JackMessageBuffer::Start()
  44. {
  45. // Before StartSync()...
  46. fRunning = true;
  47. if (fThread.StartSync() == 0) {
  48. return true;
  49. } else {
  50. fRunning = false;
  51. return false;
  52. }
  53. }
  54. bool JackMessageBuffer::Stop()
  55. {
  56. if (fOverruns > 0) {
  57. jack_error("WARNING: %d message buffer overruns!", fOverruns);
  58. } else {
  59. jack_log("no message buffer overruns");
  60. }
  61. if (fGuard.Lock()) {
  62. fRunning = false;
  63. fGuard.Signal();
  64. fGuard.Unlock();
  65. fThread.Stop();
  66. } else {
  67. fThread.Kill();
  68. }
  69. Flush();
  70. return true;
  71. }
  72. void JackMessageBuffer::Flush()
  73. {
  74. while (fOutBuffer != fInBuffer) {
  75. jack_log_function(fBuffers[fOutBuffer].level, fBuffers[fOutBuffer].message);
  76. fOutBuffer = MB_NEXT(fOutBuffer);
  77. }
  78. }
  79. void JackMessageBuffer::AddMessage(int level, const char *message)
  80. {
  81. if (fGuard.Trylock()) {
  82. fBuffers[fInBuffer].level = level;
  83. strncpy(fBuffers[fInBuffer].message, message, MB_BUFFERSIZE);
  84. fInBuffer = MB_NEXT(fInBuffer);
  85. fGuard.Signal();
  86. fGuard.Unlock();
  87. } else { /* lock collision */
  88. INC_ATOMIC(&fOverruns);
  89. }
  90. }
  91. bool JackMessageBuffer::Execute()
  92. {
  93. if (fGuard.Lock()) {
  94. while (fRunning) {
  95. fGuard.Wait();
  96. /* the client asked for all threads to run a thread
  97. initialization callback, which includes us.
  98. */
  99. if (fInit) {
  100. fInit(fInitArg);
  101. fInit = NULL;
  102. /* and we're done */
  103. fGuard.Signal();
  104. }
  105. /* releasing the mutex reduces contention */
  106. fGuard.Unlock();
  107. Flush();
  108. fGuard.Lock();
  109. }
  110. fGuard.Unlock();
  111. } else {
  112. jack_error("JackMessageBuffer::Execute lock cannot be taken");
  113. }
  114. return false;
  115. }
  116. bool JackMessageBuffer::Create()
  117. {
  118. if (fInstance == NULL) {
  119. fInstance = new JackMessageBuffer();
  120. if (!fInstance->Start()) {
  121. jack_error("JackMessageBuffer::Create cannot start thread");
  122. delete fInstance;
  123. fInstance = NULL;
  124. return false;
  125. }
  126. }
  127. return true;
  128. }
  129. bool JackMessageBuffer::Destroy()
  130. {
  131. if (fInstance != NULL) {
  132. fInstance->Stop();
  133. delete fInstance;
  134. fInstance = NULL;
  135. return true;
  136. } else {
  137. return false;
  138. }
  139. }
  140. void JackMessageBufferAdd(int level, const char *message)
  141. {
  142. if (Jack::JackMessageBuffer::fInstance == NULL) {
  143. /* Unable to print message with realtime safety. Complain and print it anyway. */
  144. jack_log_function(LOG_LEVEL_ERROR, "messagebuffer not initialized, skip message");
  145. } else {
  146. Jack::JackMessageBuffer::fInstance->AddMessage(level, message);
  147. }
  148. }
  149. int JackMessageBuffer::SetInitCallback(JackThreadInitCallback callback, void *arg)
  150. {
  151. if (fInstance && callback && fRunning && fGuard.Lock()) {
  152. /* set up the callback */
  153. fInitArg = arg;
  154. fInit = callback;
  155. #ifndef WIN32
  156. // wake msg buffer thread
  157. fGuard.Signal();
  158. // wait for it to be done
  159. fGuard.Wait();
  160. // and we're done
  161. fGuard.Unlock();
  162. #else
  163. /*
  164. The condition variable emulation code does not work reliably on Windows (lost signal).
  165. So use a "hackish" way to signal/wait for the result.
  166. Probably better in the long term : use pthread-win32 (http://sourceware.org/pthreads-win32/`
  167. */
  168. fGuard.Unlock();
  169. int count = 0;
  170. while (fInit && ++count < 1000) {
  171. /* wake msg buffer thread */
  172. fGuard.Signal();
  173. JackSleep(1000);
  174. }
  175. if (count == 1000) {
  176. jack_error("JackMessageBuffer::SetInitCallback : signal lost");
  177. return -1;
  178. }
  179. #endif
  180. return 0;
  181. }
  182. jack_error("JackMessageBuffer::SetInitCallback : callback could not be executed");
  183. return -1;
  184. }
  185. };