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.

113 lines
2.9KB

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