mistake
[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(thread_id_t tid);
39         Thread(thrd_t *t, void (*func)(void *), void *a);
40         ~Thread();
41         void complete();
42
43         static int swap(ucontext_t *ctxt, Thread *t);
44         static int swap(Thread *t, ucontext_t *ctxt);
45
46         thread_state get_state() { return state; }
47         void set_state(thread_state s) { state = s; }
48         thread_id_t get_id();
49         thrd_t get_thrd_t() { return *user_thread; }
50         Thread * get_parent() { return parent; }
51
52         void set_creation(ModelAction *act) { creation = act; }
53         ModelAction * get_creation() { return creation; }
54
55         /**
56          * Set a return value for the last action in this thread (e.g., for an
57          * atomic read).
58          * @param value The value to return
59          */
60         void set_return_value(uint64_t value) { last_action_val = value; }
61
62         /**
63          * Retrieve a return value for the last action in this thread. Used,
64          * for instance, for an atomic read to return the 'read' value. Should
65          * be called from a user context.
66          * @return The value 'returned' by the action
67          */
68         uint64_t get_return_value() { return last_action_val; }
69
70         /** @return True if this thread is finished executing */
71         bool is_complete() { return state == THREAD_COMPLETED; }
72
73         /** @return True if this thread is blocked */
74         bool is_blocked() { return state == THREAD_BLOCKED; }
75
76         /** @return True if no threads are waiting on this Thread */
77         bool wait_list_empty() { return wait_list.empty(); }
78
79         /**
80          * Add a ModelAction to the waiting list for this thread.
81          * @param t The ModelAction to add. Must be a JOIN.
82          */
83         void push_wait_list(ModelAction *act) { wait_list.push_back(act); }
84
85         unsigned int num_wait_list() {
86                 return wait_list.size();
87         }
88
89         ModelAction * get_waiter(unsigned int i) {
90                 return wait_list[i];
91         }
92
93         ModelAction * get_pending() { return pending; }
94         void set_pending(ModelAction *act) { pending = act; }
95         /**
96          * Remove one ModelAction from the waiting list
97          * @return The ModelAction that was removed from the waiting list
98          */
99         ModelAction * pop_wait_list() {
100                 ModelAction *ret = wait_list.front();
101                 wait_list.pop_back();
102                 return ret;
103         }
104
105         bool is_model_thread() { return model_thread; }
106
107         friend void thread_startup();
108
109         SNAPSHOTALLOC
110 private:
111         int create_context();
112         Thread *parent;
113         ModelAction *creation;
114
115         ModelAction *pending;
116         void (*start_routine)(void *);
117         void *arg;
118         ucontext_t context;
119         void *stack;
120         thrd_t *user_thread;
121         thread_id_t id;
122         thread_state state;
123
124         /**
125          * A list of ModelActions waiting on this Thread. Particularly, this
126          * list is used for thread joins, where another Thread waits for this
127          * Thread to complete
128          */
129         std::vector< ModelAction *, SnapshotAlloc<ModelAction *> > wait_list;
130
131         /**
132          * The value returned by the last action in this thread
133          * @see Thread::set_return_value()
134          * @see Thread::get_return_value()
135          */
136         uint64_t last_action_val;
137
138         /** @brief Is this Thread a special model-checker thread? */
139         const bool model_thread;
140 };
141
142 Thread * thread_current();
143
144 static inline thread_id_t thrd_to_id(thrd_t t)
145 {
146         return t;
147 }
148
149 static inline thread_id_t int_to_id(int i)
150 {
151         return i;
152 }
153
154 static inline int id_to_int(thread_id_t id)
155 {
156         return id;
157 }
158
159 #endif /* __THREADS_H__ */