threads: save id within class Thread
[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         int swap(Thread *t);
22         void dispose();
23
24         thread_state get_state() { return state; }
25         void set_state(thread_state s) { state = s; }
26         thread_id_t get_id();
27         thrd_t get_thrd_t() { return *user_thread; }
28 private:
29         int create_context();
30
31         void (*start_routine)();
32         void *arg;
33         ucontext_t context;
34         void *stack;
35         thrd_t *user_thread;
36         thread_id_t id;
37         thread_state state;
38 };
39
40 Thread * thread_current();
41
42 static inline thread_id_t thrd_to_id(thrd_t t)
43 {
44         return t;
45 }
46
47 #endif /* __THREADS_H__ */