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