Jack2  1.9.8
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 
26 namespace Jack
27 {
28 
29 JackMessageBuffer* JackMessageBuffer::fInstance = NULL;
30 
31 JackMessageBuffer::JackMessageBuffer()
32  :fInit(NULL),fInitArg(NULL),fThread(this),fInBuffer(0),fOutBuffer(0),fOverruns(0),fRunning(false)
33 {}
34 
35 JackMessageBuffer::~JackMessageBuffer()
36 {}
37 
38 void JackMessageBuffer::Start()
39 {
40  fRunning = true;
41  fThread.StartSync();
42 }
43 
44 void JackMessageBuffer::Stop()
45 {
46  if (fOverruns > 0) {
47  jack_error("WARNING: %d message buffer overruns!", fOverruns);
48  } else {
49  jack_log("no message buffer overruns");
50  }
51 
52  if (fGuard.Lock()) {
53  fRunning = false;
54  fGuard.Signal();
55  fGuard.Unlock();
56  fThread.Stop();
57  } else {
58  fThread.Kill();
59  }
60 
61  Flush();
62 }
63 
64 void JackMessageBuffer::Flush()
65 {
66  while (fOutBuffer != fInBuffer) {
67  jack_log_function(fBuffers[fOutBuffer].level, fBuffers[fOutBuffer].message);
68  fOutBuffer = MB_NEXT(fOutBuffer);
69  }
70 }
71 
72 void JackMessageBuffer::AddMessage(int level, const char *message)
73 {
74  if (fGuard.Trylock()) {
75  fBuffers[fInBuffer].level = level;
76  strncpy(fBuffers[fInBuffer].message, message, MB_BUFFERSIZE);
77  fInBuffer = MB_NEXT(fInBuffer);
78  fGuard.Signal();
79  fGuard.Unlock();
80  } else { /* lock collision */
81  INC_ATOMIC(&fOverruns);
82  }
83 }
84 
85 bool JackMessageBuffer::Execute()
86 {
87  if (fGuard.Lock()) {
88  while (fRunning) {
89  fGuard.Wait();
90  /* the client asked for all threads to run a thread
91  initialization callback, which includes us.
92  */
93  if (fInit) {
94  fInit(fInitArg);
95  fInit = NULL;
96  /* and we're done */
97  fGuard.Signal();
98  }
99 
100  /* releasing the mutex reduces contention */
101  fGuard.Unlock();
102  Flush();
103  fGuard.Lock();
104  }
105  fGuard.Unlock();
106  } else {
107  jack_error("JackMessageBuffer::Execute lock cannot be taken");
108  }
109 
110  return false;
111 }
112 
113 void JackMessageBuffer::Create()
114 {
115  if (fInstance == NULL) {
116  fInstance = new JackMessageBuffer();
117  fInstance->Start();
118  }
119 }
120 
121 void JackMessageBuffer::Destroy()
122 {
123  if (fInstance != NULL) {
124  fInstance->Stop();
125  delete fInstance;
126  fInstance = NULL;
127  }
128 }
129 
130 void JackMessageBufferAdd(int level, const char *message)
131 {
132  if (Jack::JackMessageBuffer::fInstance == NULL) {
133  /* Unable to print message with realtime safety. Complain and print it anyway. */
134  jack_log_function(LOG_LEVEL_ERROR, "messagebuffer not initialized, skip message");
135  } else {
136  Jack::JackMessageBuffer::fInstance->AddMessage(level, message);
137  }
138 }
139 
140 void JackMessageBuffer::SetInitCallback(JackThreadInitCallback callback, void *arg)
141 {
142  if (fGuard.Lock()) {
143  /* set up the callback */
144  fInitArg = arg;
145  fInit = callback;
146  /* wake msg buffer thread */
147  fGuard.Signal();
148  /* wait for it to be done */
149  fGuard.Wait();
150  /* and we're done */
151  fGuard.Unlock();
152  } else {
153  jack_error("JackMessageBuffer::SetInitCallback lock cannot be taken");
154  }
155 }
156 
157 };
158