check in message
[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 ModelAction;
20
21 class Thread {
22 public:
23         Thread(thrd_t *t, void (*func)(void *), 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         Thread * get_parent() { return parent; }
35
36         void set_creation(ModelAction *act) { creation = act; }
37         ModelAction * get_creation() { return creation; }
38
39         friend void thread_startup();
40
41         SNAPSHOTALLOC
42 private:
43         int create_context();
44         Thread *parent;
45         ModelAction *creation;
46
47         void (*start_routine)(void *);
48         void *arg;
49         ucontext_t context;
50         void *stack;
51         thrd_t *user_thread;
52         thread_id_t id;
53         thread_state state;
54 };
55
56 Thread * thread_current();
57
58 static inline thread_id_t thrd_to_id(thrd_t t)
59 {
60         return t;
61 }
62
63 static inline thread_id_t int_to_id(int i)
64 {
65         return i;
66 }
67
68 static inline int id_to_int(thread_id_t id)
69 {
70         return id;
71 }
72
73 #endif /* __THREADS_H__ */