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.

177 lines
4.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. namespace Jack
  25. {
  26. JackMessageBuffer* JackMessageBuffer::fInstance = NULL;
  27. JackMessageBuffer::JackMessageBuffer()
  28. :fInit(NULL),
  29. fInitArg(NULL),
  30. fThread(this),
  31. fGuard("JackMessageBuffer"),
  32. fInBuffer(0),
  33. fOutBuffer(0),
  34. fOverruns(0),
  35. fRunning(false)
  36. {}
  37. JackMessageBuffer::~JackMessageBuffer()
  38. {}
  39. bool JackMessageBuffer::Start()
  40. {
  41. fRunning = true;
  42. return fThread.StartSync();
  43. }
  44. bool JackMessageBuffer::Stop()
  45. {
  46. if (fOverruns > 0) {
  47. jack_error("WARNING: %d message buffer overruns!", fOverruns);
  48. } else {
  49. jack_log("no message buffer overruns");
  50. }
  51. if (fGuard.Lock()) {
  52. fRunning = false;
  53. fGuard.Signal();
  54. fGuard.Unlock();
  55. fThread.Stop();
  56. } else {
  57. fThread.Kill();
  58. }
  59. Flush();
  60. return true;
  61. }
  62. void JackMessageBuffer::Flush()
  63. {
  64. while (fOutBuffer != fInBuffer) {
  65. jack_log_function(fBuffers[fOutBuffer].level, fBuffers[fOutBuffer].message);
  66. fOutBuffer = MB_NEXT(fOutBuffer);
  67. }
  68. }
  69. void JackMessageBuffer::AddMessage(int level, const char *message)
  70. {
  71. if (fGuard.Trylock()) {
  72. fBuffers[fInBuffer].level = level;
  73. strncpy(fBuffers[fInBuffer].message, message, MB_BUFFERSIZE);
  74. fInBuffer = MB_NEXT(fInBuffer);
  75. fGuard.Signal();
  76. fGuard.Unlock();
  77. } else { /* lock collision */
  78. INC_ATOMIC(&fOverruns);
  79. }
  80. }
  81. bool JackMessageBuffer::Execute()
  82. {
  83. if (fGuard.Lock()) {
  84. while (fRunning) {
  85. fGuard.Wait();
  86. /* the client asked for all threads to run a thread
  87. initialization callback, which includes us.
  88. */
  89. if (fInit) {
  90. fInit(fInitArg);
  91. fInit = NULL;
  92. /* and we're done */
  93. fGuard.Signal();
  94. }
  95. /* releasing the mutex reduces contention */
  96. fGuard.Unlock();
  97. Flush();
  98. fGuard.Lock();
  99. }
  100. fGuard.Unlock();
  101. } else {
  102. jack_error("JackMessageBuffer::Execute lock cannot be taken");
  103. }
  104. return false;
  105. }
  106. bool JackMessageBuffer::Create()
  107. {
  108. if (fInstance == NULL) {
  109. fInstance = new JackMessageBuffer();
  110. if (!fInstance->Start()) {
  111. jack_error("JackMessageBuffer::Create cannot start thread...");
  112. delete fInstance;
  113. fInstance = NULL;
  114. return false;
  115. }
  116. }
  117. return true;
  118. }
  119. bool JackMessageBuffer::Destroy()
  120. {
  121. if (fInstance != NULL) {
  122. fInstance->Stop();
  123. delete fInstance;
  124. fInstance = NULL;
  125. return true;
  126. } else {
  127. return false;
  128. }
  129. }
  130. void JackMessageBufferAdd(int level, const char *message)
  131. {
  132. if (Jack::JackMessageBuffer::fInstance == NULL) {
  133. /* Unable to print message with realtime safety. Complain and print it anyway. */
  134. jack_log_function(LOG_LEVEL_ERROR, "messagebuffer not initialized, skip message");
  135. } else {
  136. Jack::JackMessageBuffer::fInstance->AddMessage(level, message);
  137. }
  138. }
  139. void JackMessageBuffer::SetInitCallback(JackThreadInitCallback callback, void *arg)
  140. {
  141. if (fInstance && fGuard.Lock()) {
  142. /* set up the callback */
  143. fInitArg = arg;
  144. fInit = callback;
  145. /* wake msg buffer thread */
  146. fGuard.Signal();
  147. /* wait for it to be done */
  148. fGuard.Wait();
  149. /* and we're done */
  150. fGuard.Unlock();
  151. } else {
  152. jack_error("JackMessageBuffer::SetInitCallback : callback cannot be executed");
  153. }
  154. }
  155. };