/[svn]/linuxsampler/trunk/src/common/Thread.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/common/Thread.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1221 - (show annotations) (download)
Wed Jun 6 18:50:03 2007 UTC (16 years, 10 months ago) by schoenebeck
File size: 9303 byte(s)
* fixed several issues in fundamental "Thread" class: set scheduling
  policy and priority on thread level, set a minimum stack size for
  thread (TODO: a reasonable value yet to be tested), bugfix: non-RT
  threads simply inherited properties of starting thread instead of
  setting their own policy and priority
* updated and fixed test cases (haven't been touched in a while, but
  are now all running successfully through all cases)

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 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 (this->PriorityMax == 1) {
185 schp.sched_priority = sched_get_priority_max(policy) + this->PriorityDelta;
186 }
187 if (this->PriorityMax == -1) {
188 schp.sched_priority = sched_get_priority_min(policy) + this->PriorityDelta;
189 }
190 if (pthread_setschedparam(__thread_id, policy, &schp) != 0) {
191 std::cerr << "Thread: WARNING, can't assign "
192 << policyDescription
193 << " scheduling to thread!"
194 << std::endl << std::flush;
195 return -1;
196 }
197 #endif
198 return 0;
199 }
200
201 /**
202 * Locks the memory so it will not be swapped out by the operating system.
203 */
204 int Thread::LockMemory() {
205 #if !defined(__APPLE__)
206 if (!bLockedMemory) return 0;
207 if (mlockall(MCL_CURRENT | MCL_FUTURE) < 0) {
208 std::cerr << "Thread: WARNING, can't mlockall() memory!\n"
209 << std::flush;
210 return -1;
211 }
212 #endif
213 return 0;
214 }
215
216 /**
217 * Registers thread destructor callback function which will be executed when
218 * the thread stops it's execution and sets the 'Running' flag to true. This
219 * method will be called by the __pthread_launcher callback function, DO NOT
220 * CALL THIS METHOD YOURSELF!
221 */
222 void Thread::EnableDestructor() {
223 RunningCondition.Lock();
224 pthread_key_create(&__thread_destructor_key, __pthread_destructor);
225 pthread_setspecific(__thread_destructor_key, this);
226 RunningCondition.Set(true);
227 RunningCondition.Unlock();
228 }
229
230 /**
231 * Will be called by the kernel when the thread stops it's execution.
232 */
233 int Thread::Destructor() {
234 pthread_key_delete(__thread_destructor_key);
235 RunningCondition.Set(false);
236 return 0;
237 }
238
239 /// Callback function for the POSIX thread API
240 void* __pthread_launcher(void* thread) {
241 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); // let the thread be killable under any circumstances
242 Thread* t;
243 t = (Thread*) thread;
244 t->SetSchedulingPriority();
245 t->LockMemory();
246 t->EnableDestructor();
247 t->Main();
248 return NULL;
249 }
250
251 /// Callback function for the POSIX thread API
252 void __pthread_destructor(void* thread) {
253 Thread* t;
254 t = (Thread*) thread;
255 t->Destructor();
256 }
257
258 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC