2 * Copyright (C) 2004 Rui Nuno Capela, Steve Harris
3 * Copyright (C) 2008 Nedko Arnaudov
4 * Copyright (C) 2008 Grame
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"
30 JackMessageBuffer
* JackMessageBuffer::fInstance
= NULL
;
32 JackMessageBuffer::JackMessageBuffer()
43 JackMessageBuffer::~JackMessageBuffer()
46 bool JackMessageBuffer::Start()
48 // Before StartSync()...
50 if (fThread
.StartSync() == 0) {
58 bool JackMessageBuffer::Stop()
61 jack_error("WARNING: %d message buffer overruns!", fOverruns
);
63 jack_log("no message buffer overruns");
79 void JackMessageBuffer::Flush()
81 while (fOutBuffer
!= fInBuffer
) {
82 jack_log_function(fBuffers
[fOutBuffer
].level
, fBuffers
[fOutBuffer
].message
);
83 fOutBuffer
= MB_NEXT(fOutBuffer
);
87 void JackMessageBuffer::AddMessage(int level
, const char *message
)
89 if (fGuard
.Trylock()) {
90 fBuffers
[fInBuffer
].level
= level
;
91 strncpy(fBuffers
[fInBuffer
].message
, message
, MB_BUFFERSIZE
);
92 fInBuffer
= MB_NEXT(fInBuffer
);
95 } else { /* lock collision */
96 INC_ATOMIC(&fOverruns
);
100 bool JackMessageBuffer::Execute()
105 /* the client asked for all threads to run a thread
106 initialization callback, which includes us.
115 /* releasing the mutex reduces contention */
122 jack_error("JackMessageBuffer::Execute lock cannot be taken");
128 bool JackMessageBuffer::Create()
130 if (fInstance
== NULL
) {
131 fInstance
= new JackMessageBuffer();
132 if (!fInstance
->Start()) {
133 jack_error("JackMessageBuffer::Create cannot start thread");
143 bool JackMessageBuffer::Destroy()
145 if (fInstance
!= NULL
) {
155 void JackMessageBufferAdd(int level
, const char *message
)
157 if (Jack::JackMessageBuffer::fInstance
== NULL
) {
158 /* Unable to print message with realtime safety. Complain and print it anyway. */
159 jack_log_function(LOG_LEVEL_ERROR
, "messagebuffer not initialized, skip message");
161 Jack::JackMessageBuffer::fInstance
->AddMessage(level
, message
);
165 int JackMessageBuffer::SetInitCallback(JackThreadInitCallback callback
, void *arg
)
167 if (fInstance
&& callback
&& fRunning
&& fGuard
.Lock()) {
168 /* set up the callback */
173 // wake msg buffer thread
175 // wait for it to be done
181 The condition variable emulation code does not work reliably on Windows (lost signal).
182 So use a "hackish" way to signal/wait for the result.
183 Probably better in the long term : use pthread-win32 (http://sourceware.org/pthreads-win32/`
187 while (fInit
&& ++count
< 1000) {
188 /* wake msg buffer thread */
193 jack_error("JackMessageBuffer::SetInitCallback : signal lost");
199 jack_error("JackMessageBuffer::SetInitCallback : callback could not be executed");