1 /** @file threads-model.h
2 * @brief Model Checker Thread class.
5 #ifndef __THREADS_MODEL_H__
6 #define __THREADS_MODEL_H__
12 #include "modeltypes.h"
13 #include "stl-model.h"
15 #include "classlist.h"
17 struct thread_params {
22 /** @brief Represents the state of a user Thread */
23 typedef enum thread_state {
24 /** Thread was just created and hasn't run yet */
26 /** Thread is running */
28 /** Thread is not currently running but is ready to run */
31 * Thread is waiting on another action (e.g., thread completion, lock
35 /** Thread has completed its execution */
40 /** @brief A Thread is created for each user-space thread */
43 Thread(thread_id_t tid);
44 Thread(thread_id_t tid, thrd_t *t, void (*func)(void *), void *a, Thread *parent);
45 Thread(thread_id_t tid, thrd_t *t, void *(*func)(void *), void *a, Thread *parent);
50 static int swap(ucontext_t *ctxt, Thread *t);
51 static int swap(Thread *t, ucontext_t *ctxt);
54 thread_state get_state() const { return state; }
55 void set_state(thread_state s);
56 thread_id_t get_id() const;
57 thrd_t get_thrd_t() const { return *user_thread; }
58 Thread * get_parent() const { return parent; }
60 void set_creation(ModelAction *act) { creation = act; }
61 ModelAction * get_creation() const { return creation; }
64 * Set a return value for the last action in this thread (e.g., for an
66 * @param value The value to return
68 void set_return_value(uint64_t value) { last_action_val = value; }
71 * Retrieve a return value for the last action in this thread. Used,
72 * for instance, for an atomic read to return the 'read' value. Should
73 * be called from a user context.
74 * @return The value 'returned' by the action
76 uint64_t get_return_value() const { return last_action_val; }
78 /** @set and get the return value from pthread functions */
79 void set_pthread_return(void *ret) { pthread_return = ret; }
80 void * get_pthread_return() { return pthread_return; }
82 /** @return True if this thread is finished executing */
83 bool is_complete() const { return state == THREAD_COMPLETED; }
85 /** @return True if this thread is blocked */
86 bool is_blocked() const { return state == THREAD_BLOCKED; }
88 /** @return The pending (next) ModelAction for this Thread
89 * @see Thread::pending */
90 ModelAction * get_pending() const { return pending; }
92 /** @brief Set the pending (next) ModelAction for this Thread
93 * @param act The pending ModelAction
94 * @see Thread::pending */
95 void set_pending(ModelAction *act) { pending = act; }
97 Thread * waiting_on() const;
98 bool is_waiting_on(const Thread *t) const;
100 bool is_model_thread() const { return model_thread; }
102 friend void thread_startup();
105 * Intentionally NOT allocated with MODELALLOC or SNAPSHOTALLOC.
106 * Threads should be allocated on the user's normal (snapshotting) heap
107 * to allow their allocation/deallocation to follow the same pattern as
108 * the rest of the backtracked/replayed program.
110 void * operator new(size_t size) {
111 return Thread_malloc(size);
113 void operator delete(void *p, size_t size) {
116 void * operator new[](size_t size) {
117 return Thread_malloc(size);
119 void operator delete[](void *p, size_t size) {
123 int create_context();
125 /** @brief The parent Thread which created this Thread */
126 Thread * const parent;
128 /** @brief The THREAD_CREATE ModelAction which created this Thread */
129 ModelAction *creation;
132 * @brief The next ModelAction to be run by this Thread
134 * This action should be kept updated by the ModelChecker, so that we
135 * always know what the next ModelAction's memory_order, action type,
138 ModelAction *pending;
140 void (*start_routine)(void *);
141 void *(*pstart_routine)(void *);
151 * The value returned by the last action in this thread
152 * @see Thread::set_return_value()
153 * @see Thread::get_return_value()
155 uint64_t last_action_val;
157 /** the value return from pthread functions */
158 void * pthread_return;
160 /** @brief Is this Thread a special model-checker thread? */
161 const bool model_thread;
164 Thread * thread_current();
166 static inline thread_id_t thrd_to_id(thrd_t t)
168 return t.priv->get_id();
172 * @brief Map a zero-based integer index to a unique thread ID
174 * This is the inverse of id_to_int
176 static inline thread_id_t int_to_id(int i)
182 * @brief Map a unique thread ID to a zero-based integer index
184 * This is the inverse of int_to_id
186 static inline int id_to_int(thread_id_t id)
191 #endif /* __THREADS_MODEL_H__ */