threads: add a wait_list
[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
15 typedef int thread_id_t;
16
17 #define THREAD_ID_T_NONE        -1
18
19 /** @brief Represents the state of a user Thread */
20 typedef enum thread_state {
21         /** Thread was just created and hasn't run yet */
22         THREAD_CREATED,
23         /** Thread is running */
24         THREAD_RUNNING,
25         /**
26          * Thread has yielded to the model-checker but is ready to run. Used
27          * during an action that caused a context switch to the model-checking
28          * context.
29          */
30         THREAD_READY,
31         /** Thread has completed its execution */
32         THREAD_COMPLETED
33 } thread_state;
34
35 class ModelAction;
36
37 /** @brief A Thread is created for each user-space thread */
38 class Thread {
39 public:
40         Thread(thrd_t *t, void (*func)(void *), void *a);
41         ~Thread();
42         void complete();
43
44         static int swap(ucontext_t *ctxt, Thread *t);
45         static int swap(Thread *t, ucontext_t *ctxt);
46
47         thread_state get_state() { return state; }
48         void set_state(thread_state s) { state = s; }
49         thread_id_t get_id();
50         thrd_t get_thrd_t() { return *user_thread; }
51         Thread * get_parent() { return parent; }
52
53         void set_creation(ModelAction *act) { creation = act; }
54         ModelAction * get_creation() { return creation; }
55
56         /**
57          * Set a return value for the last action in this thread (e.g., for an
58          * atomic read).
59          * @param value The value to return
60          */
61         void set_return_value(uint64_t value) { last_action_val = value; }
62
63         /**
64          * Retrieve a return value for the last action in this thread. Used,
65          * for instance, for an atomic read to return the 'read' value. Should
66          * be called from a user context.
67          * @return The value 'returned' by the action
68          */
69         uint64_t get_return_value() { return last_action_val; }
70
71         /** @return True if this thread is finished executing */
72         bool is_complete() { return state == THREAD_COMPLETED; }
73
74         /** @return True if no threads are waiting on this Thread */
75         bool wait_list_empty() { return wait_list.empty(); }
76
77         /**
78          * Add a thread to the waiting list for this thread.
79          * @param t The Thread to add
80          */
81         void push_wait_list(Thread *t) { wait_list.push_back(t); }
82
83         /**
84          * Remove one Thread from the waiting list
85          * @return The Thread that was removed from the waiting list
86          */
87         Thread * pop_wait_list() {
88                 Thread *ret = wait_list.front();
89                 wait_list.pop_back();
90                 return ret;
91         }
92
93         friend void thread_startup();
94
95         SNAPSHOTALLOC
96 private:
97         int create_context();
98         Thread *parent;
99         ModelAction *creation;
100
101         void (*start_routine)(void *);
102         void *arg;
103         ucontext_t context;
104         void *stack;
105         thrd_t *user_thread;
106         thread_id_t id;
107         thread_state state;
108
109         /**
110          * A list of Threads waiting on this Thread. Particularly, this list is
111          * used for thread joins, where another Thread waits for this Thread to
112          * complete
113          */
114         std::vector<Thread *> wait_list;
115
116         /**
117          * The value returned by the last action in this thread
118          * @see Thread::set_return_value()
119          * @see Thread::get_return_value()
120          */
121         uint64_t last_action_val;
122 };
123
124 Thread * thread_current();
125
126 static inline thread_id_t thrd_to_id(thrd_t t)
127 {
128         return t;
129 }
130
131 static inline thread_id_t int_to_id(int i)
132 {
133         return i;
134 }
135
136 static inline int id_to_int(thread_id_t id)
137 {
138         return id;
139 }
140
141 #endif /* __THREADS_H__ */