/[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 53 - (hide annotations) (download)
Mon Apr 26 17:15:51 2004 UTC (20 years ago) by schoenebeck
File size: 6147 byte(s)
* completely restructured source tree
* implemented multi channel support
* implemented instrument manager, which controls sharing of instruments
  between multiple sampler engines / sampler channels
* created abstract classes 'AudioOutputDevice' and 'MidiInputDevice' for
  convenient implementation of further audio output driver and MIDI input
  driver for LinuxSampler
* implemented following LSCP commands: 'SET CHANNEL MIDI INPUT TYPE',
  'LOAD ENGINE', 'GET CHANNELS', 'ADD CHANNEL', 'REMOVE CHANNEL',
  'SET CHANNEL AUDIO OUTPUT TYPE'
* temporarily removed all command line options
* LSCP server is now launched by default

1 schoenebeck 53 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5     * Copyright (C) 2003 by Benno Senoner and Christian Schoenebeck *
6     * *
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     pthread_cond_init(&__thread_exit_condition, NULL);
33     }
34    
35     Thread::~Thread() {
36     if (this->Running) StopThread();
37     pthread_cond_destroy(&__thread_exit_condition);
38     pthread_mutex_destroy(&__thread_state_mutex);
39     }
40    
41     /**
42     * Start the thread. The Main() method is the entry point for the new
43     * thread. You have to implement the Main() method in your subclass.
44     */
45     int Thread::StartThread() {
46     // Create and run the thread
47     int res = pthread_create(&this->__thread_id, NULL, __pthread_launcher, this);
48     switch (res) {
49     case 0: // Success
50     break;
51     case EAGAIN:
52     std::cerr << "Thread creation failed: System doesn't allow to create another thread."
53     << std::endl << std::flush;
54     this->Running = false;
55     break;
56     case EPERM:
57     std::cerr << "Thread creation failed: You're lacking permisssions to set required scheduling policy and parameters."
58     << std::endl << std::flush;
59     this->Running = false;
60     break;
61     default:
62     std::cerr << "Thread creation failed: Unknown cause."
63     << std::endl << std::flush;
64     this->Running = false;
65     break;
66     }
67     return res;
68     }
69    
70     /**
71     * Stops the thread. This method will wait until the thread actually stopped
72     * it's execution before it will return.
73     */
74     int Thread::StopThread() {
75     pthread_mutex_lock(&__thread_state_mutex);
76     if (Running) {
77     SignalStopThread();
78     pthread_cond_wait(&__thread_exit_condition, &__thread_state_mutex);
79     }
80     pthread_mutex_unlock(&__thread_state_mutex);
81     return 0;
82     }
83    
84     /**
85     * Stops the thread. This method will signal to stop the thread and return
86     * immediately. Note that the thread might still run when this method
87     * returns!
88     */
89     int Thread::SignalStopThread() {
90     pthread_cancel(__thread_id);
91     return 0;
92     }
93    
94     /**
95     * Sets the process SCHED_FIFO policy, if max=1 then set at max priority,
96     * else use min priority. delta is added to the priority so that we can
97     * for example set 3 SCHED_FIFO tasks to different priorities by specifying
98     * delta 0 , -1 , -2 ( 0 = highest priority because -1 is subtracted to the
99     * current priority).
100     */
101     int Thread::SetSchedulingPriority() {
102     struct sched_param schp;
103    
104     if (mlockall(MCL_CURRENT | MCL_FUTURE) < 0) {
105     perror("WARNING, can't mlockall() memory!");
106     }
107    
108     if(!isRealTime) return 0;
109    
110     /*
111     * set the process to realtime privs
112     */
113     memset(&schp, 0, sizeof(schp));
114     if (this->PriorityMax == 1) {
115     schp.sched_priority = sched_get_priority_max(SCHED_FIFO) + this->PriorityDelta;
116     }
117     if (this->PriorityMax == -1) {
118     schp.sched_priority = sched_get_priority_min(SCHED_FIFO) + this->PriorityDelta;
119     }
120    
121     if (sched_setscheduler(0, SCHED_FIFO, &schp) != 0) {
122     perror("sched_setscheduler");
123     return -1;
124     }
125    
126     return 0;
127     }
128    
129     /**
130     * Registers thread destructor callback function which will be executed when
131     * the thread stops it's execution and sets the 'Running' flag to true. This
132     * method will be called by the __pthread_launcher callback function, DO NOT
133     * CALL THIS METHOD YOURSELF!
134     */
135     void Thread::EnableDestructor() {
136     pthread_mutex_lock(&__thread_state_mutex);
137     pthread_key_create(&__thread_destructor_key, __pthread_destructor);
138     pthread_setspecific(__thread_destructor_key, this);
139     Running = true;
140     pthread_mutex_unlock(&__thread_state_mutex);
141     }
142    
143     /**
144     * Will be called by the kernel when the thread stops it's execution.
145     */
146     int Thread::Destructor() {
147     pthread_key_delete(__thread_destructor_key);
148     pthread_mutex_lock(&__thread_state_mutex);
149     Running = false;
150     pthread_mutex_unlock(&__thread_state_mutex);
151     pthread_cond_broadcast(&__thread_exit_condition);
152     }
153    
154     /// Callback function for the POSIX thread API
155     void* __pthread_launcher(void* thread) {
156     Thread* t;
157     t = (Thread*) thread;
158     t->EnableDestructor();
159     t->SetSchedulingPriority();
160     t->Main();
161     }
162    
163     /// Callback function for the POSIX thread API
164     void __pthread_destructor(void* thread) {
165     Thread* t;
166     t = (Thread*) thread;
167     t->Destructor();
168     }

  ViewVC Help
Powered by ViewVC