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 (!bLockedMemory) return 0; |
157 |
|
if (mlockall(MCL_CURRENT | MCL_FUTURE) < 0) { |
158 |
|
perror("Thread: WARNING, can't mlockall() memory!"); |
159 |
|
return -1; |
160 |
|
} |
161 |
return 0; |
return 0; |
162 |
} |
} |
163 |
|
|
193 |
Thread* t; |
Thread* t; |
194 |
t = (Thread*) thread; |
t = (Thread*) thread; |
195 |
t->SetSchedulingPriority(); |
t->SetSchedulingPriority(); |
196 |
|
t->LockMemory(); |
197 |
t->EnableDestructor(); |
t->EnableDestructor(); |
198 |
t->Main(); |
t->Main(); |
199 |
} |
} |