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 struct thread_params {
21 /** @brief Represents the state of a user Thread */
22 typedef enum thread_state {
23 /** Thread was just created and hasn't run yet */
25 /** Thread is running */
27 /** Thread is not currently running but is ready to run */
30 * Thread is waiting on another action (e.g., thread completion, lock
34 /** Thread has completed its execution */
40 /** @brief A Thread is created for each user-space thread */
43 Thread(thread_id_t tid);
44 Thread(thrd_t *t, void (*func)(void *), void *a);
48 static int swap(ucontext_t *ctxt, Thread *t);
49 static int swap(Thread *t, ucontext_t *ctxt);
51 thread_state get_state() const { return state; }
52 void set_state(thread_state s);
53 thread_id_t get_id() const;
54 thrd_t get_thrd_t() const { return *user_thread; }
55 Thread * get_parent() const { return parent; }
57 void set_creation(ModelAction *act) { creation = act; }
58 ModelAction * get_creation() const { return creation; }
61 * Set a return value for the last action in this thread (e.g., for an
63 * @param value The value to return
65 void set_return_value(uint64_t value) { last_action_val = value; }
68 * Retrieve a return value for the last action in this thread. Used,
69 * for instance, for an atomic read to return the 'read' value. Should
70 * be called from a user context.
71 * @return The value 'returned' by the action
73 uint64_t get_return_value() const { return last_action_val; }
75 /** @return True if this thread is finished executing */
76 bool is_complete() const { return state == THREAD_COMPLETED; }
78 /** @return True if this thread is blocked */
79 bool is_blocked() const { return state == THREAD_BLOCKED; }
81 /** @return True if no threads are waiting on this Thread */
82 bool wait_list_empty() const { return wait_list.empty(); }
85 * Add a ModelAction to the waiting list for this thread.
86 * @param t The ModelAction to add. Must be a JOIN.
88 void push_wait_list(ModelAction *act) { wait_list.push_back(act); }
90 unsigned int num_wait_list() const {
91 return wait_list.size();
94 ModelAction * get_waiter(unsigned int i) const {
98 ModelAction * get_pending() const { return pending; }
99 void set_pending(ModelAction *act) { pending = act; }
101 * Remove one ModelAction from the waiting list
102 * @return The ModelAction that was removed from the waiting list
104 ModelAction * pop_wait_list() {
105 ModelAction *ret = wait_list.front();
106 wait_list.pop_back();
110 bool is_model_thread() const { return model_thread; }
112 friend void thread_startup();
115 * Intentionally NOT allocated with MODELALLOC or SNAPSHOTALLOC.
116 * Threads should be allocated on the user's normal (snapshotting) heap
117 * to allow their allocation/deallocation to follow the same pattern as
118 * the rest of the backtracked/replayed program.
121 int create_context();
123 ModelAction *creation;
125 ModelAction *pending;
126 void (*start_routine)(void *);
135 * A list of ModelActions waiting on this Thread. Particularly, this
136 * list is used for thread joins, where another Thread waits for this
139 std::vector< ModelAction *, SnapshotAlloc<ModelAction *> > wait_list;
142 * The value returned by the last action in this thread
143 * @see Thread::set_return_value()
144 * @see Thread::get_return_value()
146 uint64_t last_action_val;
148 /** @brief Is this Thread a special model-checker thread? */
149 const bool model_thread;
152 Thread * thread_current();
154 static inline thread_id_t thrd_to_id(thrd_t t)
156 return t.priv->get_id();
160 * @brief Map a zero-based integer index to a unique thread ID
162 * This is the inverse of id_to_int
164 static inline thread_id_t int_to_id(int i)
170 * @brief Map a unique thread ID to a zero-based integer index
172 * This is the inverse of int_to_id
174 static inline int id_to_int(thread_id_t id)
179 #endif /* __THREADS_MODEL_H__ */