fixup more id vs. thread_id_t
[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 #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         ModelAction * get_pending() { return pending; }
85         void set_pending(ModelAction *act) { pending = act; }
86         /**
87          * Remove one ModelAction from the waiting list
88          * @return The ModelAction that was removed from the waiting list
89          */
90         ModelAction * pop_wait_list() {
91                 ModelAction *ret = wait_list.front();
92                 wait_list.pop_back();
93                 return ret;
94         }
95
96         friend void thread_startup();
97
98         SNAPSHOTALLOC
99 private:
100         int create_context();
101         Thread *parent;
102         ModelAction *creation;
103
104         ModelAction *pending;
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 ModelActions waiting on this Thread. Particularly, this
115          * list is used for thread joins, where another Thread waits for this
116          * Thread to complete
117          */
118         std::vector<ModelAction *> 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__ */