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.

116 lines
3.0KB

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