20 #ifdef USE_USE_GLIB_THREADS 31 #define THREAD_CREATE_ERROR -0+THREAD_ERROR_OFFSET 32 #define THREAD_MUTEX_UNINIT_ERROR -1+THREAD_ERROR_OFFSET 33 #define THREAD_MUTEX_DEADLK_ERROR -2+THREAD_ERROR_OFFSET 34 #define THREAD_MUTEX_DONTOWN_ERROR -3+THREAD_ERROR_OFFSET 35 #define THREAD_MUTEX_DESTROY_LOCKED_ERROR -4+THREAD_ERROR_OFFSET 36 #define THREAD_MUTEX_LOCKBUSY_WARNING -5+THREAD_ERROR_OFFSET 37 #define THREAD_NOTFOUND_ERROR -6+THREAD_ERROR_OFFSET 38 #define THREAD_COND_WAITBUSY_ERROR -7+THREAD_ERROR_OFFSET 39 #define THREAD_SCHED_ERROR -8+THREAD_ERROR_OFFSET 65 #ifdef USE_GLIB_THREADS 67 GError *threadErrorPtr;
76 #ifdef USE_GLIB_THREADS 87 #ifdef USE_GLIB_THREADS 88 g_thread_join(thread);
92 pthread_cancel(thread);
108 int retVal = pthread_cancel(thread);
122 #ifdef USE_GLIB_THREADS 123 int run(
void (*start_routine) (
void *),
void *data,
int priority=0) {
126 int run(
void *(*start_routine) (
void *),
void *data,
int priority=0) {
128 #ifdef USE_GLIB_THREADS 129 thread = g_thread_create((GThreadFunc)start_routine, (gpointer) data, TRUE, &threadErrorPtr);
134 pthread_attr_init(&attr);
137 printf(
"entering set priority\n");
139 if (threadResp=setPriority(&attr, priority))
141 printf(
"exited set priority\n");
143 if (threadResp=pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE))
144 return ThreadDebug().evaluateError(threadResp,
"Thread::run : Thread setting detatch state failed.");
148 if (threadResp=pthread_create(&thread, &attr, start_routine, data))
149 return ThreadDebug().evaluateError(threadResp,
"Thread::run : Thread creation failed.");
151 pthread_attr_destroy(&attr);
159 #ifndef USE_GLIB_THREADS 162 if ((res = pthread_attr_setinheritsched(attributes, PTHREAD_EXPLICIT_SCHED)))
163 return ThreadDebug().evaluateError(res,
"Cannot request explicit scheduling for thread");
165 if ((res = pthread_attr_setscope(attributes, PTHREAD_SCOPE_SYSTEM)))
166 return ThreadDebug().evaluateError(res,
"Cannot set scheduling scope for thread");
168 if ((res = pthread_attr_setschedpolicy(attributes, SCHED_FIFO)))
169 return ThreadDebug().evaluateError(res,
"Cannot request schedule policy for thread");
171 struct sched_param rt_param;
172 memset(&rt_param, 0,
sizeof(rt_param));
173 rt_param.sched_priority = priority;
174 if ((res = pthread_attr_setschedparam(attributes, &rt_param)))
175 return ThreadDebug().evaluateError(res,
"Cannot set scheduling priority for thread");
182 struct sched_param rt_param;
183 memset(&rt_param, 0,
sizeof(rt_param));
185 if ((res=pthread_getschedparam(thread, &policy, &rt_param)) != 0)
186 return ThreadDebug().evaluateError(res,
" Getting sched param.");
187 printf(
"Thread policy is %d priority is %d\n",policy, rt_param.sched_priority);
188 if (policy != SCHED_FIFO)
190 return rt_param.sched_priority;
199 #ifdef USE_GLIB_THREADS 200 g_thread_join(thread);
204 int threadResp=pthread_join(thread, &retVal);
207 perror(
"Thread::meetThread : Couldn't meet the thread");
220 #ifndef USE_GLIB_THREADS 221 pthread_exit(retVal);
229 #ifndef USE_GLIB_THREADS 230 return thread!=(int)NULL;
240 #ifdef USE_GLIB_THREADS 243 static void threadMainStatic(
void *data) {
257 virtual void *threadMain(
void)=0;
263 virtual int run(
int priority=0) {
264 return Thread::run(threadMainStatic, static_cast<void*>(
this), priority);
268 #ifndef USE_USE_GLIB_THREADS 279 pthread_mutex_init(&mut, NULL);
285 pthread_mutex_destroy(&mut);
296 int retVal=pthread_mutex_lock(&mut);
311 int retVal=pthread_mutex_trylock(&mut);
326 int retVal=pthread_mutex_unlock(&mut);
346 pthread_cond_init(&cond, NULL);
352 int retVal = pthread_cond_destroy(&cond);
362 pthread_cond_wait(&cond, &mut);
370 pthread_cond_signal(&cond);
378 pthread_cond_broadcast(&cond);
449 #endif // ifndef USE_USE_GLIB_THREADS virtual ~Cond()
Destructor.
#define THREAD_MUTEX_UNINIT_ERROR
The mutex wasn't initialised error.
int setPriority(pthread_attr_t *attributes, int priority)
#define THREAD_MUTEX_DONTOWN_ERROR
The thread doesn't own the mutex.
pthread_cond_t cond
The POSIX condition variable.
#define THREAD_MUTEX_LOCKBUSY_WARNING
The mutex can't be locked as it is already locked.
#define THREAD_COND_WAITBUSY_ERROR
One or more threads is waiting on the Cond variable.
std::map< int, std::string > errors
This will contain a map between error numbers and descriptive std::strings for the errors...
#define NO_ERROR
There is no error.
virtual int run(int priority=0)
#define THREAD_MUTEX_DEADLK_ERROR
The mutex is already locked.
int run(void *(*start_routine)(void *), void *data, int priority=0)
#define THREAD_CREATE_ERROR
Couldn't create the thread error.
#define THREAD_SCHED_ERROR
Scheduling setting error.
pthread_t thread
The thread structure.
pthread_mutex_t mut
The POSIX mutex semaphore.
static void * threadMainStatic(void *data)
#define THREAD_NOTFOUND_ERROR
The thread can't be found.
virtual ~WaitingThread(void)
#define THREAD_MUTEX_DESTROY_LOCKED_ERROR
The mutex can't be destroyed when locked.