2 * @brief Models actions taken by threads.
12 #include "memoryorder.h"
13 #include "modeltypes.h"
14 #include "mypthread.h"
15 #include "classlist.h"
21 using std::memory_order;
22 using std::memory_order_relaxed;
23 using std::memory_order_consume;
24 using std::memory_order_acquire;
25 using std::memory_order_release;
26 using std::memory_order_acq_rel;
27 using std::memory_order_seq_cst;
30 * @brief A recognizable don't-care value for use in the ModelAction::value
33 * Note that this value can be legitimately used by a program, and hence by
34 * iteself does not indicate no value.
36 #define VALUE_NONE 0xdeadbeef
37 #define WRITE_REFERENCED ((void *)0x1)
40 * @brief The "location" at which a fence occurs
42 * We need a non-zero memory location to associate with fences, since our hash
43 * tables don't handle NULL-pointer keys. HACK: Hopefully this doesn't collide
44 * with any legitimate memory locations.
46 #define FENCE_LOCATION ((void *)0x7)
48 /** @brief Represents an action type, identifying one of several types of
50 typedef enum action_type {
51 THREAD_CREATE, // < A thread creation action
52 THREAD_START, // < First action in each thread
53 THREAD_YIELD, // < A thread yield action
54 THREAD_JOIN, // < A thread join action
55 THREAD_FINISH, // < A thread completion action
56 THREADONLY_FINISH, // < A thread completion action
57 THREAD_SLEEP, // < A sleep operation
59 PTHREAD_CREATE, // < A pthread creation action
60 PTHREAD_JOIN, // < A pthread join action
62 NONATOMIC_WRITE, // < Represents a non-atomic store
63 ATOMIC_INIT, // < Initialization of an atomic object (e.g., atomic_init())
64 ATOMIC_WRITE, // < An atomic write action
65 ATOMIC_RMW, // < The write part of an atomic RMW action
66 ATOMIC_READ, // < An atomic read action
67 ATOMIC_RMWR, // < The read part of an atomic RMW action
68 ATOMIC_RMWRCAS, // < The read part of an atomic RMW action
69 ATOMIC_RMWC, // < Convert an atomic RMW action into a READ
71 ATOMIC_FENCE, // < A fence action
72 ATOMIC_LOCK, // < A lock action
73 ATOMIC_TRYLOCK, // < A trylock action
74 ATOMIC_UNLOCK, // < An unlock action
76 ATOMIC_NOTIFY_ONE, // < A notify_one action
77 ATOMIC_NOTIFY_ALL, // < A notify all action
78 ATOMIC_WAIT, // < A wait action
79 ATOMIC_TIMEDWAIT, // < A timed wait action
80 ATOMIC_ANNOTATION, // < An annotation action to pass information to a trace analysis
81 READY_FREE, // < Write is ready to be freed
82 ATOMIC_NOP // < Placeholder
87 * @brief Represents a single atomic action
89 * A ModelAction is always allocated as non-snapshotting, because it is used in
90 * multiple executions during backtracking. Except for non-atomic write
91 * ModelActions, each action is assigned a unique sequence
96 ModelAction(action_type_t type, memory_order order, void *loc, uint64_t value = VALUE_NONE, Thread *thread = NULL);
97 ModelAction(action_type_t type, memory_order order, void *loc, uint64_t value, int size);
98 ModelAction(action_type_t type, const char * position, memory_order order, void *loc, uint64_t value, int size);
99 ModelAction(action_type_t type, memory_order order, uint64_t value, uint64_t time);
100 ModelAction(action_type_t type, const char * position, memory_order order, void *loc, uint64_t value = VALUE_NONE, Thread *thread = NULL);
104 thread_id_t get_tid() const { return tid; }
105 action_type get_type() const { return type; }
106 void set_type(action_type _type) { type = _type; }
107 action_type get_original_type() const { return original_type; }
108 void set_original_type(action_type _type) { original_type = _type; }
109 void set_free() { type = READY_FREE; }
110 memory_order get_mo() const { return order; }
111 memory_order get_original_mo() const { return original_order; }
112 void set_mo(memory_order order) { this->order = order; }
113 void * get_location() const { return location; }
114 const char * get_position() const { return position; }
115 modelclock_t get_seq_number() const { return seq_number; }
116 uint64_t get_value() const { return value; }
117 uint64_t get_reads_from_value() const;
118 uint64_t get_write_value() const;
119 uint64_t get_return_value() const;
120 ModelAction * get_reads_from() const { return reads_from; }
121 uint64_t get_time() const {return time;}
122 cdsc::mutex * get_mutex() const;
123 bool get_swap_flag() const { return swap_flag; }
125 void set_read_from(ModelAction *act);
127 /** Store the most recent fence-release from the same thread
128 * @param fence The fence-release that occured prior to this */
129 void set_last_fence_release(const ModelAction *fence) { last_fence_release = fence; }
130 /** @return The most recent fence-release from the same thread */
131 const ModelAction * get_last_fence_release() const { return last_fence_release; }
133 void copy_from_new(ModelAction *newaction);
134 void set_seq_number(modelclock_t num);
135 void reset_seq_number();
136 void set_try_lock(bool obtainedlock);
137 bool is_thread_start() const;
138 bool is_thread_join() const;
139 bool is_mutex_op() const;
140 bool is_lock() const;
141 bool is_sleep() const;
142 bool is_trylock() const;
143 bool is_unlock() const;
144 bool is_wait() const;
145 bool is_create() const;
146 bool is_notify() const;
147 bool is_notify_one() const;
148 bool is_success_lock() const;
149 bool is_failed_trylock() const;
150 bool is_atomic_var() const;
151 bool is_read() const;
152 bool is_write() const;
153 bool is_free() const;
154 bool is_yield() const;
155 bool could_be_write() const;
156 bool is_rmwr() const;
157 bool is_rmwrcas() const;
158 bool is_rmwc() const;
160 bool is_fence() const;
161 bool is_initialization() const;
162 bool is_annotation() const;
163 bool is_relaxed() const;
164 bool is_acquire() const;
165 bool is_release() const;
166 bool is_seqcst() const;
167 bool same_var(const ModelAction *act) const;
168 bool same_thread(const ModelAction *act) const;
169 bool is_conflicting_lock(const ModelAction *act) const;
170 bool could_synchronize_with(const ModelAction *act) const;
172 Thread * get_thread_operand() const;
173 void create_cv(const ModelAction *parent = NULL);
174 ClockVector * get_cv() const { return cv; }
175 ClockVector * get_rfcv() const { return rf_cv; }
176 void set_rfcv(ClockVector * rfcv) { rf_cv = rfcv; }
177 bool synchronize_with(const ModelAction *act);
179 bool has_synchronized_with(const ModelAction *act) const;
180 bool happens_before(const ModelAction *act) const;
182 inline bool operator <(const ModelAction& act) const {
183 return get_seq_number() < act.get_seq_number();
185 inline bool operator >(const ModelAction& act) const {
186 return get_seq_number() > act.get_seq_number();
189 void process_rmw(ModelAction * act);
190 void copy_typeandorder(ModelAction * act);
191 unsigned int hash() const;
192 bool equals(const ModelAction *x) const { return this == x; }
193 void set_value(uint64_t val) { value = val; }
195 void use_original_type();
197 /* to accomodate pthread create and join */
198 Thread * thread_operand;
199 void set_thread_operand(Thread *th) { thread_operand = th; }
200 void setTraceRef(sllnode<ModelAction *> *ref) { trace_ref = ref; }
201 void setThrdMapRef(sllnode<ModelAction *> *ref) { thrdmap_ref = ref; }
202 void setActionRef(sllnode<ModelAction *> *ref) { action_ref = ref; }
203 sllnode<ModelAction *> * getTraceRef() { return trace_ref; }
204 sllnode<ModelAction *> * getThrdMapRef() { return thrdmap_ref; }
205 sllnode<ModelAction *> * getActionRef() { return action_ref; }
207 void incr_func_ref_count() { func_ref_count++; }
208 void decr_func_ref_count() { if (func_ref_count > 0) func_ref_count--; }
209 uint32_t get_func_ref_count() { return func_ref_count; }
213 const char * get_type_str() const;
214 const char * get_mo_str() const;
216 /** @brief A pointer to the memory location for this action. */
219 /** @brief A pointer to the source line for this atomic action. */
220 const char * position;
224 * @brief The store that this action reads from
226 * Only valid for reads
228 ModelAction *reads_from;
230 uint64_t time; //used for sleep
233 /** @brief The last fence release from the same thread */
234 const ModelAction *last_fence_release;
237 * @brief The clock vector for this operation
239 * Technically, this is only needed for potentially synchronizing
240 * (e.g., non-relaxed) operations, but it is very handy to have these
241 * vectors for all operations.
245 sllnode<ModelAction *> * trace_ref;
246 sllnode<ModelAction *> * thrdmap_ref;
247 sllnode<ModelAction *> * action_ref;
249 /** @brief Number of read actions that are reading from this store */
250 uint32_t func_ref_count;
252 /** @brief The value written (for write or RMW; undefined for read) */
255 /** @brief Type of action (read, write, RMW, fence, thread create, etc.) */
258 /** @brief The original type of action (read, write, RMW) before it was
259 * set as READY_FREE or weaken from a RMW to a write */
260 action_type original_type;
262 /** @brief Indicate whether the action type and the original action type
263 * has been swapped. */
266 /** @brief The memory order for this operation. */
269 /** @brief The original memory order parameter for this operation. */
270 memory_order original_order;
272 /** @brief The thread id that performed this action. */
276 * @brief The sequence number of this action
278 * Except for non atomic write actions, this number should be unique and
279 * should represent the action's position in the execution order.
281 modelclock_t seq_number;
284 #endif /* __ACTION_H__ */