0e0293ad817332e867db1e7b92c490da3c3f9b19
[c11tester.git] / threads.h
1 /** @file threads.h
2  *  @brief Model Checker Thread class.
3  */
4
5 #ifndef __THREADS_H__
6 #define __THREADS_H__
7
8 #include <ucontext.h>
9 #include "mymemory.h"
10 #include "libthreads.h"
11
12 typedef int thread_id_t;
13
14 #define THREAD_ID_T_NONE        -1
15
16 typedef enum thread_state {
17         THREAD_CREATED,
18         THREAD_RUNNING,
19         THREAD_READY,
20         THREAD_COMPLETED
21 } thread_state;
22
23 class ModelAction;
24
25 class Thread {
26 public:
27         Thread(thrd_t *t, void (*func)(void *), void *a);
28         ~Thread();
29         void complete();
30
31         static int swap(ucontext_t *ctxt, Thread *t);
32         static int swap(Thread *t, ucontext_t *ctxt);
33
34         thread_state get_state() { return state; }
35         void set_state(thread_state s) { state = s; }
36         thread_id_t get_id();
37         thrd_t get_thrd_t() { return *user_thread; }
38         Thread * get_parent() { return parent; }
39
40         void set_creation(ModelAction *act) { creation = act; }
41         ModelAction * get_creation() { return creation; }
42
43         friend void thread_startup();
44
45         SNAPSHOTALLOC
46 private:
47         int create_context();
48         Thread *parent;
49         ModelAction *creation;
50
51         void (*start_routine)(void *);
52         void *arg;
53         ucontext_t context;
54         void *stack;
55         thrd_t *user_thread;
56         thread_id_t id;
57         thread_state state;
58 };
59
60 Thread * thread_current();
61
62 static inline thread_id_t thrd_to_id(thrd_t t)
63 {
64         return t;
65 }
66
67 static inline thread_id_t int_to_id(int i)
68 {
69         return i;
70 }
71
72 static inline int id_to_int(thread_id_t id)
73 {
74         return id;
75 }
76
77 #endif /* __THREADS_H__ */