5e8cbef50a95648f2b6ac35069620b44b4e433a8
[model-checker.git] / threads.h
1 /** @file threads.h
2  *  @brief Model Checker Thread class.
3  */
4
5 #ifndef __THREADS_H__
6 #define __THREADS_H__
7
8 #include <ucontext.h>
9 #include <stdint.h>
10 #include <vector>
11
12 #include "mymemory.h"
13 #include "libthreads.h"
14 #include "modeltypes.h"
15
16 /** @brief Represents the state of a user Thread */
17 typedef enum thread_state {
18         /** Thread was just created and hasn't run yet */
19         THREAD_CREATED,
20         /** Thread is running */
21         THREAD_RUNNING,
22         /** Thread is not currently running but is ready to run */
23         THREAD_READY,
24         /**
25          * Thread is waiting on another action (e.g., thread completion, lock
26          * release, etc.)
27          */
28         THREAD_BLOCKED,
29         /** Thread has completed its execution */
30         THREAD_COMPLETED
31 } thread_state;
32
33 class ModelAction;
34
35 /** @brief A Thread is created for each user-space thread */
36 class Thread {
37 public:
38         Thread(thrd_t *t, void (*func)(void *), void *a);
39         ~Thread();
40         void complete();
41
42         static int swap(ucontext_t *ctxt, Thread *t);
43         static int swap(Thread *t, ucontext_t *ctxt);
44
45         thread_state get_state() { return state; }
46         void set_state(thread_state s) { state = s; }
47         thread_id_t get_id();
48         thrd_t get_thrd_t() { return *user_thread; }
49         Thread * get_parent() { return parent; }
50
51         void set_creation(ModelAction *act) { creation = act; }
52         ModelAction * get_creation() { return creation; }
53
54         /**
55          * Set a return value for the last action in this thread (e.g., for an
56          * atomic read).
57          * @param value The value to return
58          */
59         void set_return_value(uint64_t value) { last_action_val = value; }
60
61         /**
62          * Retrieve a return value for the last action in this thread. Used,
63          * for instance, for an atomic read to return the 'read' value. Should
64          * be called from a user context.
65          * @return The value 'returned' by the action
66          */
67         uint64_t get_return_value() { return last_action_val; }
68
69         /** @return True if this thread is finished executing */
70         bool is_complete() { return state == THREAD_COMPLETED; }
71
72         /** @return True if this thread is blocked */
73         bool is_blocked() { return state == THREAD_BLOCKED; }
74
75         /** @return True if no threads are waiting on this Thread */
76         bool wait_list_empty() { return wait_list.empty(); }
77
78         /**
79          * Add a ModelAction to the waiting list for this thread.
80          * @param t The ModelAction to add. Must be a JOIN.
81          */
82         void push_wait_list(ModelAction *act) { wait_list.push_back(act); }
83
84         unsigned int num_wait_list() {
85                 return wait_list.size();
86         }
87
88         ModelAction * get_waiter(unsigned int i) {
89                 return wait_list[i];
90         }
91
92         ModelAction * get_pending() { return pending; }
93         void set_pending(ModelAction *act) { pending = act; }
94         /**
95          * Remove one ModelAction from the waiting list
96          * @return The ModelAction that was removed from the waiting list
97          */
98         ModelAction * pop_wait_list() {
99                 ModelAction *ret = wait_list.front();
100                 wait_list.pop_back();
101                 return ret;
102         }
103
104         bool is_model_thread() { return model_thread; }
105
106         friend void thread_startup();
107
108         SNAPSHOTALLOC
109 private:
110         int create_context();
111         Thread *parent;
112         ModelAction *creation;
113
114         ModelAction *pending;
115         void (*start_routine)(void *);
116         void *arg;
117         ucontext_t context;
118         void *stack;
119         thrd_t *user_thread;
120         thread_id_t id;
121         thread_state state;
122
123         /**
124          * A list of ModelActions waiting on this Thread. Particularly, this
125          * list is used for thread joins, where another Thread waits for this
126          * Thread to complete
127          */
128         std::vector<ModelAction *> wait_list;
129
130         /**
131          * The value returned by the last action in this thread
132          * @see Thread::set_return_value()
133          * @see Thread::get_return_value()
134          */
135         uint64_t last_action_val;
136
137         /** @brief Is this Thread a special model-checker thread? */
138         const bool model_thread;
139 };
140
141 Thread * thread_current();
142
143 static inline thread_id_t thrd_to_id(thrd_t t)
144 {
145         return t;
146 }
147
148 static inline thread_id_t int_to_id(int i)
149 {
150         return i;
151 }
152
153 static inline int id_to_int(thread_id_t id)
154 {
155         return id;
156 }
157
158 #endif /* __THREADS_H__ */