2 |
* * |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003 by Benno Senoner and Christian Schoenebeck * |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* * |
* * |
7 |
* This program is free software; you can redistribute it and/or modify * |
* 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 * |
* it under the terms of the GNU General Public License as published by * |
22 |
|
|
23 |
#include "Thread.h" |
#include "Thread.h" |
24 |
|
|
25 |
Thread::Thread(bool RealTime, int PriorityMax, int PriorityDelta) { |
Thread::Thread(bool LockMemory, bool RealTime, int PriorityMax, int PriorityDelta) { |
26 |
|
this->bLockedMemory = LockMemory; |
27 |
this->isRealTime = RealTime; |
this->isRealTime = RealTime; |
28 |
this->Running = false; |
this->Running = false; |
29 |
this->PriorityDelta = PriorityDelta; |
this->PriorityDelta = PriorityDelta; |
30 |
this->PriorityMax = PriorityMax; |
this->PriorityMax = PriorityMax; |
31 |
__thread_destructor_key = 0; |
__thread_destructor_key = 0; |
32 |
pthread_mutex_init(&__thread_state_mutex, NULL); |
pthread_mutex_init(&__thread_state_mutex, NULL); |
33 |
|
pthread_cond_init(&__thread_start_condition, NULL); |
34 |
pthread_cond_init(&__thread_exit_condition, NULL); |
pthread_cond_init(&__thread_exit_condition, NULL); |
35 |
} |
} |
36 |
|
|
37 |
Thread::~Thread() { |
Thread::~Thread() { |
38 |
if (this->Running) StopThread(); |
StopThread(); |
39 |
|
pthread_cond_destroy(&__thread_start_condition); |
40 |
pthread_cond_destroy(&__thread_exit_condition); |
pthread_cond_destroy(&__thread_exit_condition); |
41 |
pthread_mutex_destroy(&__thread_state_mutex); |
pthread_mutex_destroy(&__thread_state_mutex); |
42 |
} |
} |
43 |
|
|
44 |
/** |
/** |
45 |
* Start the thread. The Main() method is the entry point for the new |
* Starts the thread. This method will wait until the thread actually |
46 |
* thread. You have to implement the Main() method in your subclass. |
* started it's execution before it will return. The abstract method |
47 |
|
* Main() is the entry point for the new thread. You have to implement the |
48 |
|
* Main() method in your subclass. |
49 |
*/ |
*/ |
50 |
int Thread::StartThread() { |
int Thread::StartThread() { |
51 |
|
pthread_mutex_lock(&__thread_state_mutex); |
52 |
|
if (!Running) { |
53 |
|
SignalStartThread(); |
54 |
|
pthread_cond_wait(&__thread_start_condition, &__thread_state_mutex); |
55 |
|
} |
56 |
|
pthread_mutex_unlock(&__thread_state_mutex); |
57 |
|
return 0; |
58 |
|
} |
59 |
|
|
60 |
|
/** |
61 |
|
* Starts the thread. This method will signal to start the thread and |
62 |
|
* return immediately. Note that the thread might not yet run when this |
63 |
|
* method returns! The abstract method Main() is the entry point for the |
64 |
|
* new thread. You have to implement the Main() method in your subclass. |
65 |
|
* |
66 |
|
* @see StartThread() |
67 |
|
*/ |
68 |
|
int Thread::SignalStartThread() { |
69 |
// Create and run the thread |
// Create and run the thread |
70 |
int res = pthread_create(&this->__thread_id, NULL, __pthread_launcher, this); |
int res = pthread_create(&this->__thread_id, NULL, __pthread_launcher, this); |
71 |
switch (res) { |
switch (res) { |
99 |
if (Running) { |
if (Running) { |
100 |
SignalStopThread(); |
SignalStopThread(); |
101 |
pthread_cond_wait(&__thread_exit_condition, &__thread_state_mutex); |
pthread_cond_wait(&__thread_exit_condition, &__thread_state_mutex); |
102 |
|
pthread_detach(__thread_id); |
103 |
} |
} |
104 |
pthread_mutex_unlock(&__thread_state_mutex); |
pthread_mutex_unlock(&__thread_state_mutex); |
105 |
return 0; |
return 0; |
109 |
* Stops the thread. This method will signal to stop the thread and return |
* Stops the thread. This method will signal to stop the thread and return |
110 |
* immediately. Note that the thread might still run when this method |
* immediately. Note that the thread might still run when this method |
111 |
* returns! |
* returns! |
112 |
|
* |
113 |
|
* @see StopThread() |
114 |
*/ |
*/ |
115 |
int Thread::SignalStopThread() { |
int Thread::SignalStopThread() { |
116 |
pthread_cancel(__thread_id); |
pthread_cancel(__thread_id); |
125 |
* current priority). |
* current priority). |
126 |
*/ |
*/ |
127 |
int Thread::SetSchedulingPriority() { |
int Thread::SetSchedulingPriority() { |
128 |
|
#if !defined(__APPLE__) |
129 |
struct sched_param schp; |
struct sched_param schp; |
130 |
|
|
131 |
if (mlockall(MCL_CURRENT | MCL_FUTURE) < 0) { |
if (!isRealTime) return 0; |
|
perror("WARNING, can't mlockall() memory!"); |
|
|
} |
|
|
|
|
|
if(!isRealTime) return 0; |
|
132 |
|
|
133 |
/* |
/* |
134 |
* set the process to realtime privs |
* set the process to realtime privs |
142 |
} |
} |
143 |
|
|
144 |
if (sched_setscheduler(0, SCHED_FIFO, &schp) != 0) { |
if (sched_setscheduler(0, SCHED_FIFO, &schp) != 0) { |
145 |
perror("sched_setscheduler"); |
perror("Thread: WARNING, can't assign realtime scheduling to thread!"); |
146 |
return -1; |
return -1; |
147 |
} |
} |
148 |
|
#endif |
149 |
|
return 0; |
150 |
|
} |
151 |
|
|
152 |
|
/** |
153 |
|
* Locks the memory so it will not be swapped out by the operating system. |
154 |
|
*/ |
155 |
|
int Thread::LockMemory() { |
156 |
|
#if !defined(__APPLE__) |
157 |
|
if (!bLockedMemory) return 0; |
158 |
|
if (mlockall(MCL_CURRENT | MCL_FUTURE) < 0) { |
159 |
|
perror("Thread: WARNING, can't mlockall() memory!"); |
160 |
|
return -1; |
161 |
|
} |
162 |
|
#endif |
163 |
return 0; |
return 0; |
164 |
} |
} |
165 |
|
|
175 |
pthread_setspecific(__thread_destructor_key, this); |
pthread_setspecific(__thread_destructor_key, this); |
176 |
Running = true; |
Running = true; |
177 |
pthread_mutex_unlock(&__thread_state_mutex); |
pthread_mutex_unlock(&__thread_state_mutex); |
178 |
|
pthread_cond_broadcast(&__thread_start_condition); |
179 |
} |
} |
180 |
|
|
181 |
/** |
/** |
187 |
Running = false; |
Running = false; |
188 |
pthread_mutex_unlock(&__thread_state_mutex); |
pthread_mutex_unlock(&__thread_state_mutex); |
189 |
pthread_cond_broadcast(&__thread_exit_condition); |
pthread_cond_broadcast(&__thread_exit_condition); |
190 |
|
return 0; |
191 |
} |
} |
192 |
|
|
193 |
/// Callback function for the POSIX thread API |
/// Callback function for the POSIX thread API |
194 |
void* __pthread_launcher(void* thread) { |
void* __pthread_launcher(void* thread) { |
195 |
|
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); // let the thread be killable under any circumstances |
196 |
Thread* t; |
Thread* t; |
197 |
t = (Thread*) thread; |
t = (Thread*) thread; |
|
t->EnableDestructor(); |
|
198 |
t->SetSchedulingPriority(); |
t->SetSchedulingPriority(); |
199 |
|
t->LockMemory(); |
200 |
|
t->EnableDestructor(); |
201 |
t->Main(); |
t->Main(); |
202 |
|
return NULL; |
203 |
} |
} |
204 |
|
|
205 |
/// Callback function for the POSIX thread API |
/// Callback function for the POSIX thread API |