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