1 /** @file threads-model.h
2 * @brief Model Checker Thread class.
5 #ifndef __THREADS_MODEL_H__
6 #define __THREADS_MODEL_H__
14 #include "modeltypes.h"
16 /** @brief Represents the state of a user Thread */
17 typedef enum thread_state {
18 /** Thread was just created and hasn't run yet */
20 /** Thread is running */
22 /** Thread is not currently running but is ready to run */
25 * Thread is waiting on another action (e.g., thread completion, lock
29 /** Thread has completed its execution */
35 /** @brief A Thread is created for each user-space thread */
38 Thread(thread_id_t tid);
39 Thread(thrd_t *t, void (*func)(void *), void *a);
43 static int swap(ucontext_t *ctxt, Thread *t);
44 static int swap(Thread *t, ucontext_t *ctxt);
46 thread_state get_state() { return state; }
47 void set_state(thread_state s) { state = s; }
49 thrd_t get_thrd_t() { return *user_thread; }
50 Thread * get_parent() { return parent; }
52 void set_creation(ModelAction *act) { creation = act; }
53 ModelAction * get_creation() { return creation; }
56 * Set a return value for the last action in this thread (e.g., for an
58 * @param value The value to return
60 void set_return_value(uint64_t value) { last_action_val = value; }
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
68 uint64_t get_return_value() { return last_action_val; }
70 /** @return True if this thread is finished executing */
71 bool is_complete() { return state == THREAD_COMPLETED; }
73 /** @return True if this thread is blocked */
74 bool is_blocked() { return state == THREAD_BLOCKED; }
76 /** @return True if no threads are waiting on this Thread */
77 bool wait_list_empty() { return wait_list.empty(); }
80 * Add a ModelAction to the waiting list for this thread.
81 * @param t The ModelAction to add. Must be a JOIN.
83 void push_wait_list(ModelAction *act) { wait_list.push_back(act); }
85 unsigned int num_wait_list() {
86 return wait_list.size();
89 ModelAction * get_waiter(unsigned int i) {
93 ModelAction * get_pending() { return pending; }
94 void set_pending(ModelAction *act) { pending = act; }
96 * Remove one ModelAction from the waiting list
97 * @return The ModelAction that was removed from the waiting list
99 ModelAction * pop_wait_list() {
100 ModelAction *ret = wait_list.front();
101 wait_list.pop_back();
105 bool is_model_thread() { return model_thread; }
107 friend void thread_startup();
110 * Intentionally NOT allocated with MODELALLOC or SNAPSHOTALLOC.
111 * Threads should be allocated on the user's normal (snapshotting) heap
112 * to allow their allocation/deallocation to follow the same pattern as
113 * the rest of the backtracked/replayed program.
116 int create_context();
118 ModelAction *creation;
120 ModelAction *pending;
121 void (*start_routine)(void *);
130 * A list of ModelActions waiting on this Thread. Particularly, this
131 * list is used for thread joins, where another Thread waits for this
134 std::vector< ModelAction *, SnapshotAlloc<ModelAction *> > wait_list;
137 * The value returned by the last action in this thread
138 * @see Thread::set_return_value()
139 * @see Thread::get_return_value()
141 uint64_t last_action_val;
143 /** @brief Is this Thread a special model-checker thread? */
144 const bool model_thread;
147 Thread * thread_current();
149 static inline thread_id_t thrd_to_id(thrd_t t)
155 * @brief Map a zero-based integer index to a unique thread ID
157 * This is the inverse of id_to_int
159 static inline thread_id_t int_to_id(int i)
165 * @brief Map a unique thread ID to a zero-based integer index
167 * This is the inverse of int_to_id
169 static inline int id_to_int(thread_id_t id)
174 #endif /* __THREADS_MODEL_H__ */