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.

123 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. #if defined(HAVE_CONFIG_H)
  29. #include "config.h"
  30. #endif
  31. #include "JackMessageBuffer.h"
  32. #include "JackGlobals.h"
  33. #include "JackError.h"
  34. namespace Jack
  35. {
  36. JackMessageBuffer* JackMessageBuffer::fInstance = NULL;
  37. JackMessageBuffer::JackMessageBuffer():fInBuffer(0),fOutBuffer(0),fOverruns(0)
  38. {
  39. fThread = JackGlobals::MakeThread(this);
  40. fSignal = JackGlobals::MakeInterProcessSync();
  41. fThread->StartSync();
  42. }
  43. JackMessageBuffer::~JackMessageBuffer()
  44. {
  45. if (fOverruns > 0) {
  46. jack_error("WARNING: %d message buffer overruns!", fOverruns);
  47. } else {
  48. jack_info("no message buffer overruns");
  49. }
  50. fThread->SetStatus(JackThread::kIdle);
  51. fSignal->Signal();
  52. fThread->Stop();
  53. Flush();
  54. delete fThread;
  55. delete fSignal;
  56. }
  57. void JackMessageBuffer::Flush()
  58. {
  59. while (fOutBuffer != fInBuffer) {
  60. jack_log_function(fBuffers[fOutBuffer].level, fBuffers[fOutBuffer].message);
  61. fOutBuffer = MB_NEXT(fOutBuffer);
  62. }
  63. }
  64. void JackMessageBuffer::AddMessage(int level, const char *message)
  65. {
  66. if (fMutex.Trylock()) {
  67. fBuffers[fInBuffer].level = level;
  68. strncpy(fBuffers[fInBuffer].message, message, MB_BUFFERSIZE);
  69. fInBuffer = MB_NEXT(fInBuffer);
  70. fSignal->SignalAll();
  71. fMutex.Unlock();
  72. } else { /* lock collision */
  73. INC_ATOMIC(&fOverruns);
  74. }
  75. }
  76. bool JackMessageBuffer::Execute()
  77. {
  78. fSignal->Wait();
  79. fMutex.Lock();
  80. Flush();
  81. fMutex.Unlock();
  82. return true;
  83. }
  84. void JackMessageBuffer::Create()
  85. {
  86. if (fInstance == NULL) {
  87. fInstance = new JackMessageBuffer();
  88. }
  89. }
  90. void JackMessageBuffer::Destroy()
  91. {
  92. if (fInstance != NULL) {
  93. delete fInstance;
  94. fInstance = NULL;
  95. }
  96. }
  97. void JackMessageBufferAdd(int level, const char *message)
  98. {
  99. if (Jack::JackMessageBuffer::fInstance == NULL) {
  100. /* Unable to print message with realtime safety.
  101. * Complain and print it anyway. */
  102. jack_log_function(LOG_LEVEL_ERROR, "messagebuffer not initialized, skip message");
  103. } else {
  104. Jack::JackMessageBuffer::fInstance->AddMessage(level, message);
  105. }
  106. }
  107. };