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
31 /** Thread has completed its execution */
37 /** @brief A Thread is created for each user-space thread */
40 Thread(thrd_t *t, void (*func)(void *), void *a);
44 static int swap(ucontext_t *ctxt, Thread *t);
45 static int swap(Thread *t, ucontext_t *ctxt);
47 thread_state get_state() { return state; }
48 void set_state(thread_state s) { state = s; }
50 thrd_t get_thrd_t() { return *user_thread; }
51 Thread * get_parent() { return parent; }
53 void set_creation(ModelAction *act) { creation = act; }
54 ModelAction * get_creation() { return creation; }
57 * Set a return value for the last action in this thread (e.g., for an
59 * @param value The value to return
61 void set_return_value(uint64_t value) { last_action_val = value; }
64 * Retrieve a return value for the last action in this thread. Used,
65 * for instance, for an atomic read to return the 'read' value. Should
66 * be called from a user context.
67 * @return The value 'returned' by the action
69 uint64_t get_return_value() { return last_action_val; }
71 /** @return True if this thread is finished executing */
72 bool is_complete() { return state == THREAD_COMPLETED; }
74 /** @return True if no threads are waiting on this Thread */
75 bool wait_list_empty() { return wait_list.empty(); }
78 * Add a thread to the waiting list for this thread.
79 * @param t The Thread to add
81 void push_wait_list(Thread *t) { wait_list.push_back(t); }
84 * Remove one Thread from the waiting list
85 * @return The Thread that was removed from the waiting list
87 Thread * pop_wait_list() {
88 Thread *ret = wait_list.front();
93 friend void thread_startup();
99 ModelAction *creation;
101 void (*start_routine)(void *);
110 * A list of Threads waiting on this Thread. Particularly, this list is
111 * used for thread joins, where another Thread waits for this Thread to
114 std::vector<Thread *> wait_list;
117 * The value returned by the last action in this thread
118 * @see Thread::set_return_value()
119 * @see Thread::get_return_value()
121 uint64_t last_action_val;
124 Thread * thread_current();
126 static inline thread_id_t thrd_to_id(thrd_t t)
131 static inline thread_id_t int_to_id(int i)
136 static inline int id_to_int(thread_id_t id)
141 #endif /* __THREADS_H__ */