X-Git-Url: https://git.cweiske.de/enigma2.git/blobdiff_plain/d63d2c3c6cbbd574dda4f8b00ebe6c661735edd5..9fac5ff7371972cef663d456b96c68f51b5c2c01:/lib/base/thread.cpp diff --git a/lib/base/thread.cpp b/lib/base/thread.cpp index 4cff9259..08ddc1fb 100644 --- a/lib/base/thread.cpp +++ b/lib/base/thread.cpp @@ -1,34 +1,88 @@ #include + #include +#include #include +void eThread::thread_completed(void *ptr) +{ + eThread *p = (eThread*) ptr; + eDebug("thread has completed.."); + p->alive=0; + p->thread_finished(); +} + void *eThread::wrapper(void *ptr) { - ((eThread*)ptr)->thread(); + eThread *p = (eThread*)ptr; + p->alive=1; + pthread_cleanup_push( thread_completed, (void*)p ); + p->thread(); pthread_exit(0); + pthread_cleanup_pop(0); + return 0; } eThread::eThread() + :the_thread(0), alive(0) { - alive=0; } -void eThread::run() +void eThread::run( int prio, int policy ) { - alive=1; - pthread_create(&the_thread, 0, wrapper, this); -} + if (alive) + { + eDebug("thread already running !!"); + return; + } + pthread_attr_t attr; + pthread_attr_init(&attr); + if (prio||policy) + { + struct sched_param p; + p.__sched_priority=prio; + pthread_attr_setschedpolicy(&attr, policy ); + pthread_attr_setschedparam(&attr, &p); + } + if ( pthread_create(&the_thread, &attr, wrapper, this) ) + { + eDebug("couldn't create new thread"); + return; + } + pthread_attr_destroy(&attr); + usleep(1000); + int timeout=20; + while(!alive && timeout--) + { + eDebug("waiting for thread start..."); + usleep(1000*10); + } + if ( !timeout ) + eDebug("thread couldn't be started !!!"); +} eThread::~eThread() { - if (alive) - kill(); + kill(); +} + +void eThread::sendSignal(int sig) +{ + if ( alive ) + pthread_kill( the_thread, sig ); + else + eDebug("send signal to non running thread"); } -void eThread::kill() +void eThread::kill(bool sendcancel) { - alive=0; - eDebug("waiting for thread shutdown"); - pthread_join(the_thread, 0); - eDebug("ok"); + if (!the_thread) + return; + if ( alive && sendcancel ) + { + eDebug("send cancel to thread"); + pthread_cancel(the_thread); + } + eDebug("thread joined %d", pthread_join(the_thread, 0)); + the_thread=0; }