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