1 #ifndef __lib_base_thread_h
2 #define __lib_base_thread_h
6 #include <lib/base/elock.h>
8 /* the following states are possible:
9 1 thread has not yet started
10 2 thread has started, but not completed initialization (hadStarted not yet called)
12 4 thread has finished, but not yet joined
13 5 thread has joined (same as state 1)
16 0 (="not alive") for 1, 4, 5
19 it will wait when state is 2. It can't differentiate between
20 state 3 and 4, because a thread could always finish.
22 all other state transitions (1 -> 2, 4 -> 5) must be activately
23 changed by either calling run() or kill().
32 /* thread_finished is called from within the thread context as the last function
33 before the thread is going to die.
34 It should be used to do final cleanups (unlock locked mutexes ....) */
35 virtual void thread_finished() {}
37 /* runAsync starts the thread.
38 it assumes that the thread is not running,
39 i.e. sync() *must* return 0.
40 it will not wait for anything. */
41 int runAsync(int prio=0, int policy=0);
43 /* run will wait until the thread has either
44 passed it's initialization, or it has
46 int run(int prio=0, int policy=0);
48 virtual void thread()=0;
50 /* waits until thread is in "run" state */
51 /* result: 0 - thread is not alive
52 1 - thread state unknown */
54 int sendSignal(int sig);
56 /* join the thread, i.e. busywait until thread has finnished. */
57 void kill(bool sendcancel=false);
61 static void *wrapper(void *ptr);
63 static void thread_completed(void *p);