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