add support for pthread_create (in progress)
[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 <stdint.h>
9
10 #include "mymemory.h"
11 #include <threads.h>
12 #include "modeltypes.h"
13 #include "stl-model.h"
14 #include "context.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(thread_id_t tid, thrd_t *t, void (*func)(void *), void *a, Thread *parent);
45         Thread(thread_id_t tid, thrd_t *t, void *(*func)(void *), void *a, Thread *parent);
46
47         ~Thread();
48         void complete();
49
50         static int swap(ucontext_t *ctxt, Thread *t);
51         static int swap(Thread *t, ucontext_t *ctxt);
52
53         thread_state get_state() const { return state; }
54         void set_state(thread_state s);
55         thread_id_t get_id() const;
56         thrd_t get_thrd_t() const { return *user_thread; }
57         Thread * get_parent() const { return parent; }
58
59         void set_creation(ModelAction *act) { creation = act; }
60         ModelAction * get_creation() const { return creation; }
61
62         /**
63          * Set a return value for the last action in this thread (e.g., for an
64          * atomic read).
65          * @param value The value to return
66          */
67         void set_return_value(uint64_t value) { last_action_val = value; }
68
69         /**
70          * Retrieve a return value for the last action in this thread. Used,
71          * for instance, for an atomic read to return the 'read' value. Should
72          * be called from a user context.
73          * @return The value 'returned' by the action
74          */
75         uint64_t get_return_value() const { return last_action_val; }
76
77         /** @return True if this thread is finished executing */
78         bool is_complete() const { return state == THREAD_COMPLETED; }
79
80         /** @return True if this thread is blocked */
81         bool is_blocked() const { return state == THREAD_BLOCKED; }
82
83         /** @return The pending (next) ModelAction for this Thread
84          *  @see Thread::pending */
85         ModelAction * get_pending() const { return pending; }
86
87         /** @brief Set the pending (next) ModelAction for this Thread
88          *  @param act The pending ModelAction
89          *  @see Thread::pending */
90         void set_pending(ModelAction *act) { pending = act; }
91
92         Thread * waiting_on() const;
93         bool is_waiting_on(const Thread *t) const;
94
95         bool is_model_thread() const { return model_thread; }
96
97         friend void thread_startup();
98
99         /**
100          * Intentionally NOT allocated with MODELALLOC or SNAPSHOTALLOC.
101          * Threads should be allocated on the user's normal (snapshotting) heap
102          * to allow their allocation/deallocation to follow the same pattern as
103          * the rest of the backtracked/replayed program.
104          */
105         void * operator new(size_t size) {
106                 return Thread_malloc(size);
107         }
108         void operator delete(void *p, size_t size) {
109                 Thread_free(p);
110         }
111         void * operator new[](size_t size) {
112                 return Thread_malloc(size);
113         }
114         void operator delete[](void *p, size_t size) {
115                 Thread_free(p);
116         }
117 private:
118         int create_context();
119
120         /** @brief The parent Thread which created this Thread */
121         Thread * const parent;
122
123         /** @brief The THREAD_CREATE ModelAction which created this Thread */
124         ModelAction *creation;
125
126         /**
127          * @brief The next ModelAction to be run by this Thread
128          *
129          * This action should be kept updated by the ModelChecker, so that we
130          * always know what the next ModelAction's memory_order, action type,
131          * and location are.
132          */
133         ModelAction *pending;
134
135         void (*start_routine)(void *);
136         void *(*pstart_routine)(void *);
137
138         void *arg;
139         ucontext_t context;
140         void *stack;
141         thrd_t *user_thread;
142         thread_id_t id;
143         thread_state state;
144
145         /**
146          * The value returned by the last action in this thread
147          * @see Thread::set_return_value()
148          * @see Thread::get_return_value()
149          */
150         uint64_t last_action_val;
151
152         /** @brief Is this Thread a special model-checker thread? */
153         const bool model_thread;
154 };
155
156 Thread * thread_current();
157
158 static inline thread_id_t thrd_to_id(thrd_t t)
159 {
160         return t.priv->get_id();
161 }
162
163 /**
164  * @brief Map a zero-based integer index to a unique thread ID
165  *
166  * This is the inverse of id_to_int
167  */
168 static inline thread_id_t int_to_id(int i)
169 {
170         return i;
171 }
172
173 /**
174  * @brief Map a unique thread ID to a zero-based integer index
175  *
176  * This is the inverse of int_to_id
177  */
178 static inline int id_to_int(thread_id_t id)
179 {
180         return id;
181 }
182
183 #endif /* __THREADS_MODEL_H__ */