remove two unnecessary files
[c11tester.git] / action.h
1 /** @file action.h
2  *  @brief Models actions taken by threads.
3  */
4
5 #ifndef __ACTION_H__
6 #define __ACTION_H__
7
8 #include <cstddef>
9 #include <inttypes.h>
10
11 #include "mymemory.h"
12 #include "memoryorder.h"
13 #include "modeltypes.h"
14
15 /* Forward declarations */
16 class ClockVector;
17 class Thread;
18 class Promise;
19
20 namespace std {
21         class mutex;
22 }
23
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;
31
32 /**
33  * @brief A recognizable don't-care value for use in the ModelAction::value
34  * field
35  *
36  * Note that this value can be legitimately used by a program, and hence by
37  * iteself does not indicate no value.
38  */
39 #define VALUE_NONE 0xdeadbeef
40
41 /**
42  * @brief The "location" at which a fence occurs
43  *
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.
47  */
48 #define FENCE_LOCATION ((void *)0x7)
49
50 /** @brief Represents an action type, identifying one of several types of
51  * ModelAction */
52 typedef enum action_type {
53         MODEL_FIXUP_RELSEQ,   /**< Special ModelAction: finalize a release
54                                *   sequence */
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         ATOMIC_UNINIT,        /**< Represents an uninitialized atomic */
61         ATOMIC_READ,          /**< An atomic read action */
62         ATOMIC_WRITE,         /**< An atomic write action */
63         ATOMIC_RMWR,          /**< The read part of an atomic RMW action */
64         ATOMIC_RMW,           /**< The write part of an atomic RMW action */
65         ATOMIC_RMWC,          /**< Convert an atomic RMW action into a READ */
66         ATOMIC_INIT,          /**< Initialization of an atomic object (e.g.,
67                                *   atomic_init()) */
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
76                                                                                                          to a trace analysis */
77 } action_type_t;
78
79 /* Forward declaration */
80 class Node;
81 class ClockVector;
82
83 /**
84  * @brief Represents a single atomic action
85  *
86  * A ModelAction is always allocated as non-snapshotting, because it is used in
87  * multiple executions during backtracking. Except for fake uninitialized
88  * (ATOMIC_UNINIT) ModelActions, each action is assigned a unique sequence
89  * number.
90  */
91 class ModelAction {
92 public:
93         ModelAction(action_type_t type, memory_order order, void *loc, uint64_t value = VALUE_NONE, Thread *thread = NULL);
94         ~ModelAction();
95         void print() const;
96
97         thread_id_t get_tid() const { return tid; }
98         action_type get_type() const { return type; }
99         memory_order get_mo() const { return order; }
100         memory_order get_original_mo() const { return original_order; }
101         void set_mo(memory_order order) { this->order = order; }
102         void * get_location() const { return location; }
103         modelclock_t get_seq_number() const { return seq_number; }
104         uint64_t get_value() const { return value; }
105         uint64_t get_reads_from_value() const;
106         uint64_t get_write_value() const;
107         uint64_t get_return_value() const;
108         const ModelAction * get_reads_from() const { return reads_from; }
109         Promise * get_reads_from_promise() const { return reads_from_promise; }
110         std::mutex * get_mutex() const;
111
112         Node * get_node() const;
113         void set_node(Node *n) { node = n; }
114
115         void set_read_from(const ModelAction *act);
116         void set_read_from_promise(Promise *promise);
117
118         /** Store the most recent fence-release from the same thread
119          *  @param fence The fence-release that occured prior to this */
120         void set_last_fence_release(const ModelAction *fence) { last_fence_release = fence; }
121         /** @return The most recent fence-release from the same thread */
122         const ModelAction * get_last_fence_release() const { return last_fence_release; }
123
124         void copy_from_new(ModelAction *newaction);
125         void set_seq_number(modelclock_t num);
126         void set_try_lock(bool obtainedlock);
127         bool is_thread_start() const;
128         bool is_thread_join() const;
129         bool is_relseq_fixup() const;
130         bool is_mutex_op() const;
131         bool is_lock() const;
132         bool is_trylock() const;
133         bool is_unlock() const;
134         bool is_wait() const;
135         bool is_notify() const;
136         bool is_notify_one() const;
137         bool is_success_lock() const;
138         bool is_failed_trylock() const;
139         bool is_atomic_var() const;
140         bool is_uninitialized() const;
141         bool is_read() const;
142         bool is_write() const;
143         bool is_yield() const;
144         bool could_be_write() const;
145         bool is_rmwr() const;
146         bool is_rmwc() const;
147         bool is_rmw() const;
148         bool is_fence() const;
149         bool is_initialization() const;
150         bool is_annotation() const;
151         bool is_relaxed() const;
152         bool is_acquire() const;
153         bool is_release() const;
154         bool is_seqcst() const;
155         bool same_var(const ModelAction *act) const;
156         bool same_thread(const ModelAction *act) const;
157         bool is_conflicting_lock(const ModelAction *act) const;
158         bool could_synchronize_with(const ModelAction *act) const;
159
160         Thread * get_thread_operand() const;
161
162         void create_cv(const ModelAction *parent = NULL);
163         ClockVector * get_cv() const { return cv; }
164         bool synchronize_with(const ModelAction *act);
165
166         bool has_synchronized_with(const ModelAction *act) const;
167         bool happens_before(const ModelAction *act) const;
168
169         inline bool operator <(const ModelAction& act) const {
170                 return get_seq_number() < act.get_seq_number();
171         }
172         inline bool operator >(const ModelAction& act) const {
173                 return get_seq_number() > act.get_seq_number();
174         }
175
176         void process_rmw(ModelAction * act);
177         void copy_typeandorder(ModelAction * act);
178
179         void set_sleep_flag() { sleep_flag=true; }
180         bool get_sleep_flag() { return sleep_flag; }
181         unsigned int hash() const;
182
183         bool equals(const ModelAction *x) const { return this == x; }
184         bool equals(const Promise *x) const { return false; }
185
186         bool may_read_from(const ModelAction *write) const;
187         bool may_read_from(const Promise *promise) const;
188         MEMALLOC
189
190 // Added by WL
191         void set_value(uint64_t val) {
192                 value = val;
193         }
194 private:
195
196         const char * get_type_str() const;
197         const char * get_mo_str() const;
198
199         /** @brief Type of action (read, write, RMW, fence, thread create, etc.) */
200         action_type type;
201
202         /** @brief The memory order for this operation. */
203         memory_order order;
204
205         /** @brief The original memory order parameter for this operation. */
206         memory_order original_order;
207
208         /** @brief A pointer to the memory location for this action. */
209         void *location;
210
211         /** @brief The thread id that performed this action. */
212         thread_id_t tid;
213
214         /** @brief The value written (for write or RMW; undefined for read) */
215         uint64_t value;
216
217         /**
218          * @brief The store that this action reads from
219          *
220          * Only valid for reads
221          */
222         const ModelAction *reads_from;
223
224         /**
225          * @brief The promise that this action reads from
226          *
227          * Only valid for reads
228          */
229         Promise *reads_from_promise;
230
231         /** @brief The last fence release from the same thread */
232         const ModelAction *last_fence_release;
233
234         /**
235          * @brief A back reference to a Node in NodeStack
236          *
237          * Only set if this ModelAction is saved on the NodeStack. (A
238          * ModelAction can be thrown away before it ever enters the NodeStack.)
239          */
240         Node *node;
241
242         /**
243          * @brief The sequence number of this action
244          *
245          * Except for ATOMIC_UNINIT actions, this number should be unique and
246          * should represent the action's position in the execution order.
247          */
248         modelclock_t seq_number;
249
250         /**
251          * @brief The clock vector for this operation
252          *
253          * Technically, this is only needed for potentially synchronizing
254          * (e.g., non-relaxed) operations, but it is very handy to have these
255          * vectors for all operations.
256          */
257         ClockVector *cv;
258
259         bool sleep_flag;
260 };
261
262 #endif /* __ACTION_H__ */