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