Correct JackInfoShutdownCallback prototype, two new JackClientProcessFailure and...
[jack2.git] / common / JackMessageBuffer.cpp
blobbc380a523fd772baeb302d0e2c5524ae6c6f7176
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.
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.
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.
22 #include "JackMessageBuffer.h"
23 #include "JackGlobals.h"
24 #include "JackError.h"
26 namespace Jack
29 JackMessageBuffer* JackMessageBuffer::fInstance = NULL;
31 JackMessageBuffer::JackMessageBuffer()
32 :fThread(this),fInBuffer(0),fOutBuffer(0),fOverruns(0),fRunning(false)
35 JackMessageBuffer::~JackMessageBuffer()
38 void JackMessageBuffer::Start()
40 fRunning = true;
41 fThread.StartSync();
44 void JackMessageBuffer::Stop()
46 if (fOverruns > 0) {
47 jack_error("WARNING: %d message buffer overruns!", fOverruns);
48 } else {
49 jack_info("no message buffer overruns");
51 fGuard.Lock();
52 fRunning = false;
53 fGuard.Signal();
54 fGuard.Unlock();
55 fThread.Stop();
56 Flush();
59 void JackMessageBuffer::Flush()
61 while (fOutBuffer != fInBuffer) {
62 jack_log_function(fBuffers[fOutBuffer].level, fBuffers[fOutBuffer].message);
63 fOutBuffer = MB_NEXT(fOutBuffer);
67 void JackMessageBuffer::AddMessage(int level, const char *message)
69 if (fGuard.Trylock()) {
70 fBuffers[fInBuffer].level = level;
71 strncpy(fBuffers[fInBuffer].message, message, MB_BUFFERSIZE);
72 fInBuffer = MB_NEXT(fInBuffer);
73 fGuard.Signal();
74 fGuard.Unlock();
75 } else { /* lock collision */
76 INC_ATOMIC(&fOverruns);
80 bool JackMessageBuffer::Execute()
82 while (fRunning) {
83 fGuard.Lock();
84 fGuard.Wait();
85 Flush();
86 fGuard.Unlock();
88 return false;
91 void JackMessageBuffer::Create()
93 if (fInstance == NULL) {
94 fInstance = new JackMessageBuffer();
95 fInstance->Start();
99 void JackMessageBuffer::Destroy()
101 if (fInstance != NULL) {
102 fInstance->Stop();
103 delete fInstance;
104 fInstance = NULL;
108 void JackMessageBufferAdd(int level, const char *message)
110 if (Jack::JackMessageBuffer::fInstance == NULL) {
111 /* Unable to print message with realtime safety. Complain and print it anyway. */
112 jack_log_function(LOG_LEVEL_ERROR, "messagebuffer not initialized, skip message");
113 } else {
114 Jack::JackMessageBuffer::fInstance->AddMessage(level, message);