X-Git-Url: https://git.cweiske.de/enigma2.git/blobdiff_plain/62b8a649fcae500c983215fac2e5202916c0195f..HEAD:/lib/base/thread.h diff --git a/lib/base/thread.h b/lib/base/thread.h index f7130057..8ee96594 100644 --- a/lib/base/thread.h +++ b/lib/base/thread.h @@ -3,25 +3,68 @@ #include #include +#include + +/* the following states are possible: + 1 thread has not yet started + 2 thread has started, but not completed initialization (hadStarted not yet called) + 3 thread is running + 4 thread has finished, but not yet joined + 5 thread has joined (same as state 1) + + sync() will return: + 0 (="not alive") for 1, 4, 5 + 1 for 3, 4 + + it will wait when state is 2. It can't differentiate between + state 3 and 4, because a thread could always finish. + + all other state transitions (1 -> 2, 4 -> 5) must be activately + changed by either calling run() or kill(). + */ class eThread { - pthread_t the_thread; - static void *wrapper(void *ptr); - int alive; - static void thread_completed(void *p); public: - bool thread_running() { return alive; } eThread(); virtual ~eThread(); - - void run(int prio=0,int policy=0); + + /* thread_finished is called from within the thread context as the last function + before the thread is going to die. + It should be used to do final cleanups (unlock locked mutexes ....) */ + virtual void thread_finished() {} + + /* runAsync starts the thread. + it assumes that the thread is not running, + i.e. sync() *must* return 0. + it will not wait for anything. */ + int runAsync(int prio=0, int policy=0); + + /* run will wait until the thread has either + passed it's initialization, or it has + died again. */ + int run(int prio=0, int policy=0); virtual void thread()=0; - virtual void thread_finished() { } + + /* waits until thread is in "run" state */ + /* result: 0 - thread is not alive + 1 - thread state unknown */ + int sync(); + int sendSignal(int sig); + + /* join the thread, i.e. busywait until thread has finnished. */ + void kill(bool sendcancel=false); +private: + pthread_t the_thread; + + static void *wrapper(void *ptr); + int m_alive, m_started; + static void thread_completed(void *p); - void sendSignal(int sig); - void kill(bool hard=false); + eSemaphore m_state; +protected: + void hasStarted(); }; #endif