threads: move thread_id_t definition and redefine thrd_t type
[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 typedef int thread_id_t;
9
10 #define THREAD_ID_T_NONE        -1
11
12 typedef enum thread_state {
13         THREAD_CREATED,
14         THREAD_RUNNING,
15         THREAD_READY,
16         THREAD_COMPLETED
17 } thread_state;
18
19 class Thread {
20 public:
21         void * operator new(size_t size);
22         void operator delete(void *ptr);
23         Thread(thrd_t *t, void (*func)(), void *a);
24         ~Thread();
25         void complete();
26
27         static int swap(ucontext_t *ctxt, Thread *t);
28         static int swap(Thread *t, ucontext_t *ctxt);
29
30         thread_state get_state() { return state; }
31         void set_state(thread_state s) { state = s; }
32         thread_id_t get_id();
33         thrd_t get_thrd_t() { return *user_thread; }
34 private:
35         int create_context();
36
37         void (*start_routine)();
38         void *arg;
39         ucontext_t context;
40         void *stack;
41         thrd_t *user_thread;
42         thread_id_t id;
43         thread_state state;
44 };
45
46 Thread * thread_current();
47
48 static inline thread_id_t thrd_to_id(thrd_t t)
49 {
50         return t;
51 }
52
53 static inline thread_id_t int_to_id(int i)
54 {
55         return i;
56 }
57
58 static inline int id_to_int(thread_id_t id)
59 {
60         return id;
61 }
62
63 #endif /* __THREADS_H__ */