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;
28 using std::volatile_order;
31 * @brief A recognizable don't-care value for use in the ModelAction::value
34 * Note that this value can be legitimately used by a program, and hence by
35 * iteself does not indicate no value.
37 #define VALUE_NONE 0xdeadbeef
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 PTHREAD_CREATE, // < A pthread creation action
57 PTHREAD_JOIN, // < A pthread join action
58 ATOMIC_UNINIT, // < Represents an uninitialized atomic
59 NONATOMIC_WRITE, // < Represents a non-atomic store
60 ATOMIC_INIT, // < Initialization of an atomic object (e.g., atomic_init())
61 ATOMIC_WRITE, // < An atomic write action
62 ATOMIC_RMW, // < The write part of an atomic RMW action
63 ATOMIC_READ, // < An atomic read action
64 ATOMIC_RMWR, // < The read part of an atomic RMW action
65 ATOMIC_RMWRCAS, // < The read part of an atomic RMW action
66 ATOMIC_RMWC, // < Convert an atomic RMW action into a READ
68 ATOMIC_FENCE, // < A fence action
69 ATOMIC_LOCK, // < A lock action
70 ATOMIC_TRYLOCK, // < A trylock action
71 ATOMIC_UNLOCK, // < An unlock action
72 ATOMIC_NOTIFY_ONE, // < A notify_one action
73 ATOMIC_NOTIFY_ALL, // < A notify all action
74 ATOMIC_WAIT, // < A wait action
75 ATOMIC_ANNOTATION, // < An annotation action to pass information to a trace analysis
78 NOOP // no operation, which returns control to scheduler
83 * @brief Represents a single atomic action
85 * A ModelAction is always allocated as non-snapshotting, because it is used in
86 * multiple executions during backtracking. Except for fake uninitialized
87 * (ATOMIC_UNINIT) ModelActions, each action is assigned a unique sequence
92 ModelAction(action_type_t type, memory_order order, void *loc, uint64_t value = VALUE_NONE, Thread *thread = NULL);
93 ModelAction(action_type_t type, memory_order order, void *loc, uint64_t value, int size);
94 ModelAction(action_type_t type, const char * position, memory_order order, void *loc, uint64_t value, int size);
95 ModelAction(action_type_t type, const char * position, memory_order order, void *loc, uint64_t value = VALUE_NONE, Thread *thread = NULL);
99 thread_id_t get_tid() const { return tid; }
100 action_type get_type() const { return type; }
101 memory_order get_mo() const { return order; }
102 memory_order get_original_mo() const { return original_order; }
103 void set_mo(memory_order order) { this->order = order; }
104 void * get_location() const { return location; }
105 const char * get_position() const { return position; }
106 modelclock_t get_seq_number() const { return seq_number; }
107 uint64_t get_value() const { return value; }
108 uint64_t get_reads_from_value() const;
109 uint64_t get_write_value() const;
110 uint64_t get_return_value() const;
111 ModelAction * get_reads_from() const { return reads_from; }
112 cdsc::mutex * get_mutex() const;
114 void set_read_from(ModelAction *act);
116 /** Store the most recent fence-release from the same thread
117 * @param fence The fence-release that occured prior to this */
118 void set_last_fence_release(const ModelAction *fence) { last_fence_release = fence; }
119 /** @return The most recent fence-release from the same thread */
120 const ModelAction * get_last_fence_release() const { return last_fence_release; }
122 void copy_from_new(ModelAction *newaction);
123 void set_seq_number(modelclock_t num);
124 void set_try_lock(bool obtainedlock);
125 bool is_thread_start() const;
126 bool is_thread_join() const;
127 bool is_mutex_op() const;
128 bool is_lock() const;
129 bool is_trylock() const;
130 bool is_unlock() const;
131 bool is_wait() const;
132 bool is_notify() const;
133 bool is_notify_one() const;
134 bool is_success_lock() const;
135 bool is_failed_trylock() const;
136 bool is_atomic_var() const;
137 bool is_uninitialized() const;
138 bool is_read() const;
139 bool is_write() const;
140 bool is_yield() const;
141 bool could_be_write() const;
142 bool is_rmwr() const;
143 bool is_rmwrcas() const;
144 bool is_rmwc() const;
146 bool is_fence() const;
147 bool is_initialization() const;
148 bool is_annotation() const;
149 bool is_relaxed() const;
150 bool is_acquire() const;
151 bool is_release() const;
152 bool is_seqcst() const;
153 bool same_var(const ModelAction *act) const;
154 bool same_thread(const ModelAction *act) const;
155 bool is_conflicting_lock(const ModelAction *act) const;
156 bool could_synchronize_with(const ModelAction *act) const;
158 Thread * get_thread_operand() const;
159 void create_cv(const ModelAction *parent = NULL);
160 ClockVector * get_cv() const { return cv; }
161 ClockVector * get_rfcv() const { return rf_cv; }
162 void set_rfcv(ClockVector * rfcv) { rf_cv = rfcv; }
163 bool synchronize_with(const ModelAction *act);
165 bool has_synchronized_with(const ModelAction *act) const;
166 bool happens_before(const ModelAction *act) const;
168 inline bool operator <(const ModelAction& act) const {
169 return get_seq_number() < act.get_seq_number();
171 inline bool operator >(const ModelAction& act) const {
172 return get_seq_number() > act.get_seq_number();
175 void process_rmw(ModelAction * act);
176 void copy_typeandorder(ModelAction * act);
177 unsigned int hash() const;
178 bool equals(const ModelAction *x) const { return this == x; }
179 void set_value(uint64_t val) { value = val; }
181 /* to accomodate pthread create and join */
182 Thread * thread_operand;
183 void set_thread_operand(Thread *th) { thread_operand = th; }
184 void set_uninit_action(ModelAction *act) { uninitaction = act; }
185 ModelAction * get_uninit_action() { return uninitaction; }
188 const char * get_type_str() const;
189 const char * get_mo_str() const;
191 /** @brief A pointer to the memory location for this action. */
194 /** @brief A pointer to the source line for this atomic action. */
195 const char * position;
199 * @brief The store that this action reads from
201 * Only valid for reads
203 ModelAction *reads_from;
207 /** @brief The last fence release from the same thread */
208 const ModelAction *last_fence_release;
209 ModelAction * uninitaction;
212 * @brief The clock vector for this operation
214 * Technically, this is only needed for potentially synchronizing
215 * (e.g., non-relaxed) operations, but it is very handy to have these
216 * vectors for all operations.
221 /** @brief The value written (for write or RMW; undefined for read) */
224 /** @brief Type of action (read, write, RMW, fence, thread create, etc.) */
227 /** @brief The memory order for this operation. */
230 /** @brief The original memory order parameter for this operation. */
231 memory_order original_order;
233 /** @brief The thread id that performed this action. */
237 * @brief The sequence number of this action
239 * Except for ATOMIC_UNINIT actions, this number should be unique and
240 * should represent the action's position in the execution order.
242 modelclock_t seq_number;
245 #endif /* __ACTION_H__ */