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