servicemp3.cpp: more simple/flexible streaming detection
[enigma2.git] / lib / base / thread.cpp
index 9878856eaa483007ccaa7d92f42043645c49a835..1fda6a47a944efb23dd7e76a72cd7ba444991c0c 100644 (file)
@@ -2,7 +2,6 @@
 
 #include <stdio.h>
 #include <unistd.h>
-#include <assert.h>
 #include <lib/base/eerror.h>
 
 void eThread::thread_completed(void *ptr)
@@ -10,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();
 }
@@ -43,11 +39,13 @@ int eThread::runAsync(int prio, int policy)
                return -1;
        
        eDebug("after: %d", m_state.value());
-       assert(m_state.value() == 1); /* sync postconditions */
-       assert(!m_alive);
+       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;
@@ -60,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);
@@ -71,7 +74,7 @@ int eThread::runAsync(int prio, int policy)
        
        pthread_attr_destroy(&attr);
        return 0;
-}                     
+}
 
 int eThread::run(int prio, int policy)
 {
@@ -89,9 +92,12 @@ eThread::~eThread()
 int eThread::sync(void)
 {
        int res;
+       int debug_val_before = m_state.value();
        m_state.down(); /* this might block */
        res = m_alive;
-       assert(m_state.value() == 0);
+       if (m_state.value() != 0)
+               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. */
 }
@@ -121,6 +127,7 @@ void eThread::kill(bool sendcancel)
 
 void eThread::hasStarted()
 {
-       assert(!m_state.value());
+       ASSERT(!m_state.value());
+       m_started = 1;
        m_state.up();
 }