X-Git-Url: https://git.cweiske.de/enigma2.git/blobdiff_plain/62b8a649fcae500c983215fac2e5202916c0195f..bd7a5be245b3f3bac1a822baec4378f341e0b781:/lib/base/thread.cpp diff --git a/lib/base/thread.cpp b/lib/base/thread.cpp index fa09691a..9878856e 100644 --- a/lib/base/thread.cpp +++ b/lib/base/thread.cpp @@ -2,86 +2,125 @@ #include #include +#include #include void eThread::thread_completed(void *ptr) { eThread *p = (eThread*) ptr; - eDebug("thread has completed.."); - p->alive=0; + p->m_alive = 0; + + /* recover state */ + if (!p->m_state.value()) + { + p->m_state.up(); + assert(p->m_state.value() == 1); + } + p->thread_finished(); } void *eThread::wrapper(void *ptr) { eThread *p = (eThread*)ptr; - p->alive=1; - pthread_cleanup_push( thread_completed, (void*)p ); + pthread_cleanup_push(thread_completed, (void*)p); p->thread(); pthread_exit(0); - pthread_cleanup_pop(0); + pthread_cleanup_pop(1); + return 0; } eThread::eThread() - :alive(0) + : the_thread(0), m_alive(0) { } -void eThread::run( int prio, int policy ) +int eThread::runAsync(int prio, int policy) { + eDebug("before: %d", m_state.value()); + /* the thread might already run. */ + if (sync()) + return -1; + + eDebug("after: %d", m_state.value()); + assert(m_state.value() == 1); /* sync postconditions */ + assert(!m_alive); + m_state.down(); + + m_alive = 1; + + /* start thread. */ pthread_attr_t attr; pthread_attr_init(&attr); - if (prio||policy) + + if (prio || policy) { struct sched_param p; p.__sched_priority=prio; - pthread_attr_setschedpolicy(&attr, policy ); + pthread_attr_setschedpolicy(&attr, policy); pthread_attr_setschedparam(&attr, &p); } - pthread_create(&the_thread, &attr, wrapper, this); - usleep(1000); - int timeout=20; - while(!alive && timeout--) + + if (pthread_create(&the_thread, &attr, wrapper, this)) { - eDebug("waiting for thread start..."); - usleep(1000*10); + pthread_attr_destroy(&attr); + m_alive = 0; + eDebug("couldn't create new thread"); + return -1; } - if ( !timeout ) - eDebug("thread couldn't be started !!!"); + + pthread_attr_destroy(&attr); + return 0; } +int eThread::run(int prio, int policy) +{ + if (runAsync(prio, policy)) + return -1; + sync(); + return 0; +} + eThread::~eThread() { - if ( alive ) - kill(); + kill(); +} + +int eThread::sync(void) +{ + int res; + m_state.down(); /* this might block */ + res = m_alive; + assert(m_state.value() == 0); + m_state.up(); + return res; /* 0: thread is guaranteed not to run. 1: state unknown. */ } -void eThread::sendSignal(int sig) +int eThread::sendSignal(int sig) { - if ( alive ) - pthread_kill( the_thread, sig ); - else + if (m_alive) + return pthread_kill(the_thread, sig); + else eDebug("send signal to non running thread"); + return -1; } -void eThread::kill(bool hard) +void eThread::kill(bool sendcancel) { - if ( !alive ) - { - eDebug("kill.. but thread don't running"); + if (!the_thread) /* already joined */ return; - } - if ( hard ) + if (sync() && sendcancel) { - eDebug("killing the thread..."); + eDebug("send cancel to thread"); pthread_cancel(the_thread); - alive=0; - } - else - { - eDebug("waiting for thread shutdown..."); - pthread_join(the_thread, 0); - eDebug("ok"); } + eDebug("thread joined %d", pthread_join(the_thread, 0)); + the_thread = 0; +} + +void eThread::hasStarted() +{ + assert(!m_state.value()); + m_state.up(); }