If pthread_setschedparam() in JackPosixThread::AcquireRealTimeImp() fails
[jack2.git] / android / JackAndroidThread.cpp
blobb30dc7894a442fb15731aa4f48e046a5e0c15960
1 /*
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"
24 #include "JackTime.h"
25 #include "JackGlobals.h"
26 #include <string.h> // for memset
27 #include <unistd.h> // for _POSIX_PRIORITY_SCHEDULING check
28 #include <signal.h>
30 #ifdef JACK_ANDROID_REALTIME_SCHED
31 #include "SchedulingPolicyService.h"
32 #endif
34 //#define JACK_SCHED_POLICY SCHED_RR
35 #define JACK_SCHED_POLICY SCHED_FIFO
37 namespace Jack
40 void JackAndroidThread::thread_exit_handler(int sig)
42 printf("this signal is %d \n", sig);
43 pthread_exit(0);
46 void* JackAndroidThread::ThreadHandler(void* arg)
48 JackAndroidThread* obj = (JackAndroidThread*)arg;
49 JackRunnableInterface* runnable = obj->fRunnable;
50 int err;
52 // Signal creation thread when started with StartSync
53 jack_log("JackAndroidThread::ThreadHandler : start");
54 obj->fStatus = kIniting;
56 // Call Init method
57 if (!runnable->Init()) {
58 jack_error("Thread init fails: thread quits");
59 return 0;
62 obj->fStatus = kRunning;
64 // If Init succeed, start the thread loop
65 bool res = true;
66 while (obj->fStatus == kRunning && res) {
67 res = runnable->Execute();
70 jack_log("JackAndroidThread::ThreadHandler : exit");
71 pthread_exit(0);
72 return 0; // never reached
75 int JackAndroidThread::Start()
77 fStatus = kStarting;
79 // Check if the thread was correctly started
80 if (StartImp(&fThread, fPriority, fRealTime, ThreadHandler, this) < 0) {
81 fStatus = kIdle;
82 return -1;
83 } else {
84 return 0;
88 int JackAndroidThread::StartSync()
90 fStatus = kStarting;
92 if (StartImp(&fThread, fPriority, fRealTime, ThreadHandler, this) < 0) {
93 fStatus = kIdle;
94 return -1;
95 } else {
96 int count = 0;
97 while (fStatus == kStarting && ++count < 1000) {
98 JackSleep(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);
109 int res;
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);
120 return -1;
123 if ((res = pthread_attr_setscope(&attributes, PTHREAD_SCOPE_SYSTEM))) {
124 jack_error("Cannot set scheduling scope for thread res = %d", res);
125 return -1;
128 if (realtime) {
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);
134 return -1;
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);
142 return -1;
145 } else {
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);
151 return -1;
154 if ((res = JackGlobals::fJackThreadCreator(thread, &attributes, start_routine, arg))) {
155 jack_error("Cannot create thread res = %d", res);
156 return -1;
159 pthread_attr_destroy(&attributes);
160 return 0;
163 int JackAndroidThread::Kill()
165 if (fThread != (jack_native_thread_t)NULL) { // If thread has been started
166 jack_log("JackAndroidThread::Kill");
167 void* status;
169 pthread_kill(fThread, SIGUSR1);
170 pthread_join(fThread, &status);
171 fStatus = kIdle;
172 fThread = (jack_native_thread_t)NULL;
173 return 0;
174 } else {
175 return -1;
179 int JackAndroidThread::Stop()
181 if (fThread != (jack_native_thread_t)NULL) { // If thread has been started
182 jack_log("JackAndroidThread::Stop");
183 void* status;
184 fStatus = kIdle; // Request for the thread to stop
185 pthread_join(fThread, &status);
186 fThread = (jack_native_thread_t)NULL;
187 return 0;
188 } else {
189 return -1;
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");
197 void* status;
198 pthread_kill(thread, SIGUSR1);
199 pthread_join(thread, &status);
200 return 0;
201 } else {
202 return -1;
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");
210 void* status;
211 pthread_join(thread, &status);
212 return 0;
213 } else {
214 return -1;
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;
242 int res;
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,
252 strerror(res));
253 return -1;
255 #else
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);
259 return -1;
261 #endif
262 return 0;
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;
278 int res;
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));
284 return -1;
286 return 0;
289 jack_native_thread_t JackAndroidThread::GetThreadID()
291 return fThread;
294 bool JackAndroidThread::IsThread()
296 return pthread_self() == fThread;
299 void JackAndroidThread::Terminate()
301 jack_log("JackAndroidThread::Terminate");
302 pthread_exit(0);
305 SERVER_EXPORT void ThreadExit()
307 jack_log("ThreadExit");
308 pthread_exit(0);
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__)
316 int min, max;
318 min = sched_get_priority_min(JACK_SCHED_POLICY);
319 if (min == -1)
321 jack_error("sched_get_priority_min() failed.");
322 return false;
325 max = sched_get_priority_max(JACK_SCHED_POLICY);
326 if (max == -1)
328 jack_error("sched_get_priority_max() failed.");
329 return false;
332 *min_ptr = min;
333 *max_ptr = max;
335 return true;
336 #else
337 return false;
338 #endif
341 bool jack_tls_allocate_key(jack_tls_key *key_ptr)
343 int ret;
345 ret = pthread_key_create(key_ptr, NULL);
346 if (ret != 0)
348 jack_error("pthread_key_create() failed with error %d", ret);
349 return false;
352 return true;
355 bool jack_tls_free_key(jack_tls_key key)
357 int ret;
359 ret = pthread_key_delete(key);
360 if (ret != 0)
362 jack_error("pthread_key_delete() failed with error %d", ret);
363 return false;
366 return true;
369 bool jack_tls_set(jack_tls_key key, void *data_ptr)
371 int ret;
373 ret = pthread_setspecific(key, (const void *)data_ptr);
374 if (ret != 0)
376 jack_error("pthread_setspecific() failed with error %d", ret);
377 return false;
380 return true;
383 void *jack_tls_get(jack_tls_key key)
385 return pthread_getspecific(key);