Update OSX install script.
[jack2.git] / common / JackMessageBuffer.cpp
blob18e4d52f9b804465174c36151957128c5150b5bb
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)
34 fRunning = true;
35 fThread.StartSync();
38 JackMessageBuffer::~JackMessageBuffer()
40 if (fOverruns > 0) {
41 jack_error("WARNING: %d message buffer overruns!", fOverruns);
42 } else {
43 jack_info("no message buffer overruns");
45 fGuard.Lock();
46 fRunning = false;
47 fGuard.Signal();
48 fGuard.Unlock();
49 fThread.Stop();
50 Flush();
53 void JackMessageBuffer::Flush()
55 while (fOutBuffer != fInBuffer) {
56 jack_log_function(fBuffers[fOutBuffer].level, fBuffers[fOutBuffer].message);
57 fOutBuffer = MB_NEXT(fOutBuffer);
61 void JackMessageBuffer::AddMessage(int level, const char *message)
63 if (fGuard.Trylock()) {
64 fBuffers[fInBuffer].level = level;
65 strncpy(fBuffers[fInBuffer].message, message, MB_BUFFERSIZE);
66 fInBuffer = MB_NEXT(fInBuffer);
67 fGuard.Signal();
68 fGuard.Unlock();
69 } else { /* lock collision */
70 INC_ATOMIC(&fOverruns);
74 bool JackMessageBuffer::Execute()
76 while (fRunning) {
77 fGuard.Lock();
78 fGuard.Wait();
79 Flush();
80 fGuard.Unlock();
82 return false;
85 void JackMessageBuffer::Create()
87 if (fInstance == NULL) {
88 fInstance = new JackMessageBuffer();
92 void JackMessageBuffer::Destroy()
94 if (fInstance != NULL) {
95 delete fInstance;
96 fInstance = NULL;
100 void JackMessageBufferAdd(int level, const char *message)
102 if (Jack::JackMessageBuffer::fInstance == NULL) {
103 /* Unable to print message with realtime safety.
104 * Complain and print it anyway. */
105 jack_log_function(LOG_LEVEL_ERROR, "messagebuffer not initialized, skip message");
106 } else {
107 Jack::JackMessageBuffer::fInstance->AddMessage(level, message);