2 * @brief Models actions taken by threads.
12 #include "memoryorder.h"
13 #include "modeltypes.h"
15 /* Forward declarations */
24 using std::memory_order;
25 using std::memory_order_relaxed;
26 using std::memory_order_acquire;
27 using std::memory_order_release;
28 using std::memory_order_acq_rel;
29 using std::memory_order_seq_cst;
32 * @brief A recognizable don't-care value for use in the ModelAction::value
35 * Note that this value can be legitimately used by a program, and hence by
36 * iteself does not indicate no value.
38 #define VALUE_NONE 0xdeadbeef
41 * @brief The "location" at which a fence occurs
43 * We need a non-zero memory location to associate with fences, since our hash
44 * tables don't handle NULL-pointer keys. HACK: Hopefully this doesn't collide
45 * with any legitimate memory locations.
47 #define FENCE_LOCATION ((void *)0x7)
49 /** @brief Represents an action type, identifying one of several types of
51 typedef enum action_type {
52 MODEL_FIXUP_RELSEQ, /**< Special ModelAction: finalize a release
54 THREAD_CREATE, /**< A thread creation action */
55 THREAD_START, /**< First action in each thread */
56 THREAD_YIELD, /**< A thread yield action */
57 THREAD_JOIN, /**< A thread join action */
58 THREAD_FINISH, /**< A thread completion action */
59 ATOMIC_UNINIT, /**< Represents an uninitialized atomic */
60 ATOMIC_READ, /**< An atomic read action */
61 ATOMIC_WRITE, /**< An atomic write action */
62 ATOMIC_RMWR, /**< The read part of an atomic RMW action */
63 ATOMIC_RMW, /**< The write part of an atomic RMW action */
64 ATOMIC_RMWC, /**< Convert an atomic RMW action into a READ */
65 ATOMIC_INIT, /**< Initialization of an atomic object (e.g.,
67 ATOMIC_FENCE, /**< A fence action */
68 ATOMIC_LOCK, /**< A lock action */
69 ATOMIC_TRYLOCK, /**< A trylock action */
70 ATOMIC_UNLOCK, /**< An unlock action */
71 ATOMIC_NOTIFY_ONE, /**< A notify_one action */
72 ATOMIC_NOTIFY_ALL, /**< A notify all action */
73 ATOMIC_WAIT /**< A wait action */
76 /* Forward declaration */
81 * @brief Represents a single atomic action
83 * A ModelAction is always allocated as non-snapshotting, because it is used in
84 * multiple executions during backtracking. Except for fake uninitialized
85 * (ATOMIC_UNINIT) ModelActions, each action is assigned a unique sequence
90 ModelAction(action_type_t type, memory_order order, void *loc, uint64_t value = VALUE_NONE, Thread *thread = NULL);
94 thread_id_t get_tid() const { return tid; }
95 action_type get_type() const { return type; }
96 memory_order get_mo() const { return order; }
97 void * get_location() const { return location; }
98 modelclock_t get_seq_number() const { return seq_number; }
99 uint64_t get_value() const { return value; }
100 uint64_t get_reads_from_value() const;
101 uint64_t get_write_value() const;
102 uint64_t get_return_value() const;
103 const ModelAction * get_reads_from() const { return reads_from; }
104 Promise * get_reads_from_promise() const { return reads_from_promise; }
105 std::mutex * get_mutex() const;
107 Node * get_node() const;
108 void set_node(Node *n) { node = n; }
110 void set_read_from(const ModelAction *act);
111 void set_read_from_promise(Promise *promise);
113 /** Store the most recent fence-release from the same thread
114 * @param fence The fence-release that occured prior to this */
115 void set_last_fence_release(const ModelAction *fence) { last_fence_release = fence; }
116 /** @return The most recent fence-release from the same thread */
117 const ModelAction * get_last_fence_release() const { return last_fence_release; }
119 void copy_from_new(ModelAction *newaction);
120 void set_seq_number(modelclock_t num);
121 void set_try_lock(bool obtainedlock);
122 bool is_thread_start() const;
123 bool is_thread_join() const;
124 bool is_relseq_fixup() const;
125 bool is_mutex_op() const;
126 bool is_lock() const;
127 bool is_trylock() const;
128 bool is_unlock() const;
129 bool is_wait() const;
130 bool is_notify() const;
131 bool is_notify_one() const;
132 bool is_success_lock() const;
133 bool is_failed_trylock() const;
134 bool is_atomic_var() const;
135 bool is_uninitialized() const;
136 bool is_read() const;
137 bool is_write() const;
138 bool is_yield() const;
139 bool could_be_write() const;
140 bool is_rmwr() const;
141 bool is_rmwc() const;
143 bool is_fence() const;
144 bool is_initialization() const;
145 bool is_relaxed() const;
146 bool is_acquire() const;
147 bool is_release() const;
148 bool is_seqcst() const;
149 bool same_var(const ModelAction *act) const;
150 bool same_thread(const ModelAction *act) const;
151 bool is_conflicting_lock(const ModelAction *act) const;
152 bool could_synchronize_with(const ModelAction *act) const;
154 Thread * get_thread_operand() const;
156 void create_cv(const ModelAction *parent = NULL);
157 ClockVector * get_cv() const { return cv; }
158 bool synchronize_with(const ModelAction *act);
160 bool has_synchronized_with(const ModelAction *act) const;
161 bool happens_before(const ModelAction *act) const;
163 inline bool operator <(const ModelAction& act) const {
164 return get_seq_number() < act.get_seq_number();
166 inline bool operator >(const ModelAction& act) const {
167 return get_seq_number() > act.get_seq_number();
170 void process_rmw(ModelAction * act);
171 void copy_typeandorder(ModelAction * act);
173 void set_sleep_flag() { sleep_flag=true; }
174 bool get_sleep_flag() { return sleep_flag; }
175 unsigned int hash() const;
177 bool equals(const ModelAction *x) const { return this == x; }
178 bool equals(const Promise *x) const { return false; }
180 bool may_read_from(const ModelAction *write) const;
181 bool may_read_from(const Promise *promise) const;
185 const char * get_type_str() const;
186 const char * get_mo_str() const;
188 /** @brief Type of action (read, write, RMW, fence, thread create, etc.) */
191 /** @brief The memory order for this operation. */
194 /** @brief A pointer to the memory location for this action. */
197 /** @brief The thread id that performed this action. */
200 /** @brief The value written (for write or RMW; undefined for read) */
204 * @brief The store that this action reads from
206 * Only valid for reads
208 const ModelAction *reads_from;
211 * @brief The promise that this action reads from
213 * Only valid for reads
215 Promise *reads_from_promise;
217 /** @brief The last fence release from the same thread */
218 const ModelAction *last_fence_release;
221 * @brief A back reference to a Node in NodeStack
223 * Only set if this ModelAction is saved on the NodeStack. (A
224 * ModelAction can be thrown away before it ever enters the NodeStack.)
229 * @brief The sequence number of this action
231 * Except for ATOMIC_UNINIT actions, this number should be unique and
232 * should represent the action's position in the execution order.
234 modelclock_t seq_number;
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.
248 #endif /* __ACTION_H__ */