test: rmwprog: add MODEL_ASSERT
[c11tester.git] / threads-model.h
1 /** @file threads-model.h
2  *  @brief Model Checker Thread class.
3  */
4
5 #ifndef __THREADS_MODEL_H__
6 #define __THREADS_MODEL_H__
7
8 #include <ucontext.h>
9 #include <stdint.h>
10
11 #include "mymemory.h"
12 #include <threads.h>
13 #include "modeltypes.h"
14 #include "stl-model.h"
15
16 struct thread_params {
17         thrd_start_t func;
18         void *arg;
19 };
20
21 /** @brief Represents the state of a user Thread */
22 typedef enum thread_state {
23         /** Thread was just created and hasn't run yet */
24         THREAD_CREATED,
25         /** Thread is running */
26         THREAD_RUNNING,
27         /** Thread is not currently running but is ready to run */
28         THREAD_READY,
29         /**
30          * Thread is waiting on another action (e.g., thread completion, lock
31          * release, etc.)
32          */
33         THREAD_BLOCKED,
34         /** Thread has completed its execution */
35         THREAD_COMPLETED
36 } thread_state;
37
38 class ModelAction;
39
40 /** @brief A Thread is created for each user-space thread */
41 class Thread {
42 public:
43         Thread(thread_id_t tid);
44         Thread(thrd_t *t, void (*func)(void *), void *a, Thread *parent);
45         ~Thread();
46         void complete();
47
48         static int swap(ucontext_t *ctxt, Thread *t);
49         static int swap(Thread *t, ucontext_t *ctxt);
50
51         thread_state get_state() const { return state; }
52         void set_state(thread_state s);
53         thread_id_t get_id() const;
54         thrd_t get_thrd_t() const { return *user_thread; }
55         Thread * get_parent() const { return parent; }
56
57         void set_creation(ModelAction *act) { creation = act; }
58         ModelAction * get_creation() const { return creation; }
59
60         /**
61          * Set a return value for the last action in this thread (e.g., for an
62          * atomic read).
63          * @param value The value to return
64          */
65         void set_return_value(uint64_t value) { last_action_val = value; }
66
67         /**
68          * Retrieve a return value for the last action in this thread. Used,
69          * for instance, for an atomic read to return the 'read' value. Should
70          * be called from a user context.
71          * @return The value 'returned' by the action
72          */
73         uint64_t get_return_value() const { return last_action_val; }
74
75         /** @return True if this thread is finished executing */
76         bool is_complete() const { return state == THREAD_COMPLETED; }
77
78         /** @return True if this thread is blocked */
79         bool is_blocked() const { return state == THREAD_BLOCKED; }
80
81         /** @return True if no threads are waiting on this Thread */
82         bool wait_list_empty() const { return wait_list.empty(); }
83
84         /**
85          * Add a ModelAction to the waiting list for this thread.
86          * @param t The ModelAction to add. Must be a JOIN.
87          */
88         void push_wait_list(ModelAction *act) { wait_list.push_back(act); }
89
90         unsigned int num_wait_list() const {
91                 return wait_list.size();
92         }
93
94         ModelAction * get_waiter(unsigned int i) const {
95                 return wait_list[i];
96         }
97
98         /** @return The pending (next) ModelAction for this Thread
99          *  @see Thread::pending */
100         ModelAction * get_pending() const { return pending; }
101
102         /** @brief Set the pending (next) ModelAction for this Thread
103          *  @param act The pending ModelAction
104          *  @see Thread::pending */
105         void set_pending(ModelAction *act) { pending = act; }
106
107         Thread * waiting_on() const;
108
109         /**
110          * Remove one ModelAction from the waiting list
111          * @return The ModelAction that was removed from the waiting list
112          */
113         ModelAction * pop_wait_list() {
114                 ModelAction *ret = wait_list.front();
115                 wait_list.pop_back();
116                 return ret;
117         }
118
119         bool is_model_thread() const { return model_thread; }
120
121         friend void thread_startup();
122
123         /**
124          * Intentionally NOT allocated with MODELALLOC or SNAPSHOTALLOC.
125          * Threads should be allocated on the user's normal (snapshotting) heap
126          * to allow their allocation/deallocation to follow the same pattern as
127          * the rest of the backtracked/replayed program.
128          */
129         void * operator new(size_t size) {
130                 return Thread_malloc(size);
131         }
132         void operator delete(void *p, size_t size) {
133                 Thread_free(p);
134         }
135         void * operator new[](size_t size) {
136                 return Thread_malloc(size);
137         }
138         void operator delete[](void *p, size_t size) {
139                 Thread_free(p);
140         }
141 private:
142         int create_context();
143
144         /** @brief The parent Thread which created this Thread */
145         Thread * const parent;
146
147         /** @brief The THREAD_CREATE ModelAction which created this Thread */
148         ModelAction *creation;
149
150         /**
151          * @brief The next ModelAction to be run by this Thread
152          *
153          * This action should be kept updated by the ModelChecker, so that we
154          * always know what the next ModelAction's memory_order, action type,
155          * and location are.
156          */
157         ModelAction *pending;
158
159         void (*start_routine)(void *);
160         void *arg;
161         ucontext_t context;
162         void *stack;
163         thrd_t *user_thread;
164         thread_id_t id;
165         thread_state state;
166
167         /**
168          * A list of ModelActions waiting on this Thread. Particularly, this
169          * list is used for thread joins, where another Thread waits for this
170          * Thread to complete
171          */
172         SnapVector<ModelAction *> wait_list;
173
174         /**
175          * The value returned by the last action in this thread
176          * @see Thread::set_return_value()
177          * @see Thread::get_return_value()
178          */
179         uint64_t last_action_val;
180
181         /** @brief Is this Thread a special model-checker thread? */
182         const bool model_thread;
183 };
184
185 Thread * thread_current();
186
187 static inline thread_id_t thrd_to_id(thrd_t t)
188 {
189         return t.priv->get_id();
190 }
191
192 /**
193  * @brief Map a zero-based integer index to a unique thread ID
194  *
195  * This is the inverse of id_to_int
196  */
197 static inline thread_id_t int_to_id(int i)
198 {
199         return i;
200 }
201
202 /**
203  * @brief Map a unique thread ID to a zero-based integer index
204  *
205  * This is the inverse of int_to_id
206  */
207 static inline int id_to_int(thread_id_t id)
208 {
209         return id;
210 }
211
212 #endif /* __THREADS_MODEL_H__ */