/[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 351 - (hide annotations) (download)
Tue Jan 25 22:11:43 2005 UTC (19 years, 3 months ago) by schoenebeck
File size: 7188 byte(s)
* fixed some memory leaks (patch by Gene Anders)

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 53 * *
7     * This program is free software; you can redistribute it and/or modify *
8     * it under the terms of the GNU General Public License as published by *
9     * the Free Software Foundation; either version 2 of the License, or *
10     * (at your option) any later version. *
11     * *
12     * This program is distributed in the hope that it will be useful, *
13     * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15     * GNU General Public License for more details. *
16     * *
17     * You should have received a copy of the GNU General Public License *
18     * along with this program; if not, write to the Free Software *
19     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
20     * MA 02111-1307 USA *
21     ***************************************************************************/
22    
23     #include "Thread.h"
24    
25     Thread::Thread(bool RealTime, int PriorityMax, int PriorityDelta) {
26     this->isRealTime = RealTime;
27     this->Running = false;
28     this->PriorityDelta = PriorityDelta;
29     this->PriorityMax = PriorityMax;
30     __thread_destructor_key = 0;
31     pthread_mutex_init(&__thread_state_mutex, NULL);
32 schoenebeck 57 pthread_cond_init(&__thread_start_condition, NULL);
33 schoenebeck 53 pthread_cond_init(&__thread_exit_condition, NULL);
34     }
35    
36     Thread::~Thread() {
37 schoenebeck 57 StopThread();
38     pthread_cond_destroy(&__thread_start_condition);
39 schoenebeck 53 pthread_cond_destroy(&__thread_exit_condition);
40     pthread_mutex_destroy(&__thread_state_mutex);
41     }
42    
43     /**
44 schoenebeck 57 * Starts the thread. This method will wait until the thread actually
45     * started it's execution before it will return. The abstract method
46     * Main() is the entry point for the new thread. You have to implement the
47     * Main() method in your subclass.
48 schoenebeck 53 */
49     int Thread::StartThread() {
50 schoenebeck 57 pthread_mutex_lock(&__thread_state_mutex);
51     if (!Running) {
52     SignalStartThread();
53     pthread_cond_wait(&__thread_start_condition, &__thread_state_mutex);
54     }
55     pthread_mutex_unlock(&__thread_state_mutex);
56     return 0;
57     }
58    
59     /**
60     * Starts the thread. This method will signal to start the thread and
61     * return immediately. Note that the thread might not yet run when this
62     * method returns! The abstract method Main() is the entry point for the
63     * new thread. You have to implement the Main() method in your subclass.
64     *
65     * @see StartThread()
66     */
67     int Thread::SignalStartThread() {
68 schoenebeck 53 // Create and run the thread
69     int res = pthread_create(&this->__thread_id, NULL, __pthread_launcher, this);
70     switch (res) {
71     case 0: // Success
72     break;
73     case EAGAIN:
74     std::cerr << "Thread creation failed: System doesn't allow to create another thread."
75     << std::endl << std::flush;
76     this->Running = false;
77     break;
78     case EPERM:
79     std::cerr << "Thread creation failed: You're lacking permisssions to set required scheduling policy and parameters."
80     << std::endl << std::flush;
81     this->Running = false;
82     break;
83     default:
84     std::cerr << "Thread creation failed: Unknown cause."
85     << std::endl << std::flush;
86     this->Running = false;
87     break;
88     }
89     return res;
90     }
91    
92     /**
93     * Stops the thread. This method will wait until the thread actually stopped
94     * it's execution before it will return.
95     */
96     int Thread::StopThread() {
97     pthread_mutex_lock(&__thread_state_mutex);
98     if (Running) {
99     SignalStopThread();
100     pthread_cond_wait(&__thread_exit_condition, &__thread_state_mutex);
101 schoenebeck 351 pthread_detach(__thread_id);
102 schoenebeck 53 }
103     pthread_mutex_unlock(&__thread_state_mutex);
104     return 0;
105     }
106    
107     /**
108     * Stops the thread. This method will signal to stop the thread and return
109     * immediately. Note that the thread might still run when this method
110     * returns!
111 schoenebeck 57 *
112     * @see StopThread()
113 schoenebeck 53 */
114     int Thread::SignalStopThread() {
115     pthread_cancel(__thread_id);
116     return 0;
117     }
118    
119     /**
120     * Sets the process SCHED_FIFO policy, if max=1 then set at max priority,
121     * else use min priority. delta is added to the priority so that we can
122     * for example set 3 SCHED_FIFO tasks to different priorities by specifying
123     * delta 0 , -1 , -2 ( 0 = highest priority because -1 is subtracted to the
124     * current priority).
125     */
126     int Thread::SetSchedulingPriority() {
127     struct sched_param schp;
128    
129 schoenebeck 57 if (!isRealTime) return 0;
130    
131 schoenebeck 53 if (mlockall(MCL_CURRENT | MCL_FUTURE) < 0) {
132     perror("WARNING, can't mlockall() memory!");
133     }
134    
135     /*
136     * set the process to realtime privs
137     */
138     memset(&schp, 0, sizeof(schp));
139     if (this->PriorityMax == 1) {
140     schp.sched_priority = sched_get_priority_max(SCHED_FIFO) + this->PriorityDelta;
141     }
142     if (this->PriorityMax == -1) {
143     schp.sched_priority = sched_get_priority_min(SCHED_FIFO) + this->PriorityDelta;
144     }
145    
146     if (sched_setscheduler(0, SCHED_FIFO, &schp) != 0) {
147     perror("sched_setscheduler");
148     return -1;
149     }
150    
151     return 0;
152     }
153    
154     /**
155     * Registers thread destructor callback function which will be executed when
156     * the thread stops it's execution and sets the 'Running' flag to true. This
157     * method will be called by the __pthread_launcher callback function, DO NOT
158     * CALL THIS METHOD YOURSELF!
159     */
160     void Thread::EnableDestructor() {
161     pthread_mutex_lock(&__thread_state_mutex);
162     pthread_key_create(&__thread_destructor_key, __pthread_destructor);
163     pthread_setspecific(__thread_destructor_key, this);
164     Running = true;
165     pthread_mutex_unlock(&__thread_state_mutex);
166 schoenebeck 57 pthread_cond_broadcast(&__thread_start_condition);
167 schoenebeck 53 }
168    
169     /**
170     * Will be called by the kernel when the thread stops it's execution.
171     */
172     int Thread::Destructor() {
173     pthread_key_delete(__thread_destructor_key);
174     pthread_mutex_lock(&__thread_state_mutex);
175     Running = false;
176     pthread_mutex_unlock(&__thread_state_mutex);
177     pthread_cond_broadcast(&__thread_exit_condition);
178     }
179    
180     /// Callback function for the POSIX thread API
181     void* __pthread_launcher(void* thread) {
182 schoenebeck 63 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); // let the thread be killable under any circumstances
183 schoenebeck 53 Thread* t;
184     t = (Thread*) thread;
185 schoenebeck 57 t->SetSchedulingPriority();
186 schoenebeck 53 t->EnableDestructor();
187     t->Main();
188     }
189    
190     /// Callback function for the POSIX thread API
191     void __pthread_destructor(void* thread) {
192     Thread* t;
193     t = (Thread*) thread;
194     t->Destructor();
195     }

  ViewVC Help
Powered by ViewVC