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.

166 lines
4.1KB

  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. void JackMessageBuffer::Start()
  40. {
  41. fRunning = true;
  42. fThread.StartSync();
  43. }
  44. void 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. }
  61. void JackMessageBuffer::Flush()
  62. {
  63. while (fOutBuffer != fInBuffer) {
  64. jack_log_function(fBuffers[fOutBuffer].level, fBuffers[fOutBuffer].message);
  65. fOutBuffer = MB_NEXT(fOutBuffer);
  66. }
  67. }
  68. void JackMessageBuffer::AddMessage(int level, const char *message)
  69. {
  70. if (fGuard.Trylock()) {
  71. fBuffers[fInBuffer].level = level;
  72. strncpy(fBuffers[fInBuffer].message, message, MB_BUFFERSIZE);
  73. fInBuffer = MB_NEXT(fInBuffer);
  74. fGuard.Signal();
  75. fGuard.Unlock();
  76. } else { /* lock collision */
  77. INC_ATOMIC(&fOverruns);
  78. }
  79. }
  80. bool JackMessageBuffer::Execute()
  81. {
  82. if (fGuard.Lock()) {
  83. while (fRunning) {
  84. fGuard.Wait();
  85. /* the client asked for all threads to run a thread
  86. initialization callback, which includes us.
  87. */
  88. if (fInit) {
  89. fInit(fInitArg);
  90. fInit = NULL;
  91. /* and we're done */
  92. fGuard.Signal();
  93. }
  94. /* releasing the mutex reduces contention */
  95. fGuard.Unlock();
  96. Flush();
  97. fGuard.Lock();
  98. }
  99. fGuard.Unlock();
  100. } else {
  101. jack_error("JackMessageBuffer::Execute lock cannot be taken");
  102. }
  103. return false;
  104. }
  105. void JackMessageBuffer::Create()
  106. {
  107. if (fInstance == NULL) {
  108. fInstance = new JackMessageBuffer();
  109. fInstance->Start();
  110. }
  111. }
  112. void JackMessageBuffer::Destroy()
  113. {
  114. if (fInstance != NULL) {
  115. fInstance->Stop();
  116. delete fInstance;
  117. fInstance = NULL;
  118. }
  119. }
  120. void JackMessageBufferAdd(int level, const char *message)
  121. {
  122. if (Jack::JackMessageBuffer::fInstance == NULL) {
  123. /* Unable to print message with realtime safety. Complain and print it anyway. */
  124. jack_log_function(LOG_LEVEL_ERROR, "messagebuffer not initialized, skip message");
  125. } else {
  126. Jack::JackMessageBuffer::fInstance->AddMessage(level, message);
  127. }
  128. }
  129. void JackMessageBuffer::SetInitCallback(JackThreadInitCallback callback, void *arg)
  130. {
  131. if (fGuard.Lock()) {
  132. /* set up the callback */
  133. fInitArg = arg;
  134. fInit = callback;
  135. /* wake msg buffer thread */
  136. fGuard.Signal();
  137. /* wait for it to be done */
  138. fGuard.Wait();
  139. /* and we're done */
  140. fGuard.Unlock();
  141. } else {
  142. jack_error("JackMessageBuffer::SetInitCallback lock cannot be taken");
  143. }
  144. }
  145. };