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; |
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; |
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 (!isRealTime) return 0; |
if (!isRealTime) return 0; |
132 |
|
|
|
if (mlockall(MCL_CURRENT | MCL_FUTURE) < 0) { |
|
|
perror("WARNING, can't mlockall() memory!"); |
|
|
} |
|
|
|
|
133 |
/* |
/* |
134 |
* set the process to realtime privs |
* set the process to realtime privs |
135 |
*/ |
*/ |
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 |
|
|
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; |
198 |
t->SetSchedulingPriority(); |
t->SetSchedulingPriority(); |
199 |
|
t->LockMemory(); |
200 |
t->EnableDestructor(); |
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 |