threads: set up Thread to be freed properly
[model-checker.git] / threads.h
1 #ifndef __THREADS_H__
2 #define __THREADS_H__
3
4 #include <ucontext.h>
5
6 #include "libthreads.h"
7
8 #define THREAD_ID_T_NONE        -1
9
10 typedef enum thread_state {
11         THREAD_CREATED,
12         THREAD_RUNNING,
13         THREAD_READY,
14         THREAD_COMPLETED
15 } thread_state;
16
17 class Thread {
18 public:
19         Thread(thrd_t *t, void (*func)(), void *a);
20         Thread(thrd_t *t);
21         ~Thread();
22         void complete();
23         int swap(Thread *t);
24
25         thread_state get_state() { return state; }
26         void set_state(thread_state s) { state = s; }
27         thread_id_t get_id();
28         thrd_t get_thrd_t() { return *user_thread; }
29 private:
30         int create_context();
31
32         void (*start_routine)();
33         void *arg;
34         ucontext_t context;
35         void *stack;
36         thrd_t *user_thread;
37         thread_id_t id;
38         thread_state state;
39 };
40
41 Thread * thread_current();
42
43 static inline thread_id_t thrd_to_id(thrd_t t)
44 {
45         return t;
46 }
47
48 #endif /* __THREADS_H__ */