2 Copyright (C) 2001 Paul Davis
3 Copyright (C) 2004-2008 Grame
4 Copyright (C) 2013 Samsung Electronics
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 "JackAndroidThread.h"
23 #include "JackError.h"
25 #include "JackGlobals.h"
26 #include <string.h> // for memset
27 #include <unistd.h> // for _POSIX_PRIORITY_SCHEDULING check
30 #ifdef JACK_ANDROID_REALTIME_SCHED
31 #include "SchedulingPolicyService.h"
34 //#define JACK_SCHED_POLICY SCHED_RR
35 #define JACK_SCHED_POLICY SCHED_FIFO
40 void JackAndroidThread::thread_exit_handler(int sig
)
42 printf("this signal is %d \n", sig
);
46 void* JackAndroidThread::ThreadHandler(void* arg
)
48 JackAndroidThread
* obj
= (JackAndroidThread
*)arg
;
49 JackRunnableInterface
* runnable
= obj
->fRunnable
;
52 // Signal creation thread when started with StartSync
53 jack_log("JackAndroidThread::ThreadHandler : start");
54 obj
->fStatus
= kIniting
;
57 if (!runnable
->Init()) {
58 jack_error("Thread init fails: thread quits");
62 obj
->fStatus
= kRunning
;
64 // If Init succeed, start the thread loop
66 while (obj
->fStatus
== kRunning
&& res
) {
67 res
= runnable
->Execute();
70 jack_log("JackAndroidThread::ThreadHandler : exit");
72 return 0; // never reached
75 int JackAndroidThread::Start()
79 // Check if the thread was correctly started
80 if (StartImp(&fThread
, fPriority
, fRealTime
, ThreadHandler
, this) < 0) {
88 int JackAndroidThread::StartSync()
92 if (StartImp(&fThread
, fPriority
, fRealTime
, ThreadHandler
, this) < 0) {
97 while (fStatus
== kStarting
&& ++count
< 1000) {
100 return (count
== 1000) ? -1 : 0;
104 int JackAndroidThread::StartImp(jack_native_thread_t
* thread
, int priority
, int realtime
, void*(*start_routine
)(void*), void* arg
)
106 pthread_attr_t attributes
;
107 struct sched_param rt_param
;
108 pthread_attr_init(&attributes
);
111 struct sigaction actions
;
112 memset(&actions
, 0, sizeof(actions
));
113 sigemptyset(&actions
.sa_mask
);
114 actions
.sa_flags
= 0;
115 actions
.sa_handler
= thread_exit_handler
;
116 sigaction(SIGUSR1
,&actions
,NULL
);
118 if ((res
= pthread_attr_setdetachstate(&attributes
, PTHREAD_CREATE_JOINABLE
))) {
119 jack_error("Cannot request joinable thread creation for thread res = %d", res
);
123 if ((res
= pthread_attr_setscope(&attributes
, PTHREAD_SCOPE_SYSTEM
))) {
124 jack_error("Cannot set scheduling scope for thread res = %d", res
);
130 jack_log("JackAndroidThread::StartImp : create RT thread");
132 if ((res
= pthread_attr_setschedpolicy(&attributes
, JACK_SCHED_POLICY
))) {
133 jack_error("Cannot set RR scheduling class for RT thread res = %d", res
);
137 memset(&rt_param
, 0, sizeof(rt_param
));
138 rt_param
.sched_priority
= priority
;
140 if ((res
= pthread_attr_setschedparam(&attributes
, &rt_param
))) {
141 jack_error("Cannot set scheduling priority for RT thread res = %d", res
);
146 jack_log("JackAndroidThread::StartImp : create non RT thread");
149 if ((res
= pthread_attr_setstacksize(&attributes
, THREAD_STACK
))) {
150 jack_error("Cannot set thread stack size res = %d", res
);
154 if ((res
= JackGlobals::fJackThreadCreator(thread
, &attributes
, start_routine
, arg
))) {
155 jack_error("Cannot create thread res = %d", res
);
159 pthread_attr_destroy(&attributes
);
163 int JackAndroidThread::Kill()
165 if (fThread
!= (jack_native_thread_t
)NULL
) { // If thread has been started
166 jack_log("JackAndroidThread::Kill");
169 pthread_kill(fThread
, SIGUSR1
);
170 pthread_join(fThread
, &status
);
172 fThread
= (jack_native_thread_t
)NULL
;
179 int JackAndroidThread::Stop()
181 if (fThread
!= (jack_native_thread_t
)NULL
) { // If thread has been started
182 jack_log("JackAndroidThread::Stop");
184 fStatus
= kIdle
; // Request for the thread to stop
185 pthread_join(fThread
, &status
);
186 fThread
= (jack_native_thread_t
)NULL
;
193 int JackAndroidThread::KillImp(jack_native_thread_t thread
)
195 if (thread
!= (jack_native_thread_t
)NULL
) { // If thread has been started
196 jack_log("JackAndroidThread::Kill");
198 pthread_kill(thread
, SIGUSR1
);
199 pthread_join(thread
, &status
);
206 int JackAndroidThread::StopImp(jack_native_thread_t thread
)
208 if (thread
!= (jack_native_thread_t
)NULL
) { // If thread has been started
209 jack_log("JackAndroidThread::Stop");
211 pthread_join(thread
, &status
);
218 int JackAndroidThread::AcquireRealTime()
220 return (fThread
!= (jack_native_thread_t
)NULL
) ? AcquireRealTimeImp(fThread
, fPriority
) : -1;
223 int JackAndroidThread::AcquireSelfRealTime()
225 return AcquireRealTimeImp(pthread_self(), fPriority
);
228 int JackAndroidThread::AcquireRealTime(int priority
)
230 fPriority
= priority
;
231 return AcquireRealTime();
234 int JackAndroidThread::AcquireSelfRealTime(int priority
)
236 fPriority
= priority
;
237 return AcquireSelfRealTime();
239 int JackAndroidThread::AcquireRealTimeImp(jack_native_thread_t thread
, int priority
)
241 struct sched_param rtparam
;
243 memset(&rtparam
, 0, sizeof(rtparam
));
244 rtparam
.sched_priority
= priority
;
246 jack_log("JackAndroidThread::AcquireRealTimeImp priority = %d", priority
);
248 #ifndef JACK_ANDROID_REALTIME_SCHED
249 if ((res
= pthread_setschedparam(thread
, JACK_SCHED_POLICY
, &rtparam
)) != 0) {
250 jack_error("Cannot use real-time scheduling (RR/%d)"
251 "(%d: %s)", rtparam
.sched_priority
, res
,
256 if ((res
= android::requestPriority(getpid(), gettid(), priority
)) != 0) {
257 jack_log("Failed to get SCHED_FIFO priority pid %d tid %d; error %d",
258 getpid(), gettid(), res
);
265 int JackAndroidThread::DropRealTime()
267 return (fThread
!= (jack_native_thread_t
)NULL
) ? DropRealTimeImp(fThread
) : -1;
270 int JackAndroidThread::DropSelfRealTime()
272 return DropRealTimeImp(pthread_self());
275 int JackAndroidThread::DropRealTimeImp(jack_native_thread_t thread
)
277 struct sched_param rtparam
;
279 memset(&rtparam
, 0, sizeof(rtparam
));
280 rtparam
.sched_priority
= 0;
282 if ((res
= pthread_setschedparam(thread
, SCHED_OTHER
, &rtparam
)) != 0) {
283 jack_error("Cannot switch to normal scheduling priority(%s)", strerror(errno
));
289 jack_native_thread_t
JackAndroidThread::GetThreadID()
294 bool JackAndroidThread::IsThread()
296 return pthread_self() == fThread
;
299 void JackAndroidThread::Terminate()
301 jack_log("JackAndroidThread::Terminate");
305 SERVER_EXPORT
void ThreadExit()
307 jack_log("ThreadExit");
311 } // end of namespace
313 bool jack_get_thread_realtime_priority_range(int * min_ptr
, int * max_ptr
)
315 #if defined(_POSIX_PRIORITY_SCHEDULING) && !defined(__APPLE__)
318 min
= sched_get_priority_min(JACK_SCHED_POLICY
);
321 jack_error("sched_get_priority_min() failed.");
325 max
= sched_get_priority_max(JACK_SCHED_POLICY
);
328 jack_error("sched_get_priority_max() failed.");
341 bool jack_tls_allocate_key(jack_tls_key
*key_ptr
)
345 ret
= pthread_key_create(key_ptr
, NULL
);
348 jack_error("pthread_key_create() failed with error %d", ret
);
355 bool jack_tls_free_key(jack_tls_key key
)
359 ret
= pthread_key_delete(key
);
362 jack_error("pthread_key_delete() failed with error %d", ret
);
369 bool jack_tls_set(jack_tls_key key
, void *data_ptr
)
373 ret
= pthread_setspecific(key
, (const void *)data_ptr
);
376 jack_error("pthread_setspecific() failed with error %d", ret
);
383 void *jack_tls_get(jack_tls_key key
)
385 return pthread_getspecific(key
);