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