clockvector: snapshot our clock vectors
[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 <stdint.h>
10 #include <vector>
11
12 #include "mymemory.h"
13 #include "libthreads.h"
14
15 typedef int thread_id_t;
16
17 #define THREAD_ID_T_NONE        -1
18
19 /** @brief Represents the state of a user Thread */
20 typedef enum thread_state {
21         /** Thread was just created and hasn't run yet */
22         THREAD_CREATED,
23         /** Thread is running */
24         THREAD_RUNNING,
25         /** Thread is not currently running but is ready to run */
26         THREAD_READY,
27         /**
28          * Thread is waiting on another action (e.g., thread completion, lock
29          * release, etc.)
30          */
31         THREAD_BLOCKED,
32         /** Thread has completed its execution */
33         THREAD_COMPLETED
34 } thread_state;
35
36 class ModelAction;
37
38 /** @brief A Thread is created for each user-space thread */
39 class Thread {
40 public:
41         Thread(thrd_t *t, void (*func)(void *), void *a);
42         ~Thread();
43         void complete();
44
45         static int swap(ucontext_t *ctxt, Thread *t);
46         static int swap(Thread *t, ucontext_t *ctxt);
47
48         thread_state get_state() { return state; }
49         void set_state(thread_state s) { state = s; }
50         thread_id_t get_id();
51         thrd_t get_thrd_t() { return *user_thread; }
52         Thread * get_parent() { return parent; }
53
54         void set_creation(ModelAction *act) { creation = act; }
55         ModelAction * get_creation() { return creation; }
56
57         /**
58          * Set a return value for the last action in this thread (e.g., for an
59          * atomic read).
60          * @param value The value to return
61          */
62         void set_return_value(uint64_t value) { last_action_val = value; }
63
64         /**
65          * Retrieve a return value for the last action in this thread. Used,
66          * for instance, for an atomic read to return the 'read' value. Should
67          * be called from a user context.
68          * @return The value 'returned' by the action
69          */
70         uint64_t get_return_value() { return last_action_val; }
71
72         /** @return True if this thread is finished executing */
73         bool is_complete() { return state == THREAD_COMPLETED; }
74
75         /** @return True if this thread is blocked */
76         bool is_blocked() { return state == THREAD_BLOCKED; }
77
78         /** @return True if no threads are waiting on this Thread */
79         bool wait_list_empty() { return wait_list.empty(); }
80
81         /**
82          * Add a ModelAction to the waiting list for this thread.
83          * @param t The ModelAction to add. Must be a JOIN.
84          */
85         void push_wait_list(ModelAction *act) { wait_list.push_back(act); }
86
87         ModelAction * get_pending() { return pending; }
88         void set_pending(ModelAction *act) { pending = act; }
89         /**
90          * Remove one ModelAction from the waiting list
91          * @return The ModelAction that was removed from the waiting list
92          */
93         ModelAction * pop_wait_list() {
94                 ModelAction *ret = wait_list.front();
95                 wait_list.pop_back();
96                 return ret;
97         }
98
99         friend void thread_startup();
100
101         SNAPSHOTALLOC
102 private:
103         int create_context();
104         Thread *parent;
105         ModelAction *creation;
106
107         ModelAction *pending;
108         void (*start_routine)(void *);
109         void *arg;
110         ucontext_t context;
111         void *stack;
112         thrd_t *user_thread;
113         thread_id_t id;
114         thread_state state;
115
116         /**
117          * A list of ModelActions waiting on this Thread. Particularly, this
118          * list is used for thread joins, where another Thread waits for this
119          * Thread to complete
120          */
121         std::vector<ModelAction *> wait_list;
122
123         /**
124          * The value returned by the last action in this thread
125          * @see Thread::set_return_value()
126          * @see Thread::get_return_value()
127          */
128         uint64_t last_action_val;
129 };
130
131 Thread * thread_current();
132
133 static inline thread_id_t thrd_to_id(thrd_t t)
134 {
135         return t;
136 }
137
138 static inline thread_id_t int_to_id(int i)
139 {
140         return i;
141 }
142
143 static inline int id_to_int(thread_id_t id)
144 {
145         return id;
146 }
147
148 #endif /* __THREADS_H__ */