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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1221 - (hide annotations) (download)
Wed Jun 6 18:50:03 2007 UTC (17 years 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 schoenebeck 53 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5 schoenebeck 56 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 schoenebeck 1212 * Copyright (C) 2005 - 2007 Christian Schoenebeck *
7 schoenebeck 53 * *
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 schoenebeck 1221 // 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 schoenebeck 1212 namespace LinuxSampler {
33    
34 schoenebeck 392 Thread::Thread(bool LockMemory, bool RealTime, int PriorityMax, int PriorityDelta) {
35     this->bLockedMemory = LockMemory;
36 schoenebeck 53 this->isRealTime = RealTime;
37     this->PriorityDelta = PriorityDelta;
38     this->PriorityMax = PriorityMax;
39     __thread_destructor_key = 0;
40 schoenebeck 1221 pthread_attr_init(&__thread_attr);
41 schoenebeck 53 }
42    
43     Thread::~Thread() {
44 schoenebeck 57 StopThread();
45 schoenebeck 1221 pthread_attr_destroy(&__thread_attr);
46 schoenebeck 53 }
47    
48     /**
49 schoenebeck 57 * 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 schoenebeck 53 */
54     int Thread::StartThread() {
55 schoenebeck 1221 RunningCondition.Lock();
56     if (!RunningCondition.GetUnsafe()) {
57 schoenebeck 57 SignalStartThread();
58 schoenebeck 1221 // wait until thread started execution
59     RunningCondition.WaitIf(false);
60 schoenebeck 57 }
61 schoenebeck 1221 RunningCondition.Unlock();
62 schoenebeck 57 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 schoenebeck 1221 // 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 schoenebeck 53 // Create and run the thread
104 schoenebeck 1221 res = pthread_create(&this->__thread_id, &__thread_attr, __pthread_launcher, this);
105 schoenebeck 53 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 schoenebeck 1221 RunningCondition.Set(false);
112 schoenebeck 53 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 schoenebeck 1221 RunningCondition.Set(false);
117 schoenebeck 53 break;
118     default:
119     std::cerr << "Thread creation failed: Unknown cause."
120     << std::endl << std::flush;
121 schoenebeck 1221 RunningCondition.Set(false);
122 schoenebeck 53 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 schoenebeck 1221 RunningCondition.Lock();
133     if (RunningCondition.GetUnsafe()) {
134 schoenebeck 53 SignalStopThread();
135 schoenebeck 1221 // wait until thread stopped execution
136     RunningCondition.WaitIf(true);
137 schoenebeck 351 pthread_detach(__thread_id);
138 schoenebeck 53 }
139 schoenebeck 1221 RunningCondition.Unlock();
140 schoenebeck 53 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 schoenebeck 57 *
148     * @see StopThread()
149 schoenebeck 53 */
150     int Thread::SignalStopThread() {
151 schoenebeck 1221 //FIXME: segfaults when thread is not yet running
152 schoenebeck 53 pthread_cancel(__thread_id);
153     return 0;
154     }
155    
156     /**
157 schoenebeck 1212 * Returns @c true in case the thread is currently running.
158     */
159     bool Thread::IsRunning() {
160 schoenebeck 1221 return RunningCondition.GetUnsafe();
161 schoenebeck 1212 }
162    
163     /**
164 schoenebeck 53 * 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 schoenebeck 361 #if !defined(__APPLE__)
172 schoenebeck 1221 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 schoenebeck 53 struct sched_param schp;
183     memset(&schp, 0, sizeof(schp));
184     if (this->PriorityMax == 1) {
185 schoenebeck 1221 schp.sched_priority = sched_get_priority_max(policy) + this->PriorityDelta;
186 schoenebeck 53 }
187     if (this->PriorityMax == -1) {
188 schoenebeck 1221 schp.sched_priority = sched_get_priority_min(policy) + this->PriorityDelta;
189 schoenebeck 53 }
190 schoenebeck 1221 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 schoenebeck 53 return -1;
196     }
197 schoenebeck 361 #endif
198 schoenebeck 53 return 0;
199     }
200    
201     /**
202 schoenebeck 392 * Locks the memory so it will not be swapped out by the operating system.
203     */
204     int Thread::LockMemory() {
205 letz 399 #if !defined(__APPLE__)
206 schoenebeck 392 if (!bLockedMemory) return 0;
207     if (mlockall(MCL_CURRENT | MCL_FUTURE) < 0) {
208 schoenebeck 1221 std::cerr << "Thread: WARNING, can't mlockall() memory!\n"
209     << std::flush;
210 schoenebeck 392 return -1;
211     }
212 letz 399 #endif
213 schoenebeck 392 return 0;
214     }
215    
216     /**
217 schoenebeck 53 * 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 schoenebeck 1221 RunningCondition.Lock();
224 schoenebeck 53 pthread_key_create(&__thread_destructor_key, __pthread_destructor);
225     pthread_setspecific(__thread_destructor_key, this);
226 schoenebeck 1221 RunningCondition.Set(true);
227     RunningCondition.Unlock();
228 schoenebeck 53 }
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 schoenebeck 1221 RunningCondition.Set(false);
236 persson 497 return 0;
237 schoenebeck 53 }
238    
239     /// Callback function for the POSIX thread API
240     void* __pthread_launcher(void* thread) {
241 schoenebeck 63 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); // let the thread be killable under any circumstances
242 schoenebeck 53 Thread* t;
243     t = (Thread*) thread;
244 schoenebeck 57 t->SetSchedulingPriority();
245 schoenebeck 392 t->LockMemory();
246 schoenebeck 53 t->EnableDestructor();
247     t->Main();
248 persson 497 return NULL;
249 schoenebeck 53 }
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 schoenebeck 1212
258     } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC