Able to evaluate predicate expression against a 'context' and generate concrete predi...
[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 "mypthread.h"
15 #include "classlist.h"
16
17 namespace cdsc {
18 class mutex;
19 }
20
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
29 /**
30  * @brief A recognizable don't-care value for use in the ModelAction::value
31  * field
32  *
33  * Note that this value can be legitimately used by a program, and hence by
34  * iteself does not indicate no value.
35  */
36 #define VALUE_NONE 0xdeadbeef
37
38 /**
39  * @brief The "location" at which a fence occurs
40  *
41  * We need a non-zero memory location to associate with fences, since our hash
42  * tables don't handle NULL-pointer keys. HACK: Hopefully this doesn't collide
43  * with any legitimate memory locations.
44  */
45 #define FENCE_LOCATION ((void *)0x7)
46
47 /** @brief Represents an action type, identifying one of several types of
48  * ModelAction */
49 typedef enum action_type {
50         THREAD_CREATE,  // < A thread creation action
51         THREAD_START,   // < First action in each thread
52         THREAD_YIELD,   // < A thread yield action
53         THREAD_JOIN,    // < A thread join action
54         THREAD_FINISH,  // < A thread completion action
55         THREADONLY_FINISH,      // < A thread completion action
56         PTHREAD_CREATE, // < A pthread creation action
57         PTHREAD_JOIN,   // < A pthread join action
58         THREAD_SLEEP,   // < A sleep operation
59         ATOMIC_UNINIT,  // < Represents an uninitialized atomic
60         NONATOMIC_WRITE,        // < Represents a non-atomic store
61         ATOMIC_INIT,    // < Initialization of an atomic object (e.g., atomic_init())
62         ATOMIC_WRITE,   // < An atomic write action
63         ATOMIC_RMW,     // < The write part of an atomic RMW action
64         ATOMIC_READ,    // < An atomic read action
65         ATOMIC_RMWR,    // < The read part of an atomic RMW action
66         ATOMIC_RMWRCAS, // < The read part of an atomic RMW action
67         ATOMIC_RMWC,    // < Convert an atomic RMW action into a READ
68
69         ATOMIC_FENCE,   // < A fence action
70         ATOMIC_LOCK,    // < A lock action
71         ATOMIC_TRYLOCK, // < A trylock action
72         ATOMIC_UNLOCK,  // < An unlock action
73         ATOMIC_NOTIFY_ONE,      // < A notify_one action
74         ATOMIC_NOTIFY_ALL,      // < A notify all action
75         ATOMIC_WAIT,    // < A wait action
76         ATOMIC_TIMEDWAIT,       // < A timed wait action
77         ATOMIC_ANNOTATION,      // < An annotation action to pass information to a trace analysis
78         NOOP    // no operation, which returns control to scheduler
79 } action_type_t;
80
81
82 /**
83  * @brief Represents a single atomic action
84  *
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
88  * number.
89  */
90 class ModelAction {
91 public:
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, memory_order order, uint64_t value, uint64_t time);
96         ModelAction(action_type_t type, const char * position, memory_order order, void *loc, uint64_t value = VALUE_NONE, Thread *thread = NULL);
97         ~ModelAction();
98         void print() const;
99
100         thread_id_t get_tid() const { return tid; }
101         action_type get_type() const { return type; }
102         memory_order get_mo() const { return order; }
103         memory_order get_original_mo() const { return original_order; }
104         void set_mo(memory_order order) { this->order = order; }
105         void * get_location() const { return location; }
106         const char * get_position() const { return position; }
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         ModelAction * get_reads_from() const { return reads_from; }
113         uint64_t get_time() const {return time;}
114         cdsc::mutex * get_mutex() const;
115
116         void set_read_from(ModelAction *act);
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 reset_seq_number();
127         void set_try_lock(bool obtainedlock);
128         bool is_thread_start() const;
129         bool is_thread_join() const;
130         bool is_mutex_op() const;
131         bool is_lock() const;
132         bool is_sleep() const;
133         bool is_trylock() const;
134         bool is_unlock() const;
135         bool is_wait() const;
136         bool is_notify() const;
137         bool is_notify_one() const;
138         bool is_success_lock() const;
139         bool is_failed_trylock() const;
140         bool is_atomic_var() const;
141         bool is_uninitialized() const;
142         bool is_read() const;
143         bool is_write() const;
144         bool is_yield() const;
145         bool could_be_write() const;
146         bool is_rmwr() const;
147         bool is_rmwrcas() const;
148         bool is_rmwc() const;
149         bool is_rmw() 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;
161         int getSize() const;
162         Thread * get_thread_operand() const;
163         void create_cv(const ModelAction *parent = NULL);
164         ClockVector * get_cv() const { return cv; }
165         ClockVector * get_rfcv() const { return rf_cv; }
166         void set_rfcv(ClockVector * rfcv) { rf_cv = rfcv; }
167         bool synchronize_with(const ModelAction *act);
168
169         bool has_synchronized_with(const ModelAction *act) const;
170         bool happens_before(const ModelAction *act) const;
171
172         inline bool operator <(const ModelAction& act) const {
173                 return get_seq_number() < act.get_seq_number();
174         }
175         inline bool operator >(const ModelAction& act) const {
176                 return get_seq_number() > act.get_seq_number();
177         }
178
179         void process_rmw(ModelAction * act);
180         void copy_typeandorder(ModelAction * act);
181         unsigned int hash() const;
182         bool equals(const ModelAction *x) const { return this == x; }
183         void set_value(uint64_t val) { value = val; }
184
185         /* to accomodate pthread create and join */
186         Thread * thread_operand;
187         void set_thread_operand(Thread *th) { thread_operand = th; }
188         void set_uninit_action(ModelAction *act) { uninitaction = act; }
189         ModelAction * get_uninit_action() { return uninitaction; }
190         SNAPSHOTALLOC
191 private:
192         const char * get_type_str() const;
193         const char * get_mo_str() const;
194
195         /** @brief A pointer to the memory location for this action. */
196         void *location;
197
198         /** @brief A pointer to the source line for this atomic action. */
199         const char * position;
200
201         union {
202                 /**
203                  * @brief The store that this action reads from
204                  *
205                  * Only valid for reads
206                  */
207                 ModelAction *reads_from;
208                 int size;
209                 uint64_t time;  //used for sleep
210         };
211
212         /** @brief The last fence release from the same thread */
213         const ModelAction *last_fence_release;
214         ModelAction * uninitaction;
215
216         /**
217          * @brief The clock vector for this operation
218          *
219          * Technically, this is only needed for potentially synchronizing
220          * (e.g., non-relaxed) operations, but it is very handy to have these
221          * vectors for all operations.
222          */
223         ClockVector *cv;
224         ClockVector *rf_cv;
225
226         /** @brief The value written (for write or RMW; undefined for read) */
227         uint64_t value;
228
229         /** @brief Type of action (read, write, RMW, fence, thread create, etc.) */
230         action_type type;
231
232         /** @brief The memory order for this operation. */
233         memory_order order;
234
235         /** @brief The original memory order parameter for this operation. */
236         memory_order original_order;
237
238         /** @brief The thread id that performed this action. */
239         thread_id_t tid;
240
241         /**
242          * @brief The sequence number of this action
243          *
244          * Except for ATOMIC_UNINIT actions, this number should be unique and
245          * should represent the action's position in the execution order.
246          */
247         modelclock_t seq_number;
248 };
249
250 #endif  /* __ACTION_H__ */