3 |
* LinuxSampler - modular, streaming capable sampler * |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
* 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 * |
* 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 * |
* it under the terms of the GNU General Public License as published by * |
23 |
|
|
24 |
#include "Thread.h" |
#include "Thread.h" |
25 |
|
|
26 |
Thread::Thread(bool RealTime, int PriorityMax, int PriorityDelta) { |
namespace LinuxSampler { |
27 |
|
|
28 |
|
Thread::Thread(bool LockMemory, bool RealTime, int PriorityMax, int PriorityDelta) { |
29 |
|
this->bLockedMemory = LockMemory; |
30 |
this->isRealTime = RealTime; |
this->isRealTime = RealTime; |
31 |
this->Running = false; |
this->Running = false; |
32 |
this->PriorityDelta = PriorityDelta; |
this->PriorityDelta = PriorityDelta; |
102 |
if (Running) { |
if (Running) { |
103 |
SignalStopThread(); |
SignalStopThread(); |
104 |
pthread_cond_wait(&__thread_exit_condition, &__thread_state_mutex); |
pthread_cond_wait(&__thread_exit_condition, &__thread_state_mutex); |
105 |
|
pthread_detach(__thread_id); |
106 |
} |
} |
107 |
pthread_mutex_unlock(&__thread_state_mutex); |
pthread_mutex_unlock(&__thread_state_mutex); |
108 |
return 0; |
return 0; |
121 |
} |
} |
122 |
|
|
123 |
/** |
/** |
124 |
|
* Returns @c true in case the thread is currently running. |
125 |
|
*/ |
126 |
|
bool Thread::IsRunning() { |
127 |
|
return Running; |
128 |
|
} |
129 |
|
|
130 |
|
/** |
131 |
* Sets the process SCHED_FIFO policy, if max=1 then set at max priority, |
* Sets the process SCHED_FIFO policy, if max=1 then set at max priority, |
132 |
* else use min priority. delta is added to the priority so that we can |
* else use min priority. delta is added to the priority so that we can |
133 |
* for example set 3 SCHED_FIFO tasks to different priorities by specifying |
* for example set 3 SCHED_FIFO tasks to different priorities by specifying |
135 |
* current priority). |
* current priority). |
136 |
*/ |
*/ |
137 |
int Thread::SetSchedulingPriority() { |
int Thread::SetSchedulingPriority() { |
138 |
|
#if !defined(__APPLE__) |
139 |
struct sched_param schp; |
struct sched_param schp; |
140 |
|
|
141 |
if (!isRealTime) return 0; |
if (!isRealTime) return 0; |
142 |
|
|
|
if (mlockall(MCL_CURRENT | MCL_FUTURE) < 0) { |
|
|
perror("WARNING, can't mlockall() memory!"); |
|
|
} |
|
|
|
|
143 |
/* |
/* |
144 |
* set the process to realtime privs |
* set the process to realtime privs |
145 |
*/ |
*/ |
152 |
} |
} |
153 |
|
|
154 |
if (sched_setscheduler(0, SCHED_FIFO, &schp) != 0) { |
if (sched_setscheduler(0, SCHED_FIFO, &schp) != 0) { |
155 |
perror("sched_setscheduler"); |
perror("Thread: WARNING, can't assign realtime scheduling to thread!"); |
156 |
return -1; |
return -1; |
157 |
} |
} |
158 |
|
#endif |
159 |
|
return 0; |
160 |
|
} |
161 |
|
|
162 |
|
/** |
163 |
|
* Locks the memory so it will not be swapped out by the operating system. |
164 |
|
*/ |
165 |
|
int Thread::LockMemory() { |
166 |
|
#if !defined(__APPLE__) |
167 |
|
if (!bLockedMemory) return 0; |
168 |
|
if (mlockall(MCL_CURRENT | MCL_FUTURE) < 0) { |
169 |
|
perror("Thread: WARNING, can't mlockall() memory!"); |
170 |
|
return -1; |
171 |
|
} |
172 |
|
#endif |
173 |
return 0; |
return 0; |
174 |
} |
} |
175 |
|
|
197 |
Running = false; |
Running = false; |
198 |
pthread_mutex_unlock(&__thread_state_mutex); |
pthread_mutex_unlock(&__thread_state_mutex); |
199 |
pthread_cond_broadcast(&__thread_exit_condition); |
pthread_cond_broadcast(&__thread_exit_condition); |
200 |
|
return 0; |
201 |
} |
} |
202 |
|
|
203 |
/// Callback function for the POSIX thread API |
/// Callback function for the POSIX thread API |
206 |
Thread* t; |
Thread* t; |
207 |
t = (Thread*) thread; |
t = (Thread*) thread; |
208 |
t->SetSchedulingPriority(); |
t->SetSchedulingPriority(); |
209 |
|
t->LockMemory(); |
210 |
t->EnableDestructor(); |
t->EnableDestructor(); |
211 |
t->Main(); |
t->Main(); |
212 |
|
return NULL; |
213 |
} |
} |
214 |
|
|
215 |
/// Callback function for the POSIX thread API |
/// Callback function for the POSIX thread API |
218 |
t = (Thread*) thread; |
t = (Thread*) thread; |
219 |
t->Destructor(); |
t->Destructor(); |
220 |
} |
} |
221 |
|
|
222 |
|
} // namespace LinuxSampler |