Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 368 lines (302 sloc) 9.212 kb
73e2d7a First import
sletz authored
1 /*
ecb8da5 Beautifull code
sletz authored
2 Copyright (C) 2001 Paul Davis
254f765 Update date
sletz authored
3 Copyright (C) 2004-2008 Grame
73e2d7a First import
sletz authored
4
5 This program is free software; you can redistribute it and/or modify
b047c26 All library side files become LGPL
sletz authored
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
73e2d7a First import
sletz authored
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b047c26 All library side files become LGPL
sletz authored
13 GNU Lesser General Public License for more details.
73e2d7a First import
sletz authored
14
b047c26 All library side files become LGPL
sletz authored
15 You should have received a copy of the GNU Lesser General Public License
48b51ef Synchronize jack.h with JACK1, use jack_native_thread_t type in POSIX an...
sletz authored
16 along with this program; if not, write to the Free Software
b047c26 All library side files become LGPL
sletz authored
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
73e2d7a First import
sletz authored
18
19 */
20
21 #include "JackPosixThread.h"
22 #include "JackError.h"
ef8bdde Use StartSync to start the client notification thread, otherwise initial...
sletz authored
23 #include "JackTime.h"
4086b3c Virtualize and allow overriding of thread creation function, to allow Wi...
sletz authored
24 #include "JackGlobals.h"
73e2d7a First import
sletz authored
25 #include <string.h> // for memset
606609f range constraint for realtime-priority engine parameter
nedko authored
26 #include <unistd.h> // for _POSIX_PRIORITY_SCHEDULING check
27
05d5411 JACK_SCHED_POLICY switched to SCHED_FIFO.
sletz authored
28 //#define JACK_SCHED_POLICY SCHED_RR
29 #define JACK_SCHED_POLICY SCHED_FIFO
73e2d7a First import
sletz authored
30
31 namespace Jack
32 {
33
34 void* JackPosixThread::ThreadHandler(void* arg)
35 {
36 JackPosixThread* obj = (JackPosixThread*)arg;
37 JackRunnableInterface* runnable = obj->fRunnable;
38 int err;
39
40 if ((err = pthread_setcanceltype(obj->fCancellation, NULL)) != 0) {
41 jack_error("pthread_setcanceltype err = %s", strerror(err));
42 }
48b51ef Synchronize jack.h with JACK1, use jack_native_thread_t type in POSIX an...
sletz authored
43
ef8bdde Use StartSync to start the client notification thread, otherwise initial...
sletz authored
44 // Signal creation thread when started with StartSync
45 jack_log("ThreadHandler: start");
d747010 Correct thread init phase
sletz authored
46 obj->fStatus = kIniting;
48b51ef Synchronize jack.h with JACK1, use jack_native_thread_t type in POSIX an...
sletz authored
47
73e2d7a First import
sletz authored
48 // Call Init method
49 if (!runnable->Init()) {
50 jack_error("Thread init fails: thread quits");
51 return 0;
52 }
48b51ef Synchronize jack.h with JACK1, use jack_native_thread_t type in POSIX an...
sletz authored
53
d747010 Correct thread init phase
sletz authored
54 obj->fStatus = kRunning;
48b51ef Synchronize jack.h with JACK1, use jack_native_thread_t type in POSIX an...
sletz authored
55
ecb8da5 Beautifull code
sletz authored
56 // If Init succeed, start the thread loop
57 bool res = true;
931148f Better handling of init and run state in JackThread.
sletz authored
58 while (obj->fStatus == kRunning && res) {
308d46a Correct thread code
sletz authored
59 res = runnable->Execute();
60 }
73e2d7a First import
sletz authored
61
d2f7053 Nedko Arnaudov log patch(2).
sletz authored
62 jack_log("ThreadHandler: exit");
0a1f18f Correct JackPosixThread::ThreadHandler termination, do not set buffer si...
sletz authored
63 pthread_exit(0);
64 return 0; // never reached
73e2d7a First import
sletz authored
65 }
66
67 int JackPosixThread::Start()
68 {
931148f Better handling of init and run state in JackThread.
sletz authored
69 fStatus = kStarting;
ecb8da5 Beautifull code
sletz authored
70
71 // Check if the thread was correctly started
72 if (StartImp(&fThread, fPriority, fRealTime, ThreadHandler, this) < 0) {
931148f Better handling of init and run state in JackThread.
sletz authored
73 fStatus = kIdle;
ecb8da5 Beautifull code
sletz authored
74 return -1;
75 } else {
76 return 0;
77 }
6eff0aa Implement thread.h API
sletz authored
78 }
48b51ef Synchronize jack.h with JACK1, use jack_native_thread_t type in POSIX an...
sletz authored
79
ef8bdde Use StartSync to start the client notification thread, otherwise initial...
sletz authored
80 int JackPosixThread::StartSync()
81 {
82 fStatus = kStarting;
48b51ef Synchronize jack.h with JACK1, use jack_native_thread_t type in POSIX an...
sletz authored
83
ef8bdde Use StartSync to start the client notification thread, otherwise initial...
sletz authored
84 if (StartImp(&fThread, fPriority, fRealTime, ThreadHandler, this) < 0) {
85 fStatus = kIdle;
86 return -1;
87 } else {
88 int count = 0;
d747010 Correct thread init phase
sletz authored
89 while (fStatus == kStarting && ++count < 1000) {
ef8bdde Use StartSync to start the client notification thread, otherwise initial...
sletz authored
90 JackSleep(1000);
91 }
92 return (count == 1000) ? -1 : 0;
48b51ef Synchronize jack.h with JACK1, use jack_native_thread_t type in POSIX an...
sletz authored
93 }
ef8bdde Use StartSync to start the client notification thread, otherwise initial...
sletz authored
94 }
48b51ef Synchronize jack.h with JACK1, use jack_native_thread_t type in POSIX an...
sletz authored
95
96 int JackPosixThread::StartImp(jack_native_thread_t* thread, int priority, int realtime, void*(*start_routine)(void*), void* arg)
6eff0aa Implement thread.h API
sletz authored
97 {
a888803 Fix JackPosixThread::StartImp and JackWinThread::StartImp.
sletz authored
98 pthread_attr_t attributes;
99 struct sched_param rt_param;
100 pthread_attr_init(&attributes);
6eff0aa Implement thread.h API
sletz authored
101 int res;
ecb8da5 Beautifull code
sletz authored
102
a888803 Fix JackPosixThread::StartImp and JackWinThread::StartImp.
sletz authored
103 if ((res = pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_JOINABLE))) {
04668c1 Correct JackPosixThread::StartImp : thread priority setting now done in ...
sletz authored
104 jack_error("Cannot request joinable thread creation for thread res = %d", res);
a888803 Fix JackPosixThread::StartImp and JackWinThread::StartImp.
sletz authored
105 return -1;
106 }
ecb8da5 Beautifull code
sletz authored
107
a888803 Fix JackPosixThread::StartImp and JackWinThread::StartImp.
sletz authored
108 if ((res = pthread_attr_setscope(&attributes, PTHREAD_SCOPE_SYSTEM))) {
04668c1 Correct JackPosixThread::StartImp : thread priority setting now done in ...
sletz authored
109 jack_error("Cannot set scheduling scope for thread res = %d", res);
a888803 Fix JackPosixThread::StartImp and JackWinThread::StartImp.
sletz authored
110 return -1;
111 }
73e2d7a First import
sletz authored
112
a888803 Fix JackPosixThread::StartImp and JackWinThread::StartImp.
sletz authored
113 if (realtime) {
48b51ef Synchronize jack.h with JACK1, use jack_native_thread_t type in POSIX an...
sletz authored
114
a888803 Fix JackPosixThread::StartImp and JackWinThread::StartImp.
sletz authored
115 jack_log("Create RT thread");
e33da41 Correct JackPosixThread::StartImp
sletz authored
116
327c590 Cleanup
sletz authored
117 if ((res = pthread_attr_setinheritsched(&attributes, PTHREAD_EXPLICIT_SCHED))) {
04668c1 Correct JackPosixThread::StartImp : thread priority setting now done in ...
sletz authored
118 jack_error("Cannot request explicit scheduling for RT thread res = %d", res);
e33da41 Correct JackPosixThread::StartImp
sletz authored
119 return -1;
1a44ede @torbenh3 fix tabs and convert em to spaces.
torbenh3 authored
120 }
48b51ef Synchronize jack.h with JACK1, use jack_native_thread_t type in POSIX an...
sletz authored
121
606609f range constraint for realtime-priority engine parameter
nedko authored
122 if ((res = pthread_attr_setschedpolicy(&attributes, JACK_SCHED_POLICY))) {
04668c1 Correct JackPosixThread::StartImp : thread priority setting now done in ...
sletz authored
123 jack_error("Cannot set RR scheduling class for RT thread res = %d", res);
73e2d7a First import
sletz authored
124 return -1;
125 }
48b51ef Synchronize jack.h with JACK1, use jack_native_thread_t type in POSIX an...
sletz authored
126
04668c1 Correct JackPosixThread::StartImp : thread priority setting now done in ...
sletz authored
127 memset(&rt_param, 0, sizeof(rt_param));
128 rt_param.sched_priority = priority;
ecb8da5 Beautifull code
sletz authored
129
04668c1 Correct JackPosixThread::StartImp : thread priority setting now done in ...
sletz authored
130 if ((res = pthread_attr_setschedparam(&attributes, &rt_param))) {
131 jack_error("Cannot set scheduling priority for RT thread res = %d", res);
132 return -1;
133 }
73e2d7a First import
sletz authored
134
04668c1 Correct JackPosixThread::StartImp : thread priority setting now done in ...
sletz authored
135 } else {
136 jack_log("Create non RT thread");
a888803 Fix JackPosixThread::StartImp and JackWinThread::StartImp.
sletz authored
137 }
73e2d7a First import
sletz authored
138
a888803 Fix JackPosixThread::StartImp and JackWinThread::StartImp.
sletz authored
139 if ((res = pthread_attr_setstacksize(&attributes, THREAD_STACK))) {
04668c1 Correct JackPosixThread::StartImp : thread priority setting now done in ...
sletz authored
140 jack_error("Cannot set thread stack size res = %d", res);
a888803 Fix JackPosixThread::StartImp and JackWinThread::StartImp.
sletz authored
141 return -1;
142 }
73e2d7a First import
sletz authored
143
4086b3c Virtualize and allow overriding of thread creation function, to allow Wi...
sletz authored
144 if ((res = JackGlobals::fJackThreadCreator(thread, &attributes, start_routine, arg))) {
04668c1 Correct JackPosixThread::StartImp : thread priority setting now done in ...
sletz authored
145 jack_error("Cannot create thread res = %d", res);
a888803 Fix JackPosixThread::StartImp and JackWinThread::StartImp.
sletz authored
146 return -1;
73e2d7a First import
sletz authored
147 }
a888803 Fix JackPosixThread::StartImp and JackWinThread::StartImp.
sletz authored
148
004016f Rework the mutex/signal classes. Use them in JackMessageBuffer.
sletz authored
149 pthread_attr_destroy(&attributes);
a888803 Fix JackPosixThread::StartImp and JackWinThread::StartImp.
sletz authored
150 return 0;
73e2d7a First import
sletz authored
151 }
152
153 int JackPosixThread::Kill()
154 {
48b51ef Synchronize jack.h with JACK1, use jack_native_thread_t type in POSIX an...
sletz authored
155 if (fThread != (jack_native_thread_t)NULL) { // If thread has been started
d2f7053 Nedko Arnaudov log patch(2).
sletz authored
156 jack_log("JackPosixThread::Kill");
73e2d7a First import
sletz authored
157 void* status;
158 pthread_cancel(fThread);
308d46a Correct thread code
sletz authored
159 pthread_join(fThread, &status);
931148f Better handling of init and run state in JackThread.
sletz authored
160 fStatus = kIdle;
48b51ef Synchronize jack.h with JACK1, use jack_native_thread_t type in POSIX an...
sletz authored
161 fThread = (jack_native_thread_t)NULL;
73e2d7a First import
sletz authored
162 return 0;
163 } else {
164 return -1;
165 }
166 }
167
168 int JackPosixThread::Stop()
169 {
48b51ef Synchronize jack.h with JACK1, use jack_native_thread_t type in POSIX an...
sletz authored
170 if (fThread != (jack_native_thread_t)NULL) { // If thread has been started
d2f7053 Nedko Arnaudov log patch(2).
sletz authored
171 jack_log("JackPosixThread::Stop");
73e2d7a First import
sletz authored
172 void* status;
931148f Better handling of init and run state in JackThread.
sletz authored
173 fStatus = kIdle; // Request for the thread to stop
73e2d7a First import
sletz authored
174 pthread_join(fThread, &status);
48b51ef Synchronize jack.h with JACK1, use jack_native_thread_t type in POSIX an...
sletz authored
175 fThread = (jack_native_thread_t)NULL;
73e2d7a First import
sletz authored
176 return 0;
177 } else {
178 return -1;
179 }
180 }
181
48b51ef Synchronize jack.h with JACK1, use jack_native_thread_t type in POSIX an...
sletz authored
182 int JackPosixThread::KillImp(jack_native_thread_t thread)
8b5911a Add new jack_client_stop_thread and jack_client_kill_thread API.
sletz authored
183 {
48b51ef Synchronize jack.h with JACK1, use jack_native_thread_t type in POSIX an...
sletz authored
184 if (thread != (jack_native_thread_t)NULL) { // If thread has been started
8b5911a Add new jack_client_stop_thread and jack_client_kill_thread API.
sletz authored
185 jack_log("JackPosixThread::Kill");
186 void* status;
187 pthread_cancel(thread);
188 pthread_join(thread, &status);
189 return 0;
190 } else {
191 return -1;
192 }
193 }
194
48b51ef Synchronize jack.h with JACK1, use jack_native_thread_t type in POSIX an...
sletz authored
195 int JackPosixThread::StopImp(jack_native_thread_t thread)
8b5911a Add new jack_client_stop_thread and jack_client_kill_thread API.
sletz authored
196 {
48b51ef Synchronize jack.h with JACK1, use jack_native_thread_t type in POSIX an...
sletz authored
197 if (thread != (jack_native_thread_t)NULL) { // If thread has been started
8b5911a Add new jack_client_stop_thread and jack_client_kill_thread API.
sletz authored
198 jack_log("JackPosixThread::Stop");
199 void* status;
200 pthread_join(thread, &status);
201 return 0;
202 } else {
203 return -1;
204 }
205 }
206
73e2d7a First import
sletz authored
207 int JackPosixThread::AcquireRealTime()
208 {
48b51ef Synchronize jack.h with JACK1, use jack_native_thread_t type in POSIX an...
sletz authored
209 return (fThread != (jack_native_thread_t)NULL) ? AcquireRealTimeImp(fThread, fPriority) : -1;
a5c2789 Fix AcquireRealTime and DropRealTime: now distinguish when called from a...
sletz authored
210 }
211
212 int JackPosixThread::AcquireSelfRealTime()
213 {
1ff726f Thread AcquireRealTime and DropRealTime were (incorrectly) using fThread...
sletz authored
214 return AcquireRealTimeImp(pthread_self(), fPriority);
6eff0aa Implement thread.h API
sletz authored
215 }
73e2d7a First import
sletz authored
216
6eff0aa Implement thread.h API
sletz authored
217 int JackPosixThread::AcquireRealTime(int priority)
218 {
ecb8da5 Beautifull code
sletz authored
219 fPriority = priority;
6eff0aa Implement thread.h API
sletz authored
220 return AcquireRealTime();
221 }
73e2d7a First import
sletz authored
222
a5c2789 Fix AcquireRealTime and DropRealTime: now distinguish when called from a...
sletz authored
223 int JackPosixThread::AcquireSelfRealTime(int priority)
224 {
225 fPriority = priority;
226 return AcquireSelfRealTime();
227 }
48b51ef Synchronize jack.h with JACK1, use jack_native_thread_t type in POSIX an...
sletz authored
228 int JackPosixThread::AcquireRealTimeImp(jack_native_thread_t thread, int priority)
6eff0aa Implement thread.h API
sletz authored
229 {
ecb8da5 Beautifull code
sletz authored
230 struct sched_param rtparam;
6eff0aa Implement thread.h API
sletz authored
231 int res;
73e2d7a First import
sletz authored
232 memset(&rtparam, 0, sizeof(rtparam));
6eff0aa Implement thread.h API
sletz authored
233 rtparam.sched_priority = priority;
73e2d7a First import
sletz authored
234
a49f53c More messages in verbose mode.
sletz authored
235 jack_log("JackPosixThread::AcquireRealTimeImp priority = %d", priority);
236
606609f range constraint for realtime-priority engine parameter
nedko authored
237 if ((res = pthread_setschedparam(thread, JACK_SCHED_POLICY, &rtparam)) != 0) {
8a7739e Support for BIG_ENDIAN machines in NetJack2.
sletz authored
238 jack_error("Cannot use real-time scheduling (RR/%d)"
73e2d7a First import
sletz authored
239 "(%d: %s)", rtparam.sched_priority, res,
240 strerror(res));
241 return -1;
242 }
243 return 0;
244 }
245
6eff0aa Implement thread.h API
sletz authored
246 int JackPosixThread::DropRealTime()
73e2d7a First import
sletz authored
247 {
48b51ef Synchronize jack.h with JACK1, use jack_native_thread_t type in POSIX an...
sletz authored
248 return (fThread != (jack_native_thread_t)NULL) ? DropRealTimeImp(fThread) : -1;
a5c2789 Fix AcquireRealTime and DropRealTime: now distinguish when called from a...
sletz authored
249 }
250
251 int JackPosixThread::DropSelfRealTime()
252 {
1ff726f Thread AcquireRealTime and DropRealTime were (incorrectly) using fThread...
sletz authored
253 return DropRealTimeImp(pthread_self());
73e2d7a First import
sletz authored
254 }
255
48b51ef Synchronize jack.h with JACK1, use jack_native_thread_t type in POSIX an...
sletz authored
256 int JackPosixThread::DropRealTimeImp(jack_native_thread_t thread)
73e2d7a First import
sletz authored
257 {
ecb8da5 Beautifull code
sletz authored
258 struct sched_param rtparam;
73e2d7a First import
sletz authored
259 int res;
260 memset(&rtparam, 0, sizeof(rtparam));
261 rtparam.sched_priority = 0;
262
6eff0aa Implement thread.h API
sletz authored
263 if ((res = pthread_setschedparam(thread, SCHED_OTHER, &rtparam)) != 0) {
8a7739e Support for BIG_ENDIAN machines in NetJack2.
sletz authored
264 jack_error("Cannot switch to normal scheduling priority(%s)", strerror(errno));
73e2d7a First import
sletz authored
265 return -1;
266 }
267 return 0;
268 }
269
48b51ef Synchronize jack.h with JACK1, use jack_native_thread_t type in POSIX an...
sletz authored
270 jack_native_thread_t JackPosixThread::GetThreadID()
73e2d7a First import
sletz authored
271 {
272 return fThread;
273 }
274
b57d07d @torbenh3 another step for jack-session...
torbenh3 authored
275 bool JackPosixThread::IsThread()
276 {
277 return pthread_self() == fThread;
278 }
279
82bdd90 Remove checking thread in CoreAudio driver, better device state change r...
sletz authored
280 void JackPosixThread::Terminate()
281 {
d2f7053 Nedko Arnaudov log patch(2).
sletz authored
282 jack_log("JackPosixThread::Terminate");
ecb8da5 Beautifull code
sletz authored
283 pthread_exit(0);
82bdd90 Remove checking thread in CoreAudio driver, better device state change r...
sletz authored
284 }
285
bfdf50d Better symbols export for server and client side libraries.
sletz authored
286 SERVER_EXPORT void ThreadExit()
713e8df Ugly temporary fix for netmanager deadlock.
sletz authored
287 {
288 jack_log("ThreadExit");
289 pthread_exit(0);
290 }
291
73e2d7a First import
sletz authored
292 } // end of namespace
293
606609f range constraint for realtime-priority engine parameter
nedko authored
294 bool jack_get_thread_realtime_priority_range(int * min_ptr, int * max_ptr)
295 {
296 #if defined(_POSIX_PRIORITY_SCHEDULING) && !defined(__APPLE__)
297 int min, max;
298
299 min = sched_get_priority_min(JACK_SCHED_POLICY);
300 if (min == -1)
301 {
302 jack_error("sched_get_priority_min() failed.");
303 return false;
304 }
305
306 max = sched_get_priority_max(JACK_SCHED_POLICY);
307 if (max == -1)
308 {
309 jack_error("sched_get_priority_max() failed.");
310 return false;
311 }
312
313 *min_ptr = min;
314 *max_ptr = max;
315
316 return true;
317 #else
318 return false;
319 #endif
320 }
321
bfdf50d Better symbols export for server and client side libraries.
sletz authored
322 bool jack_tls_allocate_key(jack_tls_key *key_ptr)
a74f1a5 Better handling of graph state read functions : never wait when used in ...
sletz authored
323 {
324 int ret;
48b51ef Synchronize jack.h with JACK1, use jack_native_thread_t type in POSIX an...
sletz authored
325
a74f1a5 Better handling of graph state read functions : never wait when used in ...
sletz authored
326 ret = pthread_key_create(key_ptr, NULL);
327 if (ret != 0)
328 {
04668c1 Correct JackPosixThread::StartImp : thread priority setting now done in ...
sletz authored
329 jack_error("pthread_key_create() failed with error %d", ret);
a74f1a5 Better handling of graph state read functions : never wait when used in ...
sletz authored
330 return false;
331 }
48b51ef Synchronize jack.h with JACK1, use jack_native_thread_t type in POSIX an...
sletz authored
332
a74f1a5 Better handling of graph state read functions : never wait when used in ...
sletz authored
333 return true;
334 }
335
bfdf50d Better symbols export for server and client side libraries.
sletz authored
336 bool jack_tls_free_key(jack_tls_key key)
a74f1a5 Better handling of graph state read functions : never wait when used in ...
sletz authored
337 {
338 int ret;
48b51ef Synchronize jack.h with JACK1, use jack_native_thread_t type in POSIX an...
sletz authored
339
a74f1a5 Better handling of graph state read functions : never wait when used in ...
sletz authored
340 ret = pthread_key_delete(key);
341 if (ret != 0)
342 {
04668c1 Correct JackPosixThread::StartImp : thread priority setting now done in ...
sletz authored
343 jack_error("pthread_key_delete() failed with error %d", ret);
a74f1a5 Better handling of graph state read functions : never wait when used in ...
sletz authored
344 return false;
345 }
48b51ef Synchronize jack.h with JACK1, use jack_native_thread_t type in POSIX an...
sletz authored
346
a74f1a5 Better handling of graph state read functions : never wait when used in ...
sletz authored
347 return true;
348 }
349
bfdf50d Better symbols export for server and client side libraries.
sletz authored
350 bool jack_tls_set(jack_tls_key key, void *data_ptr)
a74f1a5 Better handling of graph state read functions : never wait when used in ...
sletz authored
351 {
352 int ret;
48b51ef Synchronize jack.h with JACK1, use jack_native_thread_t type in POSIX an...
sletz authored
353
a74f1a5 Better handling of graph state read functions : never wait when used in ...
sletz authored
354 ret = pthread_setspecific(key, (const void *)data_ptr);
355 if (ret != 0)
356 {
04668c1 Correct JackPosixThread::StartImp : thread priority setting now done in ...
sletz authored
357 jack_error("pthread_setspecific() failed with error %d", ret);
a74f1a5 Better handling of graph state read functions : never wait when used in ...
sletz authored
358 return false;
359 }
48b51ef Synchronize jack.h with JACK1, use jack_native_thread_t type in POSIX an...
sletz authored
360
a74f1a5 Better handling of graph state read functions : never wait when used in ...
sletz authored
361 return true;
362 }
363
bfdf50d Better symbols export for server and client side libraries.
sletz authored
364 void *jack_tls_get(jack_tls_key key)
a74f1a5 Better handling of graph state read functions : never wait when used in ...
sletz authored
365 {
366 return pthread_getspecific(key);
367 }
Something went wrong with that request. Please try again.