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.

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