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.

119 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. fThread->StartSync();
  39. }
  40. JackMessageBuffer::~JackMessageBuffer()
  41. {
  42. if (fOverruns > 0) {
  43. jack_error("WARNING: %d message buffer overruns!", fOverruns);
  44. } else {
  45. jack_info("no message buffer overruns");
  46. }
  47. fThread->SetStatus(JackThread::kIdle);
  48. fSignal->Signal();
  49. fThread->Stop();
  50. Flush();
  51. delete fThread;
  52. delete fSignal;
  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 (fMutex.Trylock()) {
  64. fBuffers[fInBuffer].level = level;
  65. strncpy(fBuffers[fInBuffer].message, message, MB_BUFFERSIZE);
  66. fInBuffer = MB_NEXT(fInBuffer);
  67. fSignal->SignalAll();
  68. fMutex.Unlock();
  69. } else { /* lock collision */
  70. INC_ATOMIC(&fOverruns);
  71. }
  72. }
  73. bool JackMessageBuffer::Execute()
  74. {
  75. fSignal->Wait();
  76. fMutex.Lock();
  77. Flush();
  78. fMutex.Unlock();
  79. return true;
  80. }
  81. void JackMessageBuffer::Create()
  82. {
  83. if (fInstance == NULL) {
  84. fInstance = new JackMessageBuffer();
  85. }
  86. }
  87. void JackMessageBuffer::Destroy()
  88. {
  89. if (fInstance != NULL) {
  90. delete fInstance;
  91. fInstance = NULL;
  92. }
  93. }
  94. void JackMessageBufferAdd(int level, const char *message)
  95. {
  96. if (Jack::JackMessageBuffer::fInstance == NULL) {
  97. /* Unable to print message with realtime safety.
  98. * Complain and print it anyway. */
  99. jack_log_function(LOG_LEVEL_ERROR, "messagebuffer not initialized, skip message");
  100. } else {
  101. Jack::JackMessageBuffer::fInstance->AddMessage(level, message);
  102. }
  103. }
  104. };