1 |
/*************************************************************************** |
2 |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005 - 2007 Christian Schoenebeck * |
7 |
* * |
8 |
* This program is free software; you can redistribute it and/or modify * |
9 |
* it under the terms of the GNU General Public License as published by * |
10 |
* the Free Software Foundation; either version 2 of the License, or * |
11 |
* (at your option) any later version. * |
12 |
* * |
13 |
* This program is distributed in the hope that it will be useful, * |
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
16 |
* GNU General Public License for more details. * |
17 |
* * |
18 |
* You should have received a copy of the GNU General Public License * |
19 |
* along with this program; if not, write to the Free Software * |
20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
21 |
* MA 02111-1307 USA * |
22 |
***************************************************************************/ |
23 |
|
24 |
#include "Thread.h" |
25 |
|
26 |
// this is the minimum stack size a thread will be spawned with |
27 |
// if this value is too small, the OS will allocate memory on demand and |
28 |
// thus might lead to dropouts in realtime threads |
29 |
// TODO: should be up for testing to get a reasonable good value |
30 |
#define MIN_STACK_SIZE 524288 |
31 |
|
32 |
namespace LinuxSampler { |
33 |
|
34 |
Thread::Thread(bool LockMemory, bool RealTime, int PriorityMax, int PriorityDelta) { |
35 |
this->bLockedMemory = LockMemory; |
36 |
this->isRealTime = RealTime; |
37 |
this->PriorityDelta = PriorityDelta; |
38 |
this->PriorityMax = PriorityMax; |
39 |
__thread_destructor_key = 0; |
40 |
pthread_attr_init(&__thread_attr); |
41 |
} |
42 |
|
43 |
Thread::~Thread() { |
44 |
StopThread(); |
45 |
pthread_attr_destroy(&__thread_attr); |
46 |
} |
47 |
|
48 |
/** |
49 |
* Starts the thread. This method will wait until the thread actually |
50 |
* started it's execution before it will return. The abstract method |
51 |
* Main() is the entry point for the new thread. You have to implement the |
52 |
* Main() method in your subclass. |
53 |
*/ |
54 |
int Thread::StartThread() { |
55 |
RunningCondition.Lock(); |
56 |
if (!RunningCondition.GetUnsafe()) { |
57 |
SignalStartThread(); |
58 |
// wait until thread started execution |
59 |
RunningCondition.WaitIf(false); |
60 |
} |
61 |
RunningCondition.Unlock(); |
62 |
return 0; |
63 |
} |
64 |
|
65 |
/** |
66 |
* Starts the thread. This method will signal to start the thread and |
67 |
* return immediately. Note that the thread might not yet run when this |
68 |
* method returns! The abstract method Main() is the entry point for the |
69 |
* new thread. You have to implement the Main() method in your subclass. |
70 |
* |
71 |
* @see StartThread() |
72 |
*/ |
73 |
int Thread::SignalStartThread() { |
74 |
// prepare the thread properties |
75 |
int res = pthread_attr_setinheritsched(&__thread_attr, PTHREAD_EXPLICIT_SCHED); |
76 |
if (res) { |
77 |
std::cerr << "Thread creation failed: Could not inherit thread properties." |
78 |
<< std::endl << std::flush; |
79 |
RunningCondition.Set(false); |
80 |
return res; |
81 |
} |
82 |
res = pthread_attr_setdetachstate(&__thread_attr, PTHREAD_CREATE_JOINABLE); |
83 |
if (res) { |
84 |
std::cerr << "Thread creation failed: Could not request a joinable thread." |
85 |
<< std::endl << std::flush; |
86 |
RunningCondition.Set(false); |
87 |
return res; |
88 |
} |
89 |
res = pthread_attr_setscope(&__thread_attr, PTHREAD_SCOPE_SYSTEM); |
90 |
if (res) { |
91 |
std::cerr << "Thread creation failed: Could not request system scope for thread scheduling." |
92 |
<< std::endl << std::flush; |
93 |
RunningCondition.Set(false); |
94 |
return res; |
95 |
} |
96 |
res = pthread_attr_setstacksize(&__thread_attr, MIN_STACK_SIZE); |
97 |
if (res) { |
98 |
std::cerr << "Thread creation failed: Could not set minimum stack size." |
99 |
<< std::endl << std::flush; |
100 |
RunningCondition.Set(false); |
101 |
return res; |
102 |
} |
103 |
// Create and run the thread |
104 |
res = pthread_create(&this->__thread_id, &__thread_attr, __pthread_launcher, this); |
105 |
switch (res) { |
106 |
case 0: // Success |
107 |
break; |
108 |
case EAGAIN: |
109 |
std::cerr << "Thread creation failed: System doesn't allow to create another thread." |
110 |
<< std::endl << std::flush; |
111 |
RunningCondition.Set(false); |
112 |
break; |
113 |
case EPERM: |
114 |
std::cerr << "Thread creation failed: You're lacking permisssions to set required scheduling policy and parameters." |
115 |
<< std::endl << std::flush; |
116 |
RunningCondition.Set(false); |
117 |
break; |
118 |
default: |
119 |
std::cerr << "Thread creation failed: Unknown cause." |
120 |
<< std::endl << std::flush; |
121 |
RunningCondition.Set(false); |
122 |
break; |
123 |
} |
124 |
return res; |
125 |
} |
126 |
|
127 |
/** |
128 |
* Stops the thread. This method will wait until the thread actually stopped |
129 |
* it's execution before it will return. |
130 |
*/ |
131 |
int Thread::StopThread() { |
132 |
RunningCondition.Lock(); |
133 |
if (RunningCondition.GetUnsafe()) { |
134 |
SignalStopThread(); |
135 |
// wait until thread stopped execution |
136 |
RunningCondition.WaitIf(true); |
137 |
pthread_detach(__thread_id); |
138 |
} |
139 |
RunningCondition.Unlock(); |
140 |
return 0; |
141 |
} |
142 |
|
143 |
/** |
144 |
* Stops the thread. This method will signal to stop the thread and return |
145 |
* immediately. Note that the thread might still run when this method |
146 |
* returns! |
147 |
* |
148 |
* @see StopThread() |
149 |
*/ |
150 |
int Thread::SignalStopThread() { |
151 |
//FIXME: segfaults when thread is not yet running |
152 |
pthread_cancel(__thread_id); |
153 |
return 0; |
154 |
} |
155 |
|
156 |
/** |
157 |
* Returns @c true in case the thread is currently running. |
158 |
*/ |
159 |
bool Thread::IsRunning() { |
160 |
return RunningCondition.GetUnsafe(); |
161 |
} |
162 |
|
163 |
/** |
164 |
* Sets the process SCHED_FIFO policy, if max=1 then set at max priority, |
165 |
* else use min priority. delta is added to the priority so that we can |
166 |
* for example set 3 SCHED_FIFO tasks to different priorities by specifying |
167 |
* delta 0 , -1 , -2 ( 0 = highest priority because -1 is subtracted to the |
168 |
* current priority). |
169 |
*/ |
170 |
int Thread::SetSchedulingPriority() { |
171 |
#if !defined(__APPLE__) |
172 |
int policy; |
173 |
const char* policyDescription = NULL; |
174 |
if (isRealTime) { // becomes a RT thread |
175 |
policy = SCHED_FIFO; |
176 |
policyDescription = "realtime"; |
177 |
} else { // 'normal', non-RT thread |
178 |
policy = SCHED_OTHER; |
179 |
policyDescription = "normal (non-RT)"; |
180 |
} |
181 |
// set selected scheduling policy and priority |
182 |
struct sched_param schp; |
183 |
memset(&schp, 0, sizeof(schp)); |
184 |
if (isRealTime) { // it is not possible to change priority for the SCHED_OTHER policy |
185 |
if (this->PriorityMax == 1) { |
186 |
schp.sched_priority = sched_get_priority_max(policy) + this->PriorityDelta; |
187 |
} |
188 |
if (this->PriorityMax == -1) { |
189 |
schp.sched_priority = sched_get_priority_min(policy) + this->PriorityDelta; |
190 |
} |
191 |
} |
192 |
if (pthread_setschedparam(__thread_id, policy, &schp) != 0) { |
193 |
std::cerr << "Thread: WARNING, can't assign " |
194 |
<< policyDescription |
195 |
<< " scheduling to thread!" |
196 |
<< std::endl << std::flush; |
197 |
return -1; |
198 |
} |
199 |
#endif |
200 |
return 0; |
201 |
} |
202 |
|
203 |
/** |
204 |
* Locks the memory so it will not be swapped out by the operating system. |
205 |
*/ |
206 |
int Thread::LockMemory() { |
207 |
#if !defined(__APPLE__) |
208 |
if (!bLockedMemory) return 0; |
209 |
if (mlockall(MCL_CURRENT | MCL_FUTURE) < 0) { |
210 |
std::cerr << "Thread: WARNING, can't mlockall() memory!\n" |
211 |
<< std::flush; |
212 |
return -1; |
213 |
} |
214 |
#endif |
215 |
return 0; |
216 |
} |
217 |
|
218 |
/** |
219 |
* Registers thread destructor callback function which will be executed when |
220 |
* the thread stops it's execution and sets the 'Running' flag to true. This |
221 |
* method will be called by the __pthread_launcher callback function, DO NOT |
222 |
* CALL THIS METHOD YOURSELF! |
223 |
*/ |
224 |
void Thread::EnableDestructor() { |
225 |
RunningCondition.Lock(); |
226 |
pthread_key_create(&__thread_destructor_key, __pthread_destructor); |
227 |
pthread_setspecific(__thread_destructor_key, this); |
228 |
RunningCondition.Set(true); |
229 |
RunningCondition.Unlock(); |
230 |
} |
231 |
|
232 |
/** |
233 |
* Will be called by the kernel when the thread stops it's execution. |
234 |
*/ |
235 |
int Thread::Destructor() { |
236 |
pthread_key_delete(__thread_destructor_key); |
237 |
RunningCondition.Set(false); |
238 |
return 0; |
239 |
} |
240 |
|
241 |
/// Callback function for the POSIX thread API |
242 |
void* __pthread_launcher(void* thread) { |
243 |
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); // let the thread be killable under any circumstances |
244 |
Thread* t; |
245 |
t = (Thread*) thread; |
246 |
t->SetSchedulingPriority(); |
247 |
t->LockMemory(); |
248 |
t->EnableDestructor(); |
249 |
t->Main(); |
250 |
return NULL; |
251 |
} |
252 |
|
253 |
/// Callback function for the POSIX thread API |
254 |
void __pthread_destructor(void* thread) { |
255 |
Thread* t; |
256 |
t = (Thread*) thread; |
257 |
t->Destructor(); |
258 |
} |
259 |
|
260 |
} // namespace LinuxSampler |