X-Git-Url: https://git.cweiske.de/enigma2.git/blobdiff_plain/df88846fa78ebe40f935e0486462af0fc0ea81e9..e51726bcb1904bf8422b62798bd9f0cd2efb53ad:/lib/base/thread.h diff --git a/lib/base/thread.h b/lib/base/thread.h index 94cdd47e..dad80424 100644 --- a/lib/base/thread.h +++ b/lib/base/thread.h @@ -3,26 +3,63 @@ #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); + /* 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 before_set_thread_alive() { } - virtual void thread_finished() { } + + /* waits until thread is in "run" state */ + /* result: 0 - thread is not alive + 1 - thread state unknown */ + int sync(); void 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; + static void thread_completed(void *p); + + eSemaphore m_state; +protected: + void hasStarted(); }; #endif