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.

121 lines
3.2KB

  1. /*
  2. * messagebuffer.h -- realtime-safe message interface for jackd.
  3. *
  4. * This function is included in libjack so backend drivers can use
  5. * it, *not* for external client processes. The VERBOSE() and
  6. * MESSAGE() macros are realtime-safe.
  7. */
  8. /*
  9. * Copyright (C) 2004 Rui Nuno Capela, Steve Harris
  10. * Copyright (C) 2008 Nedko Arnaudov
  11. * Copyright (C) 2008 Grame
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU Lesser General Public License as published by
  15. * the Free Software Foundation; either version 2.1 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Lesser General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  26. *
  27. */
  28. #include "JackMessageBuffer.h"
  29. #include "JackGlobals.h"
  30. #include "JackError.h"
  31. namespace Jack
  32. {
  33. JackMessageBuffer* JackMessageBuffer::fInstance = NULL;
  34. JackMessageBuffer::JackMessageBuffer():fInBuffer(0),fOutBuffer(0),fOverruns(0)
  35. {
  36. fThread = JackGlobals::MakeThread(this);
  37. fSignal = JackGlobals::MakeInterProcessSync();
  38. fMutex = new JackMutex();
  39. fThread->Start();
  40. }
  41. JackMessageBuffer::~JackMessageBuffer()
  42. {
  43. if (fOverruns > 0) {
  44. jack_error("WARNING: %d message buffer overruns!", fOverruns);
  45. } else {
  46. jack_info("no message buffer overruns");
  47. }
  48. fThread->SetStatus(JackThread::kIdle);
  49. fSignal->Signal();
  50. fThread->Stop();
  51. Flush();
  52. delete fThread;
  53. delete fMutex;
  54. delete fSignal;
  55. }
  56. void JackMessageBuffer::Flush()
  57. {
  58. while (fOutBuffer != fInBuffer) {
  59. jack_log_function(fBuffers[fOutBuffer].level, fBuffers[fOutBuffer].message);
  60. fOutBuffer = MB_NEXT(fOutBuffer);
  61. }
  62. }
  63. void JackMessageBuffer::AddMessage(int level, const char *message)
  64. {
  65. if (fMutex->Trylock()) {
  66. fBuffers[fInBuffer].level = level;
  67. strncpy(fBuffers[fInBuffer].message, message, MB_BUFFERSIZE);
  68. fInBuffer = MB_NEXT(fInBuffer);
  69. fSignal->SignalAll();
  70. fMutex->Unlock();
  71. } else { /* lock collision */
  72. INC_ATOMIC(&fOverruns);
  73. }
  74. }
  75. bool JackMessageBuffer::Execute()
  76. {
  77. fSignal->Wait();
  78. fMutex->Lock();
  79. Flush();
  80. fMutex->Unlock();
  81. return true;
  82. }
  83. void JackMessageBuffer::Create()
  84. {
  85. if (fInstance == NULL) {
  86. fInstance = new JackMessageBuffer();
  87. }
  88. }
  89. void JackMessageBuffer::Destroy()
  90. {
  91. if (fInstance != NULL) {
  92. delete fInstance;
  93. fInstance = NULL;
  94. }
  95. }
  96. void JackMessageBufferAdd(int level, const char *message)
  97. {
  98. if (Jack::JackMessageBuffer::fInstance == NULL) {
  99. /* Unable to print message with realtime safety.
  100. * Complain and print it anyway. */
  101. jack_log_function(LOG_LEVEL_ERROR, "messagebuffer not initialized, skip message");
  102. } else {
  103. Jack::JackMessageBuffer::fInstance->AddMessage(level, message);
  104. }
  105. }
  106. };