2 * @brief Model Checker Thread class.
13 #include "libthreads.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(thrd_t *t, void (*func)(void *), void *a);
42 static int swap(ucontext_t *ctxt, Thread *t);
43 static int swap(Thread *t, ucontext_t *ctxt);
45 thread_state get_state() { return state; }
46 void set_state(thread_state s) { state = s; }
48 thrd_t get_thrd_t() { return *user_thread; }
49 Thread * get_parent() { return parent; }
51 void set_creation(ModelAction *act) { creation = act; }
52 ModelAction * get_creation() { return creation; }
55 * Set a return value for the last action in this thread (e.g., for an
57 * @param value The value to return
59 void set_return_value(uint64_t value) { last_action_val = value; }
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
67 uint64_t get_return_value() { return last_action_val; }
69 /** @return True if this thread is finished executing */
70 bool is_complete() { return state == THREAD_COMPLETED; }
72 /** @return True if this thread is blocked */
73 bool is_blocked() { return state == THREAD_BLOCKED; }
75 /** @return True if no threads are waiting on this Thread */
76 bool wait_list_empty() { return wait_list.empty(); }
79 * Add a ModelAction to the waiting list for this thread.
80 * @param t The ModelAction to add. Must be a JOIN.
82 void push_wait_list(ModelAction *act) { wait_list.push_back(act); }
84 ModelAction * get_pending() { return pending; }
85 void set_pending(ModelAction *act) { pending = act; }
87 * Remove one ModelAction from the waiting list
88 * @return The ModelAction that was removed from the waiting list
90 ModelAction * pop_wait_list() {
91 ModelAction *ret = wait_list.front();
96 friend void thread_startup();
100 int create_context();
102 ModelAction *creation;
104 ModelAction *pending;
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__ */