fix 'thread recovery' race with sync() by not abusing m_state.
authorFelix Domke <tmbinc@elitedvb.net>
Mon, 18 May 2009 20:47:43 +0000 (22:47 +0200)
committerFelix Domke <tmbinc@elitedvb.net>
Mon, 18 May 2009 20:47:43 +0000 (22:47 +0200)
lib/base/thread.cpp
lib/base/thread.h

index 58c4fd72619a1064f2971fe265a0f41d3df8c5b7..fa923debe3ddf08aed24ec059b663a1b7486e071 100644 (file)
@@ -9,12 +9,9 @@ void eThread::thread_completed(void *ptr)
        eThread *p = (eThread*) ptr;
        p->m_alive = 0;
 
        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();
 }
 
        p->thread_finished();
 }
@@ -48,6 +45,7 @@ int eThread::runAsync(int prio, int policy)
        ASSERT(m_state.value() == 0);
        
        m_alive = 1;
        ASSERT(m_state.value() == 0);
        
        m_alive = 1;
+       m_started = 0;
 
                /* start thread. */
        pthread_attr_t attr;
 
                /* start thread. */
        pthread_attr_t attr;
@@ -125,5 +123,6 @@ void eThread::kill(bool sendcancel)
 void eThread::hasStarted()
 {
        ASSERT(!m_state.value());
 void eThread::hasStarted()
 {
        ASSERT(!m_state.value());
+       m_started = 1;
        m_state.up();
 }
        m_state.up();
 }
index 12c5b25cbaba0c1c72f4e4ea9549d2320b9f566c..8ee96594aa6c940e4ee30180ba598039bc188cd2 100644 (file)
@@ -59,7 +59,7 @@ private:
        pthread_t the_thread;
 
        static void *wrapper(void *ptr);
        pthread_t the_thread;
 
        static void *wrapper(void *ptr);
-       int m_alive;
+       int m_alive, m_started;
        static void thread_completed(void *p);
 
        eSemaphore m_state;
        static void thread_completed(void *p);
 
        eSemaphore m_state;