clean up unnecessary member functions
[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 #include "pthread.h"
15
16 /* Forward declarations */
17 class ClockVector;
18 class Thread;
19 class Promise;
20
21 namespace std {
22         class mutex;
23 }
24
25 using std::memory_order;
26 using std::memory_order_relaxed;
27 using std::memory_order_consume;
28 using std::memory_order_acquire;
29 using std::memory_order_release;
30 using std::memory_order_acq_rel;
31 using std::memory_order_seq_cst;
32
33 /**
34  * @brief A recognizable don't-care value for use in the ModelAction::value
35  * field
36  *
37  * Note that this value can be legitimately used by a program, and hence by
38  * iteself does not indicate no value.
39  */
40 #define VALUE_NONE 0xdeadbeef
41
42 /**
43  * @brief The "location" at which a fence occurs
44  *
45  * We need a non-zero memory location to associate with fences, since our hash
46  * tables don't handle NULL-pointer keys. HACK: Hopefully this doesn't collide
47  * with any legitimate memory locations.
48  */
49 #define FENCE_LOCATION ((void *)0x7)
50
51 /** @brief Represents an action type, identifying one of several types of
52  * ModelAction */
53 typedef enum action_type {
54         MODEL_FIXUP_RELSEQ,   /**< Special ModelAction: finalize a release
55                                *   sequence */
56         THREAD_CREATE,        /**< A thread creation action */
57         THREAD_START,         /**< First action in each thread */
58         THREAD_YIELD,         /**< A thread yield action */
59         THREAD_JOIN,          /**< A thread join action */
60         THREAD_FINISH,        /**< A thread completion action */
61         PTHREAD_CREATE,       /**< A pthread creation action */
62         PTHREAD_JOIN,         /**< A pthread join action */
63         
64         ATOMIC_UNINIT,        /**< Represents an uninitialized atomic */
65         ATOMIC_READ,          /**< An atomic read action */
66         ATOMIC_WRITE,         /**< An atomic write action */
67         ATOMIC_RMWR,          /**< The read part of an atomic RMW action */
68         ATOMIC_RMW,           /**< The write part of an atomic RMW action */
69         ATOMIC_RMWC,          /**< Convert an atomic RMW action into a READ */
70         ATOMIC_INIT,          /**< Initialization of an atomic object (e.g.,
71                                *   atomic_init()) */
72         ATOMIC_FENCE,         /**< A fence action */
73         ATOMIC_LOCK,          /**< A lock action */
74         ATOMIC_TRYLOCK,       /**< A trylock action */
75         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_ANNOTATION     /**< An annotation action to pass information
80                                                                                                          to a trace analysis */
81 } action_type_t;
82
83 /* Forward declaration */
84 class Node;
85 class ClockVector;
86
87 /**
88  * @brief Represents a single atomic action
89  *
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
93  * number.
94  */
95 class ModelAction {
96 public:
97         ModelAction(action_type_t type, memory_order order, void *loc, uint64_t value = VALUE_NONE, Thread *thread = NULL);
98         ~ModelAction();
99         void print() const;
100
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         Promise * get_reads_from_promise() const { return reads_from_promise; }
114         std::mutex * get_mutex() const;
115
116         Node * get_node() const;
117         void set_node(Node *n) { node = n; }
118
119         void set_read_from(const ModelAction *act);
120         void set_read_from_promise(Promise *promise);
121
122         /** Store the most recent fence-release from the same thread
123          *  @param fence The fence-release that occured prior to this */
124         void set_last_fence_release(const ModelAction *fence) { last_fence_release = fence; }
125         /** @return The most recent fence-release from the same thread */
126         const ModelAction * get_last_fence_release() const { return last_fence_release; }
127
128         void copy_from_new(ModelAction *newaction);
129         void set_seq_number(modelclock_t num);
130         void set_try_lock(bool obtainedlock);
131         bool is_thread_start() const;
132         bool is_thread_join() const;
133         bool is_relseq_fixup() const;
134         bool is_mutex_op() const;
135         bool is_lock() const;
136         bool is_trylock() const;
137         bool is_unlock() const;
138         bool is_wait() const;
139         bool is_notify() const;
140         bool is_notify_one() const;
141         bool is_success_lock() const;
142         bool is_failed_trylock() const;
143         bool is_atomic_var() const;
144         bool is_uninitialized() const;
145         bool is_read() const;
146         bool is_write() const;
147         bool is_yield() const;
148         bool could_be_write() const;
149         bool is_rmwr() const;
150         bool is_rmwc() const;
151         bool is_rmw() const;
152         bool is_fence() const;
153         bool is_initialization() const;
154         bool is_annotation() const;
155         bool is_relaxed() const;
156         bool is_acquire() const;
157         bool is_release() const;
158         bool is_seqcst() const;
159         bool same_var(const ModelAction *act) const;
160         bool same_thread(const ModelAction *act) const;
161         bool is_conflicting_lock(const ModelAction *act) const;
162         bool could_synchronize_with(const ModelAction *act) const;
163
164         Thread * get_thread_operand() const;
165
166         void create_cv(const ModelAction *parent = NULL);
167         ClockVector * get_cv() const { return cv; }
168         bool synchronize_with(const ModelAction *act);
169
170         bool has_synchronized_with(const ModelAction *act) const;
171         bool happens_before(const ModelAction *act) const;
172
173         inline bool operator <(const ModelAction& act) const {
174                 return get_seq_number() < act.get_seq_number();
175         }
176         inline bool operator >(const ModelAction& act) const {
177                 return get_seq_number() > act.get_seq_number();
178         }
179
180         void process_rmw(ModelAction * act);
181         void copy_typeandorder(ModelAction * act);
182
183         void set_sleep_flag() { sleep_flag=true; }
184         bool get_sleep_flag() { return sleep_flag; }
185         unsigned int hash() const;
186
187         bool equals(const ModelAction *x) const { return this == x; }
188         bool equals(const Promise *x) const { return false; }
189
190         bool may_read_from(const ModelAction *write) const;
191         bool may_read_from(const Promise *promise) const;
192         MEMALLOC
193
194         void set_value(uint64_t val) { value = val; }
195
196         /* to accomodate pthread create and join */
197         Thread * thread_operand;
198         void set_thread_operand(Thread *th) { thread_operand = th; } 
199 private:
200
201         const char * get_type_str() const;
202         const char * get_mo_str() const;
203
204         /** @brief Type of action (read, write, RMW, fence, thread create, etc.) */
205         action_type type;
206
207         /** @brief The memory order for this operation. */
208         memory_order order;
209
210         /** @brief The original memory order parameter for this operation. */
211         memory_order original_order;
212
213         /** @brief A pointer to the memory location for this action. */
214         void *location;
215
216         /** @brief The thread id that performed this action. */
217         thread_id_t tid;
218
219         /** @brief The value written (for write or RMW; undefined for read) */
220         uint64_t value;
221
222         /**
223          * @brief The store that this action reads from
224          *
225          * Only valid for reads
226          */
227         const ModelAction *reads_from;
228
229         /**
230          * @brief The promise that this action reads from
231          *
232          * Only valid for reads
233          */
234         Promise *reads_from_promise;
235
236         /** @brief The last fence release from the same thread */
237         const ModelAction *last_fence_release;
238
239         /**
240          * @brief A back reference to a Node in NodeStack
241          *
242          * Only set if this ModelAction is saved on the NodeStack. (A
243          * ModelAction can be thrown away before it ever enters the NodeStack.)
244          */
245         Node *node;
246
247         /**
248          * @brief The sequence number of this action
249          *
250          * Except for ATOMIC_UNINIT actions, this number should be unique and
251          * should represent the action's position in the execution order.
252          */
253         modelclock_t seq_number;
254
255         /**
256          * @brief The clock vector for this operation
257          *
258          * Technically, this is only needed for potentially synchronizing
259          * (e.g., non-relaxed) operations, but it is very handy to have these
260          * vectors for all operations.
261          */
262         ClockVector *cv;
263
264         bool sleep_flag;
265 };
266
267 #endif /* __ACTION_H__ */