2 * @brief Model Checker Thread class.
13 #include "libthreads.h"
15 typedef int thread_id_t;
17 #define THREAD_ID_T_NONE -1
19 /** @brief Represents the state of a user Thread */
20 typedef enum thread_state {
21 /** Thread was just created and hasn't run yet */
23 /** Thread is running */
25 /** Thread is not currently running but is ready to run */
28 * Thread is waiting on another action (e.g., thread completion, lock
32 /** Thread has completed its execution */
38 /** @brief A Thread is created for each user-space thread */
41 Thread(thrd_t *t, void (*func)(void *), void *a);
45 static int swap(ucontext_t *ctxt, Thread *t);
46 static int swap(Thread *t, ucontext_t *ctxt);
48 thread_state get_state() { return state; }
49 void set_state(thread_state s) { state = s; }
51 thrd_t get_thrd_t() { return *user_thread; }
52 Thread * get_parent() { return parent; }
54 void set_creation(ModelAction *act) { creation = act; }
55 ModelAction * get_creation() { return creation; }
58 * Set a return value for the last action in this thread (e.g., for an
60 * @param value The value to return
62 void set_return_value(uint64_t value) { last_action_val = value; }
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
70 uint64_t get_return_value() { return last_action_val; }
72 /** @return True if this thread is finished executing */
73 bool is_complete() { return state == THREAD_COMPLETED; }
75 /** @return True if this thread is blocked */
76 bool is_blocked() { return state == THREAD_BLOCKED; }
78 /** @return True if no threads are waiting on this Thread */
79 bool wait_list_empty() { return wait_list.empty(); }
82 * Add a ModelAction to the waiting list for this thread.
83 * @param t The ModelAction to add. Must be a JOIN.
85 void push_wait_list(ModelAction *act) { wait_list.push_back(act); }
88 * Remove one ModelAction from the waiting list
89 * @return The ModelAction that was removed from the waiting list
91 ModelAction * pop_wait_list() {
92 ModelAction *ret = wait_list.front();
97 friend void thread_startup();
101 int create_context();
103 ModelAction *creation;
105 void (*start_routine)(void *);
114 * A list of ModelActions waiting on this Thread. Particularly, this
115 * list is used for thread joins, where another Thread waits for this
118 std::vector<ModelAction *> wait_list;
121 * The value returned by the last action in this thread
122 * @see Thread::set_return_value()
123 * @see Thread::get_return_value()
125 uint64_t last_action_val;
128 Thread * thread_current();
130 static inline thread_id_t thrd_to_id(thrd_t t)
135 static inline thread_id_t int_to_id(int i)
140 static inline int id_to_int(thread_id_t id)
145 #endif /* __THREADS_H__ */