edit
[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
10 #include "mymemory.h"
11 #include "threads.h"
12 #include "modeltypes.h"
13 #include "stl-model.h"
14 #include "context.h"
15 #include "classlist.h"
16 #include "pthread.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_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
51         static int swap(ucontext_t *ctxt, Thread *t);
52         static int swap(Thread *t, ucontext_t *ctxt);
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         Thread * waiting_on() const;
98         bool is_waiting_on(const Thread *t) const;
99
100         bool is_model_thread() const { return model_thread; }
101
102         friend void thread_startup();
103 #ifdef TLS
104         friend void setup_context();
105         friend void * helper_thread(void *);
106         friend void finalize_helper_thread();
107 #endif
108
109         /**
110          * Intentionally NOT allocated with MODELALLOC or SNAPSHOTALLOC.
111          * Threads should be allocated on the user's normal (snapshotting) heap
112          * to allow their allocation/deallocation to follow the same pattern as
113          * the rest of the backtracked/replayed program.
114          */
115         void * operator new(size_t size) {
116                 return Thread_malloc(size);
117         }
118         void operator delete(void *p, size_t size) {
119                 Thread_free(p);
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 #ifdef TLS
128         void setTLS(char *_tls) { tls = _tls;}
129 #endif
130 private:
131         int create_context();
132
133         /** @brief The parent Thread which created this Thread */
134         Thread * const parent;
135
136         /** @brief The THREAD_CREATE ModelAction which created this Thread */
137         ModelAction *creation;
138
139         /**
140          * @brief The next ModelAction to be run by this Thread
141          *
142          * This action should be kept updated by the ModelChecker, so that we
143          * always know what the next ModelAction's memory_order, action type,
144          * and location are.
145          */
146         ModelAction *pending;
147
148         void (*start_routine)(void *);
149         void *(*pstart_routine)(void *);
150
151         void *arg;
152         ucontext_t context;
153         void *stack;
154 #ifdef TLS
155 public:
156         char *tls;
157         ucontext_t helpercontext;
158         pthread_mutex_t mutex;
159         pthread_mutex_t mutex2;
160         pthread_t thread;
161 private:
162 #endif
163         thrd_t *user_thread;
164         thread_id_t id;
165         thread_state state;
166
167         /**
168          * The value returned by the last action in this thread
169          * @see Thread::set_return_value()
170          * @see Thread::get_return_value()
171          */
172         uint64_t last_action_val;
173
174         /** the value return from pthread functions */
175         void * pthread_return;
176
177         /** @brief Is this Thread a special model-checker thread? */
178         const bool model_thread;
179 };
180
181 #ifdef TLS
182 uintptr_t get_tls_addr();
183 #endif
184
185 Thread * thread_current();
186 void thread_startup();
187 void main_thread_startup();
188
189 static inline thread_id_t thrd_to_id(thrd_t t)
190 {
191         return t.priv->get_id();
192 }
193
194 /**
195  * @brief Map a zero-based integer index to a unique thread ID
196  *
197  * This is the inverse of id_to_int
198  */
199 static inline thread_id_t int_to_id(int i)
200 {
201         return i;
202 }
203
204 /**
205  * @brief Map a unique thread ID to a zero-based integer index
206  *
207  * This is the inverse of int_to_id
208  */
209 static inline int id_to_int(thread_id_t id)
210 {
211         return id;
212 }
213
214 #endif  /* __THREADS_MODEL_H__ */