Jack2 1.9.12

JackMessageBuffer.cpp
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
22 #include "JackMessageBuffer.h"
23 #include "JackGlobals.h"
24 #include "JackError.h"
25 #include "JackTime.h"
26
27 namespace Jack
28 {
29
30 JackMessageBuffer* JackMessageBuffer::fInstance = NULL;
31
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)
41 {}
42
43 JackMessageBuffer::~JackMessageBuffer()
44 {}
45
46 bool JackMessageBuffer::Start()
47 {
48 // Before StartSync()...
49 fRunning = true;
50 if (fThread.StartSync() == 0) {
51 return true;
52 } else {
53 fRunning = false;
54 return false;
55 }
56 }
57
58 bool JackMessageBuffer::Stop()
59 {
60 if (fOverruns > 0) {
61 jack_error("WARNING: %d message buffer overruns!", fOverruns);
62 } else {
63 jack_log("no message buffer overruns");
64 }
65
66 if (fGuard.Lock()) {
67 fRunning = false;
68 fGuard.Signal();
69 fGuard.Unlock();
70 fThread.Stop();
71 } else {
72 fThread.Kill();
73 }
74
75 Flush();
76 return true;
77 }
78
79 void JackMessageBuffer::Flush()
80 {
81 while (fOutBuffer != fInBuffer) {
82 jack_log_function(fBuffers[fOutBuffer].level, fBuffers[fOutBuffer].message);
83 fOutBuffer = MB_NEXT(fOutBuffer);
84 }
85 }
86
87 void JackMessageBuffer::AddMessage(int level, const char *message)
88 {
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);
97 }
98 }
99
100 bool JackMessageBuffer::Execute()
101 {
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.
107 */
108 if (fInit) {
109 fInit(fInitArg);
110 fInit = NULL;
111 /* and we're done */
112 fGuard.Signal();
113 }
114
115 /* releasing the mutex reduces contention */
116 fGuard.Unlock();
117 Flush();
118 fGuard.Lock();
119 }
120 fGuard.Unlock();
121 } else {
122 jack_error("JackMessageBuffer::Execute lock cannot be taken");
123 }
124
125 return false;
126 }
127
128 bool JackMessageBuffer::Create()
129 {
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;
137 }
138 }
139
140 return true;
141 }
142
143 bool JackMessageBuffer::Destroy()
144 {
145 if (fInstance != NULL) {
146 fInstance->Stop();
147 delete fInstance;
148 fInstance = NULL;
149 return true;
150 } else {
151 return false;
152 }
153 }
154
155 void JackMessageBufferAdd(int level, const char *message)
156 {
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);
162 }
163 }
164
165 int JackMessageBuffer::SetInitCallback(JackThreadInitCallback callback, void *arg)
166 {
167 if (fInstance && callback && fRunning && fGuard.Lock()) {
168 /* set up the callback */
169 fInitArg = arg;
170 fInit = callback;
171
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
180 /*
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 Probaly better in the long term : use pthread-win32 (http://sourceware.org/pthreads-win32/`
184 */
185 fGuard.Unlock();
186 int count = 0;
187 while (fInit && ++count < 1000) {
188 /* wake msg buffer thread */
189 fGuard.Signal();
190 JackSleep(1000);
191 }
192 if (count == 1000) {
193 jack_error("JackMessageBuffer::SetInitCallback : signal lost");
194 return -1;
195 }
196 #endif
197 return 0;
198 }
199 jack_error("JackMessageBuffer::SetInitCallback : callback could not be executed");
200 return -1;
201 }
202
203 };
204
jack_error
SERVER_EXPORT void jack_error(const char *fmt,...)
Definition: JackError.cpp:92
jack_log
SERVER_EXPORT void jack_log(const char *fmt,...)
Definition: JackError.cpp:108