Fix build under mixed mode
[jack2.git] / common / JackMessageBuffer.cpp
blobcd19799326e84eaa3e4960aaa582ffd24975fe9e
1 /*
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"
25 #include "JackTime.h"
27 namespace Jack
30 JackMessageBuffer* JackMessageBuffer::fInstance = NULL;
32 JackMessageBuffer::JackMessageBuffer()
33 :fInit(NULL),
34 fInitArg(NULL),
35 fThread(this),
36 fGuard(),
37 fInBuffer(0),
38 fOutBuffer(0),
39 fOverruns(0),
40 fRunning(false)
43 JackMessageBuffer::~JackMessageBuffer()
46 bool JackMessageBuffer::Start()
48 // Before StartSync()...
49 fRunning = true;
50 if (fThread.StartSync() == 0) {
51 return true;
52 } else {
53 fRunning = false;
54 return false;
58 bool JackMessageBuffer::Stop()
60 if (fOverruns > 0) {
61 jack_error("WARNING: %d message buffer overruns!", fOverruns);
62 } else {
63 jack_log("no message buffer overruns");
66 if (fGuard.Lock()) {
67 fRunning = false;
68 fGuard.Signal();
69 fGuard.Unlock();
70 fThread.Stop();
71 } else {
72 fThread.Kill();
75 Flush();
76 return true;
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);
93 fGuard.Signal();
94 fGuard.Unlock();
95 } else { /* lock collision */
96 INC_ATOMIC(&fOverruns);
100 bool JackMessageBuffer::Execute()
102 if (fGuard.Lock()) {
103 while (fRunning) {
104 fGuard.Wait();
105 /* the client asked for all threads to run a thread
106 initialization callback, which includes us.
108 if (fInit) {
109 fInit(fInitArg);
110 fInit = NULL;
111 /* and we're done */
112 fGuard.Signal();
115 /* releasing the mutex reduces contention */
116 fGuard.Unlock();
117 Flush();
118 fGuard.Lock();
120 fGuard.Unlock();
121 } else {
122 jack_error("JackMessageBuffer::Execute lock cannot be taken");
125 return false;
128 bool JackMessageBuffer::Create()
130 if (fInstance == NULL) {
131 fInstance = new JackMessageBuffer();
132 if (!fInstance->Start()) {
133 jack_error("JackMessageBuffer::Create cannot start thread");
134 delete fInstance;
135 fInstance = NULL;
136 return false;
140 return true;
143 bool JackMessageBuffer::Destroy()
145 if (fInstance != NULL) {
146 fInstance->Stop();
147 delete fInstance;
148 fInstance = NULL;
149 return true;
150 } else {
151 return false;
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");
160 } else {
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 */
169 fInitArg = arg;
170 fInit = callback;
172 #ifndef WIN32
173 // wake msg buffer thread
174 fGuard.Signal();
175 // wait for it to be done
176 fGuard.Wait();
177 // and we're done
178 fGuard.Unlock();
179 #else
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/`
185 fGuard.Unlock();
186 int count = 0;
187 while (fInit && ++count < 1000) {
188 /* wake msg buffer thread */
189 fGuard.Signal();
190 JackSleep(1000);
192 if (count == 1000) {
193 jack_error("JackMessageBuffer::SetInitCallback : signal lost");
194 return -1;
196 #endif
197 return 0;
199 jack_error("JackMessageBuffer::SetInitCallback : callback could not be executed");
200 return -1;