factor page alignment into function call...place near existing call
[model-checker.git] / threads.h
1 #ifndef __THREADS_H__
2 #define __THREADS_H__
3
4 #include <ucontext.h>
5 #include "mymemory.h"
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         Thread(thrd_t *t, void (*func)(void *), void *a);
22         ~Thread();
23         void complete();
24
25         static int swap(ucontext_t *ctxt, Thread *t);
26         static int swap(Thread *t, ucontext_t *ctxt);
27
28         thread_state get_state() { return state; }
29         void set_state(thread_state s) { state = s; }
30         thread_id_t get_id();
31         thrd_t get_thrd_t() { return *user_thread; }
32         Thread * get_parent() { return parent; }
33
34         friend void thread_startup();
35
36         MEMALLOC
37 private:
38         int create_context();
39         Thread *parent;
40
41         void (*start_routine)(void *);
42         void *arg;
43         ucontext_t context;
44         void *stack;
45         thrd_t *user_thread;
46         thread_id_t id;
47         thread_state state;
48 };
49
50 Thread * thread_current();
51
52 static inline thread_id_t thrd_to_id(thrd_t t)
53 {
54         return t;
55 }
56
57 static inline thread_id_t int_to_id(int i)
58 {
59         return i;
60 }
61
62 static inline int id_to_int(thread_id_t id)
63 {
64         return id;
65 }
66
67 #endif /* __THREADS_H__ */