1 |
/* |
---|
2 |
Modifications for Freebob (C) 2006, Pieter Palmers |
---|
3 |
|
---|
4 |
Copied from the jackd/jackdmp sources |
---|
5 |
function names changed in order to avoid naming problems when using this in |
---|
6 |
a jackd backend. |
---|
7 |
|
---|
8 |
Copyright (C) 2001 Paul Davis |
---|
9 |
Copyright (C) 2004-2006 Grame |
---|
10 |
|
---|
11 |
This program is free software; you can redistribute it and/or modify |
---|
12 |
it under the terms of the GNU General Public License as published by |
---|
13 |
the Free Software Foundation; either version 2 of the License, or |
---|
14 |
(at your option) any later version. |
---|
15 |
|
---|
16 |
This program is distributed in the hope that it will be useful, |
---|
17 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
19 |
GNU General Public License for more details. |
---|
20 |
|
---|
21 |
You should have received a copy of the GNU General Public License |
---|
22 |
along with this program; if not, write to the Free Software |
---|
23 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
24 |
|
---|
25 |
*/ |
---|
26 |
|
---|
27 |
#include "PosixThread.h" |
---|
28 |
#include <string.h> // for memset |
---|
29 |
#include <errno.h> |
---|
30 |
#include <assert.h> |
---|
31 |
|
---|
32 |
namespace FreebobUtil |
---|
33 |
{ |
---|
34 |
|
---|
35 |
IMPL_DEBUG_MODULE( Thread, Thread, DEBUG_LEVEL_VERBOSE ); |
---|
36 |
|
---|
37 |
void* PosixThread::ThreadHandler(void* arg) |
---|
38 |
{ |
---|
39 |
PosixThread* obj = (PosixThread*)arg; |
---|
40 |
RunnableInterface* runnable = obj->fRunnable; |
---|
41 |
int err; |
---|
42 |
|
---|
43 |
if ((err = pthread_setcanceltype(obj->fCancellation, NULL)) != 0) { |
---|
44 |
debugError("pthread_setcanceltype err = %s", strerror(err)); |
---|
45 |
} |
---|
46 |
|
---|
47 |
// Call Init method |
---|
48 |
if (!runnable->Init()) { |
---|
49 |
debugError("Thread init fails: thread quits"); |
---|
50 |
return 0; |
---|
51 |
} |
---|
52 |
|
---|
53 |
debugOutput( DEBUG_LEVEL_VERBOSE, "ThreadHandler: start\n"); |
---|
54 |
|
---|
55 |
// If Init succeed start the thread loop |
---|
56 |
bool res = true; |
---|
57 |
while (obj->fRunning && res) { |
---|
58 |
res = runnable->Execute(); |
---|
59 |
//pthread_testcancel(); |
---|
60 |
} |
---|
61 |
|
---|
62 |
debugOutput( DEBUG_LEVEL_VERBOSE, "ThreadHandler: exit\n"); |
---|
63 |
return 0; |
---|
64 |
} |
---|
65 |
|
---|
66 |
int PosixThread::Start() |
---|
67 |
{ |
---|
68 |
int res; |
---|
69 |
fRunning = true; |
---|
70 |
|
---|
71 |
if (fRealTime) { |
---|
72 |
|
---|
73 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Create RT thread with priority %d\n", fPriority); |
---|
74 |
|
---|
75 |
/* Get the client thread to run as an RT-FIFO |
---|
76 |
scheduled thread of appropriate priority. |
---|
77 |
*/ |
---|
78 |
pthread_attr_t attributes; |
---|
79 |
struct sched_param rt_param; |
---|
80 |
pthread_attr_init(&attributes); |
---|
81 |
|
---|
82 |
if ((res = pthread_attr_setschedpolicy(&attributes, SCHED_FIFO))) { |
---|
83 |
|
---|
84 |
//if ((res = pthread_attr_setschedpolicy(&attributes, SCHED_RR))) { |
---|
85 |
debugError("Cannot set FIFO scheduling class for RT thread %d %s", res, strerror(errno)); |
---|
86 |
return -1; |
---|
87 |
} |
---|
88 |
|
---|
89 |
if ((res = pthread_attr_setscope(&attributes, PTHREAD_SCOPE_SYSTEM))) { |
---|
90 |
debugError("Cannot set scheduling scope for RT thread %d %s", res, strerror(errno)); |
---|
91 |
return -1; |
---|
92 |
} |
---|
93 |
|
---|
94 |
memset(&rt_param, 0, sizeof(rt_param)); |
---|
95 |
rt_param.sched_priority = fPriority; |
---|
96 |
|
---|
97 |
if ((res = pthread_attr_setschedparam(&attributes, &rt_param))) { |
---|
98 |
debugError("Cannot set scheduling priority for RT thread %d %s", res, strerror(errno)); |
---|
99 |
return -1; |
---|
100 |
} |
---|
101 |
|
---|
102 |
if ((res = pthread_create(&fThread, &attributes, ThreadHandler, this))) { |
---|
103 |
debugError("Cannot set create thread %d %s", res, strerror(errno)); |
---|
104 |
return -1; |
---|
105 |
} |
---|
106 |
|
---|
107 |
return 0; |
---|
108 |
} else { |
---|
109 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Create non RT thread\n"); |
---|
110 |
|
---|
111 |
if ((res = pthread_create(&fThread, 0, ThreadHandler, this))) { |
---|
112 |
debugError("Cannot set create thread %d %s", res, strerror(errno)); |
---|
113 |
return -1; |
---|
114 |
} |
---|
115 |
|
---|
116 |
return 0; |
---|
117 |
} |
---|
118 |
} |
---|
119 |
|
---|
120 |
int PosixThread::Kill() |
---|
121 |
{ |
---|
122 |
if (fThread) { // If thread has been started |
---|
123 |
debugOutput( DEBUG_LEVEL_VERBOSE, "PosixThread::Kill\n"); |
---|
124 |
void* status; |
---|
125 |
pthread_cancel(fThread); |
---|
126 |
pthread_join(fThread, &status); |
---|
127 |
return 0; |
---|
128 |
} else { |
---|
129 |
return -1; |
---|
130 |
} |
---|
131 |
} |
---|
132 |
|
---|
133 |
int PosixThread::Stop() |
---|
134 |
{ |
---|
135 |
if (fThread) { // If thread has been started |
---|
136 |
debugOutput( DEBUG_LEVEL_VERBOSE, "PosixThread::Stop\n"); |
---|
137 |
void* status; |
---|
138 |
fRunning = false; // Request for the thread to stop |
---|
139 |
pthread_join(fThread, &status); |
---|
140 |
return 0; |
---|
141 |
} else { |
---|
142 |
return -1; |
---|
143 |
} |
---|
144 |
} |
---|
145 |
|
---|
146 |
int PosixThread::AcquireRealTime() |
---|
147 |
{ |
---|
148 |
struct sched_param rtparam; |
---|
149 |
int res; |
---|
150 |
|
---|
151 |
if (!fThread) |
---|
152 |
return -1; |
---|
153 |
|
---|
154 |
memset(&rtparam, 0, sizeof(rtparam)); |
---|
155 |
rtparam.sched_priority = fPriority; |
---|
156 |
|
---|
157 |
//if ((res = pthread_setschedparam(fThread, SCHED_FIFO, &rtparam)) != 0) { |
---|
158 |
|
---|
159 |
if ((res = pthread_setschedparam(fThread, SCHED_RR, &rtparam)) != 0) { |
---|
160 |
debugError("Cannot use real-time scheduling (FIFO/%d) " |
---|
161 |
"(%d: %s)", rtparam.sched_priority, res, |
---|
162 |
strerror(res)); |
---|
163 |
return -1; |
---|
164 |
} |
---|
165 |
return 0; |
---|
166 |
} |
---|
167 |
|
---|
168 |
int PosixThread::AcquireRealTime(int priority) |
---|
169 |
{ |
---|
170 |
fPriority = priority; |
---|
171 |
return AcquireRealTime(); |
---|
172 |
} |
---|
173 |
|
---|
174 |
int PosixThread::DropRealTime() |
---|
175 |
{ |
---|
176 |
struct sched_param rtparam; |
---|
177 |
int res; |
---|
178 |
|
---|
179 |
if (!fThread) |
---|
180 |
return -1; |
---|
181 |
|
---|
182 |
memset(&rtparam, 0, sizeof(rtparam)); |
---|
183 |
rtparam.sched_priority = 0; |
---|
184 |
|
---|
185 |
if ((res = pthread_setschedparam(fThread, SCHED_OTHER, &rtparam)) != 0) { |
---|
186 |
debugError("Cannot switch to normal scheduling priority(%s)\n", strerror(errno)); |
---|
187 |
return -1; |
---|
188 |
} |
---|
189 |
return 0; |
---|
190 |
} |
---|
191 |
|
---|
192 |
pthread_t PosixThread::GetThreadID() |
---|
193 |
{ |
---|
194 |
return fThread; |
---|
195 |
} |
---|
196 |
|
---|
197 |
} // end of namespace |
---|
198 |
|
---|