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.

120 lines
3.1KB

  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()
  38. :fThread(this),fInBuffer(0),fOutBuffer(0),fOverruns(0)
  39. {
  40. fThread.StartSync();
  41. }
  42. JackMessageBuffer::~JackMessageBuffer()
  43. {
  44. if (fOverruns > 0) {
  45. jack_error("WARNING: %d message buffer overruns!", fOverruns);
  46. } else {
  47. jack_info("no message buffer overruns");
  48. }
  49. fThread.SetStatus(JackThread::kIdle);
  50. fSignal.Signal();
  51. fThread.Stop();
  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 (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. };