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 */
26 * Thread has yielded to the model-checker but is ready to run. Used
27 * during an action that caused a context switch to the model-checking
32 * Thread is waiting on another action (e.g., thread completion, lock
36 /** Thread has completed its execution */
42 /** @brief A Thread is created for each user-space thread */
45 Thread(thrd_t *t, void (*func)(void *), void *a);
49 static int swap(ucontext_t *ctxt, Thread *t);
50 static int swap(Thread *t, ucontext_t *ctxt);
52 thread_state get_state() { return state; }
53 void set_state(thread_state s) { state = s; }
55 thrd_t get_thrd_t() { return *user_thread; }
56 Thread * get_parent() { return parent; }
58 void set_creation(ModelAction *act) { creation = act; }
59 ModelAction * get_creation() { return creation; }
62 * Set a return value for the last action in this thread (e.g., for an
64 * @param value The value to return
66 void set_return_value(uint64_t value) { last_action_val = value; }
69 * Retrieve a return value for the last action in this thread. Used,
70 * for instance, for an atomic read to return the 'read' value. Should
71 * be called from a user context.
72 * @return The value 'returned' by the action
74 uint64_t get_return_value() { return last_action_val; }
76 /** @return True if this thread is finished executing */
77 bool is_complete() { return state == THREAD_COMPLETED; }
79 /** @return True if this thread is blocked */
80 bool is_blocked() { return state == THREAD_BLOCKED; }
82 /** @return True if no threads are waiting on this Thread */
83 bool wait_list_empty() { return wait_list.empty(); }
86 * Add a thread to the waiting list for this thread.
87 * @param t The Thread to add
89 void push_wait_list(Thread *t) { wait_list.push_back(t); }
92 * Remove one Thread from the waiting list
93 * @return The Thread that was removed from the waiting list
95 Thread * pop_wait_list() {
96 Thread *ret = wait_list.front();
101 friend void thread_startup();
105 int create_context();
107 ModelAction *creation;
109 void (*start_routine)(void *);
118 * A list of Threads waiting on this Thread. Particularly, this list is
119 * used for thread joins, where another Thread waits for this Thread to
122 std::vector<Thread *> wait_list;
125 * The value returned by the last action in this thread
126 * @see Thread::set_return_value()
127 * @see Thread::get_return_value()
129 uint64_t last_action_val;
132 Thread * thread_current();
134 static inline thread_id_t thrd_to_id(thrd_t t)
139 static inline thread_id_t int_to_id(int i)
144 static inline int id_to_int(thread_id_t id)
149 #endif /* __THREADS_H__ */