2 Copyright (C) 2001 Paul Davis
3 Copyright (C) 2004-2008 Grame
5 This program is free software; you can redistribute it and/or modify
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
8 (at your option) any later version.
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
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include "JackPosixThread.h"
22 #include "JackError.h"
24 #include "JackGlobals.h"
25 #include <string.h> // for memset
26 #include <unistd.h> // for _POSIX_PRIORITY_SCHEDULING check
28 //#define JACK_SCHED_POLICY SCHED_RR
29 #define JACK_SCHED_POLICY SCHED_FIFO
34 void* JackPosixThread::ThreadHandler(void* arg
)
36 JackPosixThread
* obj
= (JackPosixThread
*)arg
;
37 JackRunnableInterface
* runnable
= obj
->fRunnable
;
40 if ((err
= pthread_setcanceltype(obj
->fCancellation
, NULL
)) != 0) {
41 jack_error("pthread_setcanceltype err = %s", strerror(err
));
44 // Signal creation thread when started with StartSync
45 jack_log("JackPosixThread::ThreadHandler : start");
46 obj
->fStatus
= kIniting
;
49 if (!runnable
->Init()) {
50 jack_error("Thread init fails: thread quits");
54 obj
->fStatus
= kRunning
;
56 // If Init succeed, start the thread loop
58 while (obj
->fStatus
== kRunning
&& res
) {
59 res
= runnable
->Execute();
62 jack_log("JackPosixThread::ThreadHandler : exit");
64 return 0; // never reached
67 int JackPosixThread::Start()
71 // Check if the thread was correctly started
72 if (StartImp(&fThread
, fPriority
, fRealTime
, ThreadHandler
, this) < 0) {
80 int JackPosixThread::StartSync()
84 if (StartImp(&fThread
, fPriority
, fRealTime
, ThreadHandler
, this) < 0) {
89 while (fStatus
== kStarting
&& ++count
< 1000) {
92 return (count
== 1000) ? -1 : 0;
96 int JackPosixThread::StartImp(jack_native_thread_t
* thread
, int priority
, int realtime
, void*(*start_routine
)(void*), void* arg
)
98 pthread_attr_t attributes
;
99 struct sched_param rt_param
;
100 pthread_attr_init(&attributes
);
103 if ((res
= pthread_attr_setdetachstate(&attributes
, PTHREAD_CREATE_JOINABLE
))) {
104 jack_error("Cannot request joinable thread creation for thread res = %d", res
);
108 if ((res
= pthread_attr_setscope(&attributes
, PTHREAD_SCOPE_SYSTEM
))) {
109 jack_error("Cannot set scheduling scope for thread res = %d", res
);
115 jack_log("JackPosixThread::StartImp : create RT thread");
117 if ((res
= pthread_attr_setinheritsched(&attributes
, PTHREAD_EXPLICIT_SCHED
))) {
118 jack_error("Cannot request explicit scheduling for RT thread res = %d", res
);
122 if ((res
= pthread_attr_setschedpolicy(&attributes
, JACK_SCHED_POLICY
))) {
123 jack_error("Cannot set RR scheduling class for RT thread res = %d", res
);
127 memset(&rt_param
, 0, sizeof(rt_param
));
128 rt_param
.sched_priority
= priority
;
130 if ((res
= pthread_attr_setschedparam(&attributes
, &rt_param
))) {
131 jack_error("Cannot set scheduling priority for RT thread res = %d", res
);
136 jack_log("JackPosixThread::StartImp : create non RT thread");
137 if ((res
= pthread_attr_setinheritsched(&attributes
, PTHREAD_EXPLICIT_SCHED
))) {
138 jack_log("Cannot request explicit scheduling for non RT thread res = %d", res
);
142 if ((res
= pthread_attr_setstacksize(&attributes
, THREAD_STACK
))) {
143 jack_error("Cannot set thread stack size res = %d", res
);
147 if ((res
= JackGlobals::fJackThreadCreator(thread
, &attributes
, start_routine
, arg
))) {
148 jack_error("Cannot create thread res = %d", res
);
152 pthread_attr_destroy(&attributes
);
156 int JackPosixThread::Kill()
158 if (fThread
!= (jack_native_thread_t
)NULL
) { // If thread has been started
159 jack_log("JackPosixThread::Kill");
161 pthread_cancel(fThread
);
162 pthread_join(fThread
, &status
);
164 fThread
= (jack_native_thread_t
)NULL
;
171 int JackPosixThread::Stop()
173 if (fThread
!= (jack_native_thread_t
)NULL
) { // If thread has been started
174 jack_log("JackPosixThread::Stop");
176 fStatus
= kIdle
; // Request for the thread to stop
177 pthread_join(fThread
, &status
);
178 fThread
= (jack_native_thread_t
)NULL
;
185 int JackPosixThread::KillImp(jack_native_thread_t thread
)
187 if (thread
!= (jack_native_thread_t
)NULL
) { // If thread has been started
188 jack_log("JackPosixThread::Kill");
190 pthread_cancel(thread
);
191 pthread_join(thread
, &status
);
198 int JackPosixThread::StopImp(jack_native_thread_t thread
)
200 if (thread
!= (jack_native_thread_t
)NULL
) { // If thread has been started
201 jack_log("JackPosixThread::Stop");
203 pthread_join(thread
, &status
);
210 int JackPosixThread::AcquireRealTime()
212 return (fThread
!= (jack_native_thread_t
)NULL
) ? AcquireRealTimeImp(fThread
, fPriority
) : -1;
215 int JackPosixThread::AcquireSelfRealTime()
217 return AcquireRealTimeImp(pthread_self(), fPriority
);
220 int JackPosixThread::AcquireRealTime(int priority
)
222 fPriority
= priority
;
223 return AcquireRealTime();
226 int JackPosixThread::AcquireSelfRealTime(int priority
)
228 fPriority
= priority
;
229 return AcquireSelfRealTime();
231 int JackPosixThread::AcquireRealTimeImp(jack_native_thread_t thread
, int priority
)
233 struct sched_param rtparam
;
235 memset(&rtparam
, 0, sizeof(rtparam
));
236 rtparam
.sched_priority
= priority
;
238 jack_log("JackPosixThread::AcquireRealTimeImp priority = %d", priority
);
240 if ((res
= pthread_setschedparam(thread
, JACK_SCHED_POLICY
, &rtparam
)) != 0) {
241 jack_error("Cannot use real-time scheduling (RR/%d)"
242 "(%d: %s)", rtparam
.sched_priority
, res
,
249 int JackPosixThread::DropRealTime()
251 return (fThread
!= (jack_native_thread_t
)NULL
) ? DropRealTimeImp(fThread
) : -1;
254 int JackPosixThread::DropSelfRealTime()
256 return DropRealTimeImp(pthread_self());
259 int JackPosixThread::DropRealTimeImp(jack_native_thread_t thread
)
261 struct sched_param rtparam
;
263 memset(&rtparam
, 0, sizeof(rtparam
));
264 rtparam
.sched_priority
= 0;
266 if ((res
= pthread_setschedparam(thread
, SCHED_OTHER
, &rtparam
)) != 0) {
267 jack_error("Cannot switch to normal scheduling priority(%s)", strerror(errno
));
273 jack_native_thread_t
JackPosixThread::GetThreadID()
278 bool JackPosixThread::IsThread()
280 return pthread_self() == fThread
;
283 void JackPosixThread::Terminate()
285 jack_log("JackPosixThread::Terminate");
289 SERVER_EXPORT
void ThreadExit()
291 jack_log("ThreadExit");
295 } // end of namespace
297 bool jack_get_thread_realtime_priority_range(int * min_ptr
, int * max_ptr
)
299 #if defined(_POSIX_PRIORITY_SCHEDULING) && !defined(__APPLE__)
302 min
= sched_get_priority_min(JACK_SCHED_POLICY
);
305 jack_error("sched_get_priority_min() failed.");
309 max
= sched_get_priority_max(JACK_SCHED_POLICY
);
312 jack_error("sched_get_priority_max() failed.");
325 bool jack_tls_allocate_key(jack_tls_key
*key_ptr
)
329 ret
= pthread_key_create(key_ptr
, NULL
);
332 jack_error("pthread_key_create() failed with error %d", ret
);
339 bool jack_tls_free_key(jack_tls_key key
)
343 ret
= pthread_key_delete(key
);
346 jack_error("pthread_key_delete() failed with error %d", ret
);
353 bool jack_tls_set(jack_tls_key key
, void *data_ptr
)
357 ret
= pthread_setspecific(key
, (const void *)data_ptr
);
360 jack_error("pthread_setspecific() failed with error %d", ret
);
367 void *jack_tls_get(jack_tls_key key
)
369 return pthread_getspecific(key
);