Merge branch 'netone-isolateion'
[jack2.git] / posix / JackPosixThread.cpp
blobd61eb932ba7eaefc582a5398bce61c5b2fcef30c
1 /*
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"
23 #include "JackTime.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
31 namespace Jack
34 void* JackPosixThread::ThreadHandler(void* arg)
36 JackPosixThread* obj = (JackPosixThread*)arg;
37 JackRunnableInterface* runnable = obj->fRunnable;
38 int err;
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("ThreadHandler: start");
46 obj->fStatus = kIniting;
48 // Call Init method
49 if (!runnable->Init()) {
50 jack_error("Thread init fails: thread quits");
51 return 0;
54 obj->fStatus = kRunning;
56 // If Init succeed, start the thread loop
57 bool res = true;
58 while (obj->fStatus == kRunning && res) {
59 res = runnable->Execute();
62 jack_log("ThreadHandler: exit");
63 pthread_exit(0);
64 return 0; // never reached
67 int JackPosixThread::Start()
69 fStatus = kStarting;
71 // Check if the thread was correctly started
72 if (StartImp(&fThread, fPriority, fRealTime, ThreadHandler, this) < 0) {
73 fStatus = kIdle;
74 return -1;
75 } else {
76 return 0;
80 int JackPosixThread::StartSync()
82 fStatus = kStarting;
84 if (StartImp(&fThread, fPriority, fRealTime, ThreadHandler, this) < 0) {
85 fStatus = kIdle;
86 return -1;
87 } else {
88 int count = 0;
89 while (fStatus == kStarting && ++count < 1000) {
90 JackSleep(1000);
92 return (count == 1000) ? -1 : 0;
96 int JackPosixThread::StartImp(pthread_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);
101 int res;
103 if ((res = pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_JOINABLE))) {
104 jack_error("Cannot request joinable thread creation for RT thread res = %d err = %s", res, strerror(errno));
105 return -1;
108 if ((res = pthread_attr_setscope(&attributes, PTHREAD_SCOPE_SYSTEM))) {
109 jack_error("Cannot set scheduling scope for RT thread res = %d err = %s", res, strerror(errno));
110 return -1;
113 if (realtime) {
115 jack_log("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 err = %s", res, strerror(errno));
119 return -1;
122 if ((res = pthread_attr_setschedpolicy(&attributes, JACK_SCHED_POLICY))) {
123 jack_error("Cannot set RR scheduling class for RT thread res = %d err = %s", res, strerror(errno));
124 return -1;
126 } else {
127 jack_log("Create non RT thread");
130 memset(&rt_param, 0, sizeof(rt_param));
131 rt_param.sched_priority = priority;
133 if ((res = pthread_attr_setschedparam(&attributes, &rt_param))) {
134 jack_error("Cannot set scheduling priority for RT thread res = %d err = %s", res, strerror(errno));
135 return -1;
138 if ((res = pthread_attr_setstacksize(&attributes, THREAD_STACK))) {
139 jack_error("Cannot set thread stack size res = %d err = %s", res, strerror(errno));
140 return -1;
143 if ((res = JackGlobals::fJackThreadCreator(thread, &attributes, start_routine, arg))) {
144 jack_error("Cannot create thread res = %d err = %s", res, strerror(errno));
145 return -1;
148 pthread_attr_destroy(&attributes);
149 return 0;
152 int JackPosixThread::Kill()
154 if (fThread != (pthread_t)NULL) { // If thread has been started
155 jack_log("JackPosixThread::Kill");
156 void* status;
157 pthread_cancel(fThread);
158 pthread_join(fThread, &status);
159 fStatus = kIdle;
160 fThread = (pthread_t)NULL;
161 return 0;
162 } else {
163 return -1;
167 int JackPosixThread::Stop()
169 if (fThread != (pthread_t)NULL) { // If thread has been started
170 jack_log("JackPosixThread::Stop");
171 void* status;
172 fStatus = kIdle; // Request for the thread to stop
173 pthread_join(fThread, &status);
174 fThread = (pthread_t)NULL;
175 return 0;
176 } else {
177 return -1;
181 int JackPosixThread::KillImp(pthread_t thread)
183 if (thread != (pthread_t)NULL) { // If thread has been started
184 jack_log("JackPosixThread::Kill");
185 void* status;
186 pthread_cancel(thread);
187 pthread_join(thread, &status);
188 return 0;
189 } else {
190 return -1;
194 int JackPosixThread::StopImp(pthread_t thread)
196 if (thread != (pthread_t)NULL) { // If thread has been started
197 jack_log("JackPosixThread::Stop");
198 void* status;
199 pthread_join(thread, &status);
200 return 0;
201 } else {
202 return -1;
206 int JackPosixThread::AcquireRealTime()
208 return (fThread != (pthread_t)NULL) ? AcquireRealTimeImp(fThread, fPriority) : -1;
211 int JackPosixThread::AcquireRealTime(int priority)
213 fPriority = priority;
214 return AcquireRealTime();
217 int JackPosixThread::AcquireRealTimeImp(pthread_t thread, int priority)
219 struct sched_param rtparam;
220 int res;
221 memset(&rtparam, 0, sizeof(rtparam));
222 rtparam.sched_priority = priority;
224 if ((res = pthread_setschedparam(thread, JACK_SCHED_POLICY, &rtparam)) != 0) {
225 jack_error("Cannot use real-time scheduling (RR/%d)"
226 "(%d: %s)", rtparam.sched_priority, res,
227 strerror(res));
228 return -1;
230 return 0;
233 int JackPosixThread::DropRealTime()
235 return (fThread != (pthread_t)NULL) ? DropRealTimeImp(fThread) : -1;
238 int JackPosixThread::DropRealTimeImp(pthread_t thread)
240 struct sched_param rtparam;
241 int res;
242 memset(&rtparam, 0, sizeof(rtparam));
243 rtparam.sched_priority = 0;
245 if ((res = pthread_setschedparam(thread, SCHED_OTHER, &rtparam)) != 0) {
246 jack_error("Cannot switch to normal scheduling priority(%s)", strerror(errno));
247 return -1;
249 return 0;
252 pthread_t JackPosixThread::GetThreadID()
254 return fThread;
257 void JackPosixThread::Terminate()
259 jack_log("JackPosixThread::Terminate");
260 pthread_exit(0);
263 SERVER_EXPORT void ThreadExit()
265 jack_log("ThreadExit");
266 pthread_exit(0);
269 } // end of namespace
271 bool jack_get_thread_realtime_priority_range(int * min_ptr, int * max_ptr)
273 #if defined(_POSIX_PRIORITY_SCHEDULING) && !defined(__APPLE__)
274 int min, max;
276 min = sched_get_priority_min(JACK_SCHED_POLICY);
277 if (min == -1)
279 jack_error("sched_get_priority_min() failed.");
280 return false;
283 max = sched_get_priority_max(JACK_SCHED_POLICY);
284 if (max == -1)
286 jack_error("sched_get_priority_max() failed.");
287 return false;
290 *min_ptr = min;
291 *max_ptr = max;
293 return true;
294 #else
295 return false;
296 #endif
299 bool jack_tls_allocate_key(jack_tls_key *key_ptr)
301 int ret;
303 ret = pthread_key_create(key_ptr, NULL);
304 if (ret != 0)
306 jack_error("pthread_key_create() failed with error %d errno %s", ret, strerror(errno));
307 return false;
310 return true;
313 bool jack_tls_free_key(jack_tls_key key)
315 int ret;
317 ret = pthread_key_delete(key);
318 if (ret != 0)
320 jack_error("pthread_key_delete() failed with error %d errno %s", ret, strerror(errno));
321 return false;
324 return true;
327 bool jack_tls_set(jack_tls_key key, void *data_ptr)
329 int ret;
331 ret = pthread_setspecific(key, (const void *)data_ptr);
332 if (ret != 0)
334 jack_error("pthread_setspecific() failed with error %d errno %s", ret, strerror(errno));
335 return false;
338 return true;
341 void *jack_tls_get(jack_tls_key key)
343 return pthread_getspecific(key);