1 /*
2 Copyright (C) 2001 Paul Davis
3 Copyright (C) 2004-2008 Grame
4 
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.
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
13 GNU Lesser General Public License for more details.
14 
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.
18 
19 */
20 
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
27 
28 //#define JACK_SCHED_POLICY SCHED_RR
29 #define JACK_SCHED_POLICY SCHED_FIFO
30 
31 #if defined(__linux__) && !defined(SCHED_RESET_ON_FORK)
32 # define SCHED_RESET_ON_FORK 0x40000000
33 #endif
34 
35 namespace Jack
36 {
37 
ThreadHandler(void * arg)38 void* JackPosixThread::ThreadHandler(void* arg)
39 {
40     JackPosixThread* obj = (JackPosixThread*)arg;
41     JackRunnableInterface* runnable = obj->fRunnable;
42     int err;
43 
44     if ((err = pthread_setcanceltype(obj->fCancellation, NULL)) != 0) {
45         jack_error("pthread_setcanceltype err = %s", strerror(err));
46     }
47 
48     // Signal creation thread when started with StartSync
49     jack_log("JackPosixThread::ThreadHandler : start");
50     obj->fStatus = kIniting;
51 
52     // Call Init method
53     if (!runnable->Init()) {
54         jack_error("Thread init fails: thread quits");
55         return 0;
56     }
57 
58     obj->fStatus = kRunning;
59 
60     // If Init succeed, start the thread loop
61     bool res = true;
62     while (obj->fStatus == kRunning && res) {
63         res = runnable->Execute();
64     }
65 
66     jack_log("JackPosixThread::ThreadHandler : exit");
67     pthread_exit(0);
68     return 0; // never reached
69 }
70 
Start()71 int JackPosixThread::Start()
72 {
73     fStatus = kStarting;
74 
75     // Check if the thread was correctly started
76     if (StartImp(&fThread, fPriority, fRealTime, ThreadHandler, this) < 0) {
77         fStatus = kIdle;
78         return -1;
79     } else {
80         return 0;
81     }
82 }
83 
StartSync()84 int JackPosixThread::StartSync()
85 {
86     fStatus = kStarting;
87 
88     if (StartImp(&fThread, fPriority, fRealTime, ThreadHandler, this) < 0) {
89         fStatus = kIdle;
90         return -1;
91     } else {
92         int count = 0;
93         while (fStatus == kStarting && ++count < 1000) {
94             JackSleep(1000);
95         }
96         return (count == 1000) ? -1 : 0;
97     }
98 }
99 
StartImp(jack_native_thread_t * thread,int priority,int realtime,void * (* start_routine)(void *),void * arg)100 int JackPosixThread::StartImp(jack_native_thread_t* thread, int priority, int realtime, void*(*start_routine)(void*), void* arg)
101 {
102     pthread_attr_t attributes;
103     struct sched_param rt_param;
104     pthread_attr_init(&attributes);
105     int res;
106 
107     if ((res = pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_JOINABLE))) {
108         jack_error("Cannot request joinable thread creation for thread res = %d", res);
109         return -1;
110     }
111 
112     if ((res = pthread_attr_setscope(&attributes, PTHREAD_SCOPE_SYSTEM))) {
113         jack_error("Cannot set scheduling scope for thread res = %d", res);
114         return -1;
115     }
116 
117     if (realtime) {
118 
119         jack_log("JackPosixThread::StartImp : create RT thread");
120 
121         if ((res = pthread_attr_setinheritsched(&attributes, PTHREAD_EXPLICIT_SCHED))) {
122             jack_error("Cannot request explicit scheduling for RT thread res = %d", res);
123             return -1;
124         }
125 
126         if ((res = pthread_attr_setschedpolicy(&attributes, JACK_SCHED_POLICY))) {
127             jack_error("Cannot set RR scheduling class for RT thread res = %d", res);
128             return -1;
129         }
130 
131         memset(&rt_param, 0, sizeof(rt_param));
132         rt_param.sched_priority = priority;
133 
134         if ((res = pthread_attr_setschedparam(&attributes, &rt_param))) {
135             jack_error("Cannot set scheduling priority for RT thread res = %d", res);
136             return -1;
137         }
138 
139     } else {
140         jack_log("JackPosixThread::StartImp : create non RT thread");
141         if ((res = pthread_attr_setinheritsched(&attributes, PTHREAD_EXPLICIT_SCHED))) {
142             jack_log("Cannot request explicit scheduling for non RT thread res = %d", res);
143         }
144     }
145 
146     if ((res = pthread_attr_setstacksize(&attributes, THREAD_STACK))) {
147         jack_error("Cannot set thread stack size res = %d", res);
148         return -1;
149     }
150 
151     if ((res = JackGlobals::fJackThreadCreator(thread, &attributes, start_routine, arg))) {
152         jack_error("Cannot create thread res = %d", res);
153         return -1;
154     }
155 
156     pthread_attr_destroy(&attributes);
157     return 0;
158 }
159 
Kill()160 int JackPosixThread::Kill()
161 {
162     if (fThread != (jack_native_thread_t)NULL) { // If thread has been started
163         jack_log("JackPosixThread::Kill");
164         void* status;
165         pthread_cancel(fThread);
166         pthread_join(fThread, &status);
167         fStatus = kIdle;
168         fThread = (jack_native_thread_t)NULL;
169         return 0;
170     } else {
171         return -1;
172     }
173 }
174 
Stop()175 int JackPosixThread::Stop()
176 {
177     if (fThread != (jack_native_thread_t)NULL) { // If thread has been started
178         jack_log("JackPosixThread::Stop");
179         void* status;
180         fStatus = kIdle; // Request for the thread to stop
181         pthread_join(fThread, &status);
182         fThread = (jack_native_thread_t)NULL;
183         return 0;
184     } else {
185         return -1;
186     }
187 }
188 
KillImp(jack_native_thread_t thread)189 int JackPosixThread::KillImp(jack_native_thread_t thread)
190 {
191     if (thread != (jack_native_thread_t)NULL) { // If thread has been started
192         jack_log("JackPosixThread::Kill");
193         void* status;
194         pthread_cancel(thread);
195         pthread_join(thread, &status);
196         return 0;
197     } else {
198         return -1;
199     }
200 }
201 
StopImp(jack_native_thread_t thread)202 int JackPosixThread::StopImp(jack_native_thread_t thread)
203 {
204     if (thread != (jack_native_thread_t)NULL) { // If thread has been started
205         jack_log("JackPosixThread::Stop");
206         void* status;
207         pthread_join(thread, &status);
208         return 0;
209     } else {
210         return -1;
211     }
212 }
213 
AcquireRealTime()214 int JackPosixThread::AcquireRealTime()
215 {
216     return (fThread != (jack_native_thread_t)NULL) ? AcquireRealTimeImp(fThread, fPriority) : -1;
217 }
218 
AcquireSelfRealTime()219 int JackPosixThread::AcquireSelfRealTime()
220 {
221     return AcquireRealTimeImp(pthread_self(), fPriority);
222 }
223 
AcquireRealTime(int priority)224 int JackPosixThread::AcquireRealTime(int priority)
225 {
226     fPriority = priority;
227     return AcquireRealTime();
228 }
229 
AcquireSelfRealTime(int priority)230 int JackPosixThread::AcquireSelfRealTime(int priority)
231 {
232     fPriority = priority;
233     return AcquireSelfRealTime();
234 }
AcquireRealTimeImp(jack_native_thread_t thread,int priority)235 int JackPosixThread::AcquireRealTimeImp(jack_native_thread_t thread, int priority)
236 {
237     struct sched_param rtparam;
238     int res;
239     memset(&rtparam, 0, sizeof(rtparam));
240     rtparam.sched_priority = priority;
241 
242     jack_log("JackPosixThread::AcquireRealTimeImp priority = %d", priority);
243 
244     if ((res = pthread_setschedparam(thread, JACK_SCHED_POLICY, &rtparam)) == 0)
245         return 0;
246 
247 #ifdef SCHED_RESET_ON_FORK
248     jack_log("pthread_setschedparam() failed (%d), trying with SCHED_RESET_ON_FORK.", res);
249     if ((res = pthread_setschedparam(thread, JACK_SCHED_POLICY|SCHED_RESET_ON_FORK, &rtparam)) == 0)
250         return 0;
251 #endif
252 
253     jack_error("Cannot use real-time scheduling (RR/%d)"
254                " (%d: %s)", rtparam.sched_priority, res,
255                strerror(res));
256     return -1;
257 }
258 
DropRealTime()259 int JackPosixThread::DropRealTime()
260 {
261     return (fThread != (jack_native_thread_t)NULL) ? DropRealTimeImp(fThread) : -1;
262 }
263 
DropSelfRealTime()264 int JackPosixThread::DropSelfRealTime()
265 {
266     return DropRealTimeImp(pthread_self());
267 }
268 
DropRealTimeImp(jack_native_thread_t thread)269 int JackPosixThread::DropRealTimeImp(jack_native_thread_t thread)
270 {
271     struct sched_param rtparam;
272     int res;
273     memset(&rtparam, 0, sizeof(rtparam));
274     rtparam.sched_priority = 0;
275 
276     if ((res = pthread_setschedparam(thread, SCHED_OTHER, &rtparam)) != 0) {
277         jack_error("Cannot switch to normal scheduling priority(%s)", strerror(errno));
278         return -1;
279     }
280     return 0;
281 }
282 
GetThreadID()283 jack_native_thread_t JackPosixThread::GetThreadID()
284 {
285     return fThread;
286 }
287 
IsThread()288 bool JackPosixThread::IsThread()
289 {
290     return pthread_self() == fThread;
291 }
292 
Terminate()293 void JackPosixThread::Terminate()
294 {
295     jack_log("JackPosixThread::Terminate");
296     pthread_exit(0);
297 }
298 
ThreadExit()299 SERVER_EXPORT void ThreadExit()
300 {
301     jack_log("ThreadExit");
302     pthread_exit(0);
303 }
304 
305 } // end of namespace
306 
jack_get_thread_realtime_priority_range(int * min_ptr,int * max_ptr)307 bool jack_get_thread_realtime_priority_range(int * min_ptr, int * max_ptr)
308 {
309 #if defined(_POSIX_PRIORITY_SCHEDULING) && !defined(__APPLE__)
310     int min, max;
311 
312     min = sched_get_priority_min(JACK_SCHED_POLICY);
313     if (min == -1)
314     {
315         jack_error("sched_get_priority_min() failed.");
316         return false;
317     }
318 
319     max = sched_get_priority_max(JACK_SCHED_POLICY);
320     if (max == -1)
321     {
322         jack_error("sched_get_priority_max() failed.");
323         return false;
324     }
325 
326     *min_ptr = min;
327     *max_ptr = max;
328 
329     return true;
330 #else
331     return false;
332 #endif
333 }
334 
jack_tls_allocate_key(jack_tls_key * key_ptr)335 bool jack_tls_allocate_key(jack_tls_key *key_ptr)
336 {
337     int ret;
338 
339     ret = pthread_key_create(key_ptr, NULL);
340     if (ret != 0)
341     {
342         jack_error("pthread_key_create() failed with error %d", ret);
343         return false;
344     }
345 
346     return true;
347 }
348 
jack_tls_free_key(jack_tls_key key)349 bool jack_tls_free_key(jack_tls_key key)
350 {
351     int ret;
352 
353     ret = pthread_key_delete(key);
354     if (ret != 0)
355     {
356         jack_error("pthread_key_delete() failed with error %d", ret);
357         return false;
358     }
359 
360     return true;
361 }
362 
jack_tls_set(jack_tls_key key,void * data_ptr)363 bool jack_tls_set(jack_tls_key key, void *data_ptr)
364 {
365     int ret;
366 
367     ret = pthread_setspecific(key, (const void *)data_ptr);
368     if (ret != 0)
369     {
370         jack_error("pthread_setspecific() failed with error %d", ret);
371         return false;
372     }
373 
374     return true;
375 }
376 
jack_tls_get(jack_tls_key key)377 void *jack_tls_get(jack_tls_key key)
378 {
379     return pthread_getspecific(key);
380 }
381