2 * @brief Models actions taken by threads.
12 #include "memoryorder.h"
13 #include "modeltypes.h"
16 /* Forward declarations */
24 using std::memory_order;
25 using std::memory_order_relaxed;
26 using std::memory_order_consume;
27 using std::memory_order_acquire;
28 using std::memory_order_release;
29 using std::memory_order_acq_rel;
30 using std::memory_order_seq_cst;
33 * @brief A recognizable don't-care value for use in the ModelAction::value
36 * Note that this value can be legitimately used by a program, and hence by
37 * iteself does not indicate no value.
39 #define VALUE_NONE 0xdeadbeef
42 * @brief The "location" at which a fence occurs
44 * We need a non-zero memory location to associate with fences, since our hash
45 * tables don't handle NULL-pointer keys. HACK: Hopefully this doesn't collide
46 * with any legitimate memory locations.
48 #define FENCE_LOCATION ((void *)0x7)
50 /** @brief Represents an action type, identifying one of several types of
52 typedef enum action_type {
53 MODEL_FIXUP_RELSEQ, /**< Special ModelAction: finalize a release
55 THREAD_CREATE, /**< A thread creation action */
56 THREAD_START, /**< First action in each thread */
57 THREAD_YIELD, /**< A thread yield action */
58 THREAD_JOIN, /**< A thread join action */
59 THREAD_FINISH, /**< A thread completion action */
60 PTHREAD_CREATE, /**< A pthread creation action */
61 PTHREAD_JOIN, /**< A pthread join action */
63 ATOMIC_UNINIT, /**< Represents an uninitialized atomic */
64 ATOMIC_READ, /**< An atomic read action */
65 ATOMIC_WRITE, /**< An atomic write action */
66 ATOMIC_RMWR, /**< The read part of an atomic RMW action */
67 ATOMIC_RMW, /**< The write part of an atomic RMW action */
68 ATOMIC_RMWC, /**< Convert an atomic RMW action into a READ */
69 ATOMIC_INIT, /**< Initialization of an atomic object (e.g.,
71 ATOMIC_FENCE, /**< A fence action */
72 ATOMIC_LOCK, /**< A lock action */
73 ATOMIC_TRYLOCK, /**< A trylock action */
74 ATOMIC_UNLOCK, /**< An unlock action */
75 ATOMIC_NOTIFY_ONE, /**< A notify_one action */
76 ATOMIC_NOTIFY_ALL, /**< A notify all action */
77 ATOMIC_WAIT, /**< A wait action */
78 ATOMIC_ANNOTATION, /**< An annotation action to pass information
79 to a trace analysis */
83 /* Forward declaration */
88 * @brief Represents a single atomic action
90 * A ModelAction is always allocated as non-snapshotting, because it is used in
91 * multiple executions during backtracking. Except for fake uninitialized
92 * (ATOMIC_UNINIT) ModelActions, each action is assigned a unique sequence
97 ModelAction(action_type_t type, memory_order order, void *loc, uint64_t value = VALUE_NONE, Thread *thread = NULL);
101 thread_id_t get_tid() const { return tid; }
102 action_type get_type() const { return type; }
103 memory_order get_mo() const { return order; }
104 memory_order get_original_mo() const { return original_order; }
105 void set_mo(memory_order order) { this->order = order; }
106 void * get_location() const { return location; }
107 modelclock_t get_seq_number() const { return seq_number; }
108 uint64_t get_value() const { return value; }
109 uint64_t get_reads_from_value() const;
110 uint64_t get_write_value() const;
111 uint64_t get_return_value() const;
112 const ModelAction * get_reads_from() const { return reads_from; }
113 cdsc::mutex * get_mutex() const;
115 Node * get_node() const;
116 void set_node(Node *n) { node = n; }
118 void set_read_from(const ModelAction *act);
120 /** Store the most recent fence-release from the same thread
121 * @param fence The fence-release that occured prior to this */
122 void set_last_fence_release(const ModelAction *fence) { last_fence_release = fence; }
123 /** @return The most recent fence-release from the same thread */
124 const ModelAction * get_last_fence_release() const { return last_fence_release; }
126 void copy_from_new(ModelAction *newaction);
127 void set_seq_number(modelclock_t num);
128 void set_try_lock(bool obtainedlock);
129 bool is_thread_start() const;
130 bool is_thread_join() const;
131 bool is_relseq_fixup() const;
132 bool is_mutex_op() const;
133 bool is_lock() const;
134 bool is_trylock() const;
135 bool is_unlock() const;
136 bool is_wait() const;
137 bool is_notify() const;
138 bool is_notify_one() const;
139 bool is_success_lock() const;
140 bool is_failed_trylock() const;
141 bool is_atomic_var() const;
142 bool is_uninitialized() const;
143 bool is_read() const;
144 bool is_write() const;
145 bool is_yield() const;
146 bool could_be_write() const;
147 bool is_rmwr() const;
148 bool is_rmwc() const;
150 bool is_fence() const;
151 bool is_initialization() const;
152 bool is_annotation() const;
153 bool is_relaxed() const;
154 bool is_acquire() const;
155 bool is_release() const;
156 bool is_seqcst() const;
157 bool same_var(const ModelAction *act) const;
158 bool same_thread(const ModelAction *act) const;
159 bool is_conflicting_lock(const ModelAction *act) const;
160 bool could_synchronize_with(const ModelAction *act) const;
162 Thread * get_thread_operand() const;
164 void create_cv(const ModelAction *parent = NULL);
165 ClockVector * get_cv() const { return cv; }
166 bool synchronize_with(const ModelAction *act);
168 bool has_synchronized_with(const ModelAction *act) const;
169 bool happens_before(const ModelAction *act) const;
171 inline bool operator <(const ModelAction& act) const {
172 return get_seq_number() < act.get_seq_number();
174 inline bool operator >(const ModelAction& act) const {
175 return get_seq_number() > act.get_seq_number();
178 void process_rmw(ModelAction * act);
179 void copy_typeandorder(ModelAction * act);
181 void set_sleep_flag() { sleep_flag=true; }
182 bool get_sleep_flag() { return sleep_flag; }
183 unsigned int hash() const;
185 bool equals(const ModelAction *x) const { return this == x; }
189 void set_value(uint64_t val) { value = val; }
191 /* to accomodate pthread create and join */
192 Thread * thread_operand;
193 void set_thread_operand(Thread *th) { thread_operand = th; }
196 const char * get_type_str() const;
197 const char * get_mo_str() const;
199 /** @brief Type of action (read, write, RMW, fence, thread create, etc.) */
202 /** @brief The memory order for this operation. */
205 /** @brief The original memory order parameter for this operation. */
206 memory_order original_order;
208 /** @brief A pointer to the memory location for this action. */
211 /** @brief The thread id that performed this action. */
214 /** @brief The value written (for write or RMW; undefined for read) */
218 * @brief The store that this action reads from
220 * Only valid for reads
222 const ModelAction *reads_from;
224 /** @brief The last fence release from the same thread */
225 const ModelAction *last_fence_release;
228 * @brief A back reference to a Node in NodeStack
230 * Only set if this ModelAction is saved on the NodeStack. (A
231 * ModelAction can be thrown away before it ever enters the NodeStack.)
236 * @brief The sequence number of this action
238 * Except for ATOMIC_UNINIT actions, this number should be unique and
239 * should represent the action's position in the execution order.
241 modelclock_t seq_number;
244 * @brief The clock vector for this operation
246 * Technically, this is only needed for potentially synchronizing
247 * (e.g., non-relaxed) operations, but it is very handy to have these
248 * vectors for all operations.
255 #endif /* __ACTION_H__ */