29 |
this->PriorityMax = PriorityMax; |
this->PriorityMax = PriorityMax; |
30 |
__thread_destructor_key = 0; |
__thread_destructor_key = 0; |
31 |
pthread_mutex_init(&__thread_state_mutex, NULL); |
pthread_mutex_init(&__thread_state_mutex, NULL); |
32 |
|
pthread_cond_init(&__thread_start_condition, NULL); |
33 |
pthread_cond_init(&__thread_exit_condition, NULL); |
pthread_cond_init(&__thread_exit_condition, NULL); |
34 |
} |
} |
35 |
|
|
36 |
Thread::~Thread() { |
Thread::~Thread() { |
37 |
if (this->Running) StopThread(); |
StopThread(); |
38 |
|
pthread_cond_destroy(&__thread_start_condition); |
39 |
pthread_cond_destroy(&__thread_exit_condition); |
pthread_cond_destroy(&__thread_exit_condition); |
40 |
pthread_mutex_destroy(&__thread_state_mutex); |
pthread_mutex_destroy(&__thread_state_mutex); |
41 |
} |
} |
42 |
|
|
43 |
/** |
/** |
44 |
* Start the thread. The Main() method is the entry point for the new |
* Starts the thread. This method will wait until the thread actually |
45 |
* thread. You have to implement the Main() method in your subclass. |
* started it's execution before it will return. The abstract method |
46 |
|
* Main() is the entry point for the new thread. You have to implement the |
47 |
|
* Main() method in your subclass. |
48 |
*/ |
*/ |
49 |
int Thread::StartThread() { |
int Thread::StartThread() { |
50 |
|
pthread_mutex_lock(&__thread_state_mutex); |
51 |
|
if (!Running) { |
52 |
|
SignalStartThread(); |
53 |
|
pthread_cond_wait(&__thread_start_condition, &__thread_state_mutex); |
54 |
|
} |
55 |
|
pthread_mutex_unlock(&__thread_state_mutex); |
56 |
|
return 0; |
57 |
|
} |
58 |
|
|
59 |
|
/** |
60 |
|
* Starts the thread. This method will signal to start the thread and |
61 |
|
* return immediately. Note that the thread might not yet run when this |
62 |
|
* method returns! The abstract method Main() is the entry point for the |
63 |
|
* new thread. You have to implement the Main() method in your subclass. |
64 |
|
* |
65 |
|
* @see StartThread() |
66 |
|
*/ |
67 |
|
int Thread::SignalStartThread() { |
68 |
// Create and run the thread |
// Create and run the thread |
69 |
int res = pthread_create(&this->__thread_id, NULL, __pthread_launcher, this); |
int res = pthread_create(&this->__thread_id, NULL, __pthread_launcher, this); |
70 |
switch (res) { |
switch (res) { |
107 |
* 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 |
108 |
* immediately. Note that the thread might still run when this method |
* immediately. Note that the thread might still run when this method |
109 |
* returns! |
* returns! |
110 |
|
* |
111 |
|
* @see StopThread() |
112 |
*/ |
*/ |
113 |
int Thread::SignalStopThread() { |
int Thread::SignalStopThread() { |
114 |
pthread_cancel(__thread_id); |
pthread_cancel(__thread_id); |
125 |
int Thread::SetSchedulingPriority() { |
int Thread::SetSchedulingPriority() { |
126 |
struct sched_param schp; |
struct sched_param schp; |
127 |
|
|
128 |
|
if (!isRealTime) return 0; |
129 |
|
|
130 |
if (mlockall(MCL_CURRENT | MCL_FUTURE) < 0) { |
if (mlockall(MCL_CURRENT | MCL_FUTURE) < 0) { |
131 |
perror("WARNING, can't mlockall() memory!"); |
perror("WARNING, can't mlockall() memory!"); |
132 |
} |
} |
133 |
|
|
|
if(!isRealTime) return 0; |
|
|
|
|
134 |
/* |
/* |
135 |
* set the process to realtime privs |
* set the process to realtime privs |
136 |
*/ |
*/ |
162 |
pthread_setspecific(__thread_destructor_key, this); |
pthread_setspecific(__thread_destructor_key, this); |
163 |
Running = true; |
Running = true; |
164 |
pthread_mutex_unlock(&__thread_state_mutex); |
pthread_mutex_unlock(&__thread_state_mutex); |
165 |
|
pthread_cond_broadcast(&__thread_start_condition); |
166 |
} |
} |
167 |
|
|
168 |
/** |
/** |
180 |
void* __pthread_launcher(void* thread) { |
void* __pthread_launcher(void* thread) { |
181 |
Thread* t; |
Thread* t; |
182 |
t = (Thread*) thread; |
t = (Thread*) thread; |
|
t->EnableDestructor(); |
|
183 |
t->SetSchedulingPriority(); |
t->SetSchedulingPriority(); |
184 |
|
t->EnableDestructor(); |
185 |
t->Main(); |
t->Main(); |
186 |
} |
} |
187 |
|
|