Eliminate system context
[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 #include "mymemory.h"
10 #include "threads.h"
11 #include "modeltypes.h"
12 #include "stl-model.h"
13 #include "context.h"
14 #include "classlist.h"
15 #include "pthread.h"
16
17 struct thread_params {
18         thrd_start_t func;
19         void *arg;
20 };
21
22 /** @brief Represents the state of a user Thread */
23 typedef enum thread_state {
24         /** Thread was just created and hasn't run yet */
25         THREAD_CREATED,
26         /** Thread is running */
27         THREAD_RUNNING,
28         /** Thread is not currently running but is ready to run */
29         THREAD_READY,
30         /**
31          * Thread is waiting on another action (e.g., thread completion, lock
32          * release, etc.)
33          */
34         THREAD_BLOCKED,
35         /** Thread has completed its execution */
36         THREAD_COMPLETED
37 } thread_state;
38
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         void freeResources();
50
51         static int swap(ucontext_t *ctxt, Thread *t);
52         static int swap(Thread *t, ucontext_t *ctxt);
53         static int swap(Thread *t, Thread *t2);
54
55         thread_state get_state() const { return state; }
56         void set_state(thread_state s);
57         thread_id_t get_id() const;
58         thrd_t get_thrd_t() const { return *user_thread; }
59         Thread * get_parent() const { return parent; }
60
61         void set_creation(ModelAction *act) { creation = act; }
62         ModelAction * get_creation() const { return creation; }
63
64         /**
65          * Set a return value for the last action in this thread (e.g., for an
66          * atomic read).
67          * @param value The value to return
68          */
69         void set_return_value(uint64_t value) { last_action_val = value; }
70
71         /**
72          * Retrieve a return value for the last action in this thread. Used,
73          * for instance, for an atomic read to return the 'read' value. Should
74          * be called from a user context.
75          * @return The value 'returned' by the action
76          */
77         uint64_t get_return_value() const { return last_action_val; }
78
79         /** @set and get the return value from pthread functions */
80         void set_pthread_return(void *ret) { pthread_return = ret; }
81         void * get_pthread_return() { return pthread_return; }
82
83         /** @return True if this thread is finished executing */
84         bool is_complete() const { return state == THREAD_COMPLETED; }
85
86         /** @return True if this thread is blocked */
87         bool is_blocked() const { return state == THREAD_BLOCKED; }
88
89         /** @return The pending (next) ModelAction for this Thread
90          *  @see Thread::pending */
91         ModelAction * get_pending() const { return pending; }
92
93         /** @brief Set the pending (next) ModelAction for this Thread
94          *  @param act The pending ModelAction
95          *  @see Thread::pending */
96         void set_pending(ModelAction *act) { pending = act; }
97
98         bool just_woken_up() { return wakeup_state; }
99         void set_wakeup_state(bool state) { wakeup_state = state; }
100
101         Thread * waiting_on() const;
102         bool is_waiting_on(const Thread *t) const;
103
104         bool is_model_thread() const { return model_thread; }
105
106         void * get_stack_addr() { return stack; }
107         ClockVector * get_acq_fence_cv() { return acq_fence_cv; }
108
109         friend void thread_startup();
110 #ifdef TLS
111         friend void setup_context();
112         friend void * helper_thread(void *);
113         friend void finalize_helper_thread();
114 #endif
115
116         /**
117          * Intentionally NOT allocated with MODELALLOC or SNAPSHOTALLOC.
118          * Threads should be allocated on the user's normal (snapshotting) heap
119          * to allow their allocation/deallocation to follow the same pattern as
120          * the rest of the backtracked/replayed program.
121          */
122         void * operator new(size_t size) {
123                 return Thread_malloc(size);
124         }
125         void operator delete(void *p, size_t size) {
126                 Thread_free(p);
127         }
128         void * operator new[](size_t size) {
129                 return Thread_malloc(size);
130         }
131         void operator delete[](void *p, size_t size) {
132                 Thread_free(p);
133         }
134 #ifdef TLS
135         void setTLS(char *_tls) { tls = _tls;}
136 #endif
137 private:
138         int create_context();
139
140         /** @brief The parent Thread which created this Thread */
141         Thread * const parent;
142
143         /** @brief Acquire fence cv */
144         ClockVector *acq_fence_cv;
145
146         /** @brief The THREAD_CREATE ModelAction which created this Thread */
147         ModelAction *creation;
148
149         /**
150          * @brief The next ModelAction to be run by this Thread
151          *
152          * This action should be kept updated by the ModelChecker, so that we
153          * always know what the next ModelAction's memory_order, action type,
154          * and location are.
155          */
156         ModelAction *pending;
157
158         /** @brief True if this thread was just woken up */
159         bool wakeup_state;
160
161         void (*start_routine)(void *);
162         void *(*pstart_routine)(void *);
163
164         void *arg;
165         ucontext_t context;
166         void *stack;
167         uint32_t stack_size;
168 #ifdef TLS
169         void * helper_stack;
170 public:
171         char *tls;
172         ucontext_t helpercontext;
173         pthread_mutex_t mutex;
174         pthread_mutex_t mutex2;
175         pthread_t thread;
176 private:
177 #endif
178         thrd_t *user_thread;
179         thread_id_t id;
180         thread_state state;
181
182         /**
183          * The value returned by the last action in this thread
184          * @see Thread::set_return_value()
185          * @see Thread::get_return_value()
186          */
187         uint64_t last_action_val;
188
189         /** the value return from pthread functions */
190         void * pthread_return;
191
192         /** @brief Is this Thread a special model-checker thread? */
193         const bool model_thread;
194 };
195
196 #ifdef TLS
197 uintptr_t get_tls_addr();
198 void tlsdestructor(void *v);
199 #endif
200
201 Thread * thread_current();
202 void thread_startup();
203 void initMainThread();
204
205 static inline thread_id_t thrd_to_id(thrd_t t)
206 {
207         return t.priv->get_id();
208 }
209
210 /**
211  * @brief Map a zero-based integer index to a unique thread ID
212  *
213  * This is the inverse of id_to_int
214  */
215 static inline thread_id_t int_to_id(int i)
216 {
217         return i;
218 }
219
220 /**
221  * @brief Map a unique thread ID to a zero-based integer index
222  *
223  * This is the inverse of int_to_id
224  */
225 static inline int id_to_int(thread_id_t id)
226 {
227         return id;
228 }
229
230 int real_pthread_mutex_init(pthread_mutex_t *__mutex, const pthread_mutexattr_t *__mutexattr);
231 int real_pthread_mutex_lock (pthread_mutex_t *__mutex);
232 int real_pthread_mutex_unlock (pthread_mutex_t *__mutex);
233 int real_pthread_create (pthread_t *__restrict __newthread, const pthread_attr_t *__restrict __attr, void *(*__start_routine)(void *), void *__restrict __arg);
234 int real_pthread_join (pthread_t __th, void ** __thread_return);
235 void real_pthread_exit (void * value_ptr) __attribute__((noreturn));
236 void real_init_all();
237
238 #endif  /* __THREADS_MODEL_H__ */