X-Git-Url: https://git.cweiske.de/enigma2.git/blobdiff_plain/056af5306995176e457ddd471aa2b5c3a5223c4f..HEAD:/lib/base/thread.cpp diff --git a/lib/base/thread.cpp b/lib/base/thread.cpp index 56467597..1fda6a47 100644 --- a/lib/base/thread.cpp +++ b/lib/base/thread.cpp @@ -9,12 +9,9 @@ void eThread::thread_completed(void *ptr) eThread *p = (eThread*) ptr; p->m_alive = 0; - /* recover state */ - if (!p->m_state.value()) - { - p->m_state.up(); - ASSERT(p->m_state.value() == 1); - } + /* recover state in case thread was cancelled before calling hasStarted */ + if (!p->m_started) + p->hasStarted(); p->thread_finished(); } @@ -45,8 +42,10 @@ int eThread::runAsync(int prio, int policy) ASSERT(m_state.value() == 1); /* sync postconditions */ ASSERT(!m_alive); m_state.down(); + ASSERT(m_state.value() == 0); m_alive = 1; + m_started = 0; /* start thread. */ pthread_attr_t attr; @@ -59,7 +58,12 @@ int eThread::runAsync(int prio, int policy) pthread_attr_setschedpolicy(&attr, policy); pthread_attr_setschedparam(&attr, &p); } - + + if (the_thread) { + eDebug("old thread joined %d", pthread_join(the_thread, 0)); + the_thread = 0; + } + if (pthread_create(&the_thread, &attr, wrapper, this)) { pthread_attr_destroy(&attr); @@ -70,7 +74,7 @@ int eThread::runAsync(int prio, int policy) pthread_attr_destroy(&attr); return 0; -} +} int eThread::run(int prio, int policy) { @@ -88,10 +92,11 @@ eThread::~eThread() int eThread::sync(void) { int res; + int debug_val_before = m_state.value(); m_state.down(); /* this might block */ res = m_alive; if (m_state.value() != 0) - eFatal("eThread::sync: m_state.value() == %d", m_state.value()); + eFatal("eThread::sync: m_state.value() == %d - was %d before", m_state.value(), debug_val_before); ASSERT(m_state.value() == 0); m_state.up(); return res; /* 0: thread is guaranteed not to run. 1: state unknown. */ @@ -123,5 +128,6 @@ void eThread::kill(bool sendcancel) void eThread::hasStarted() { ASSERT(!m_state.value()); + m_started = 1; m_state.up(); }