Replace some switch_to_master with switch_thread
[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         THREAD_SLEEP,   // < A sleep operation
57
58         PTHREAD_CREATE, // < A pthread creation action
59         PTHREAD_JOIN,   // < A pthread join action
60
61         NONATOMIC_WRITE,        // < Represents a non-atomic store
62         ATOMIC_INIT,    // < Initialization of an atomic object (e.g., atomic_init())
63         ATOMIC_WRITE,   // < An atomic write action
64         ATOMIC_RMW,     // < The write part of an atomic RMW action
65         ATOMIC_READ,    // < An atomic read action
66         ATOMIC_RMWR,    // < The read part of an atomic RMW action
67         ATOMIC_RMWRCAS, // < The read part of an atomic RMW action
68         ATOMIC_RMWC,    // < Convert an atomic RMW action into a READ
69
70         ATOMIC_FENCE,   // < A fence action
71         ATOMIC_LOCK,    // < A lock action
72         ATOMIC_TRYLOCK, // < A trylock action
73         ATOMIC_UNLOCK,  // < An unlock action
74
75         ATOMIC_NOTIFY_ONE,      // < A notify_one action
76         ATOMIC_NOTIFY_ALL,      // < A notify all action
77         ATOMIC_WAIT,    // < A wait action
78         ATOMIC_TIMEDWAIT,       // < A timed wait action
79         ATOMIC_ANNOTATION,      // < An annotation action to pass information to a trace analysis
80         READY_FREE,     // < Write is ready to be freed
81         ATOMIC_NOP      // < Placeholder
82 } action_type_t;
83
84
85 /**
86  * @brief Represents a single atomic action
87  *
88  * A ModelAction is always allocated as non-snapshotting, because it is used in
89  * multiple executions during backtracking. Except for non-atomic write
90  * ModelActions, each action is assigned a unique sequence
91  * number.
92  */
93 class ModelAction {
94 public:
95         ModelAction(action_type_t type, memory_order order, void *loc, uint64_t value = VALUE_NONE, Thread *thread = NULL);
96         ModelAction(action_type_t type, memory_order order, void *loc, uint64_t value, int size);
97         ModelAction(action_type_t type, const char * position, memory_order order, void *loc, uint64_t value, int size);
98         ModelAction(action_type_t type, memory_order order, uint64_t value, uint64_t time);
99         ModelAction(action_type_t type, const char * position, memory_order order, void *loc, uint64_t value = VALUE_NONE, Thread *thread = NULL);
100         ~ModelAction();
101         void print() const;
102
103         thread_id_t get_tid() const { return tid; }
104         action_type get_type() const { return type; }
105         void set_type(action_type _type) { type = _type; }
106         void set_free() { type = READY_FREE; }
107         memory_order get_mo() const { return order; }
108         memory_order get_original_mo() const { return original_order; }
109         void set_mo(memory_order order) { this->order = order; }
110         void * get_location() const { return location; }
111         const char * get_position() const { return position; }
112         modelclock_t get_seq_number() const { return seq_number; }
113         uint64_t get_value() const { return value; }
114         uint64_t get_reads_from_value() const;
115         uint64_t get_write_value() const;
116         uint64_t get_return_value() const;
117         ModelAction * get_reads_from() const { return reads_from; }
118         uint64_t get_time() const {return time;}
119         cdsc::mutex * get_mutex() const;
120
121         void set_read_from(ModelAction *act);
122
123         /** Store the most recent fence-release from the same thread
124          *  @param fence The fence-release that occured prior to this */
125         void set_last_fence_release(const ModelAction *fence) { last_fence_release = fence; }
126         /** @return The most recent fence-release from the same thread */
127         const ModelAction * get_last_fence_release() const { return last_fence_release; }
128
129         void copy_from_new(ModelAction *newaction);
130         void set_seq_number(modelclock_t num);
131         void reset_seq_number();
132         void set_try_lock(bool obtainedlock);
133         bool is_thread_start() const;
134         bool is_thread_join() const;
135         bool is_mutex_op() const;
136         bool is_lock() const;
137         bool is_sleep() const;
138         bool is_trylock() const;
139         bool is_unlock() const;
140         bool is_wait() const;
141         bool is_create() const;
142         bool is_notify() const;
143         bool is_notify_one() const;
144         bool is_success_lock() const;
145         bool is_failed_trylock() const;
146         bool is_atomic_var() const;
147         bool is_read() const;
148         bool is_write() const;
149         bool is_free() const;
150         bool is_yield() const;
151         bool could_be_write() const;
152         bool is_rmwr() const;
153         bool is_rmwrcas() const;
154         bool is_rmwc() const;
155         bool is_rmw() const;
156         bool is_fence() const;
157         bool is_initialization() const;
158         bool is_annotation() const;
159         bool is_relaxed() const;
160         bool is_acquire() const;
161         bool is_release() const;
162         bool is_seqcst() const;
163         bool same_var(const ModelAction *act) const;
164         bool same_thread(const ModelAction *act) const;
165         bool is_conflicting_lock(const ModelAction *act) const;
166         bool could_synchronize_with(const ModelAction *act) const;
167         int getSize() const;
168         Thread * get_thread_operand() const;
169         void create_cv(const ModelAction *parent = NULL);
170         ClockVector * get_cv() const { return cv; }
171         ClockVector * get_rfcv() const { return rf_cv; }
172         void set_rfcv(ClockVector * rfcv) { rf_cv = rfcv; }
173         bool synchronize_with(const ModelAction *act);
174
175         bool has_synchronized_with(const ModelAction *act) const;
176         bool happens_before(const ModelAction *act) const;
177
178         inline bool operator <(const ModelAction& act) const {
179                 return get_seq_number() < act.get_seq_number();
180         }
181         inline bool operator >(const ModelAction& act) const {
182                 return get_seq_number() > act.get_seq_number();
183         }
184
185         void process_rmw(ModelAction * act);
186         void copy_typeandorder(ModelAction * act);
187         unsigned int hash() const;
188         bool equals(const ModelAction *x) const { return this == x; }
189         void set_value(uint64_t val) { value = val; }
190
191         /* to accomodate pthread create and join */
192         Thread * thread_operand;
193         void set_thread_operand(Thread *th) { thread_operand = th; }
194         void setTraceRef(sllnode<ModelAction *> *ref) { trace_ref = ref; }
195         void setThrdMapRef(sllnode<ModelAction *> *ref) { thrdmap_ref = ref; }
196         void setActionRef(sllnode<ModelAction *> *ref) { action_ref = ref; }
197         sllnode<ModelAction *> * getTraceRef() { return trace_ref; }
198         sllnode<ModelAction *> * getThrdMapRef() { return thrdmap_ref; }
199         sllnode<ModelAction *> * getActionRef() { return action_ref; }
200         SNAPSHOTALLOC
201 private:
202         const char * get_type_str() const;
203         const char * get_mo_str() const;
204
205         /** @brief A pointer to the memory location for this action. */
206         void *location;
207
208         /** @brief A pointer to the source line for this atomic action. */
209         const char * position;
210
211         union {
212                 /**
213                  * @brief The store that this action reads from
214                  *
215                  * Only valid for reads
216                  */
217                 ModelAction *reads_from;
218                 int size;
219                 uint64_t time;  //used for sleep
220         };
221
222         /** @brief The last fence release from the same thread */
223         const ModelAction *last_fence_release;
224
225         /**
226          * @brief The clock vector for this operation
227          *
228          * Technically, this is only needed for potentially synchronizing
229          * (e.g., non-relaxed) operations, but it is very handy to have these
230          * vectors for all operations.
231          */
232         ClockVector *cv;
233         ClockVector *rf_cv;
234         sllnode<ModelAction *> * trace_ref;
235         sllnode<ModelAction *> * thrdmap_ref;
236         sllnode<ModelAction *> * action_ref;
237
238
239         /** @brief The value written (for write or RMW; undefined for read) */
240         uint64_t value;
241
242         /** @brief Type of action (read, write, RMW, fence, thread create, etc.) */
243         action_type type;
244
245         /** @brief The memory order for this operation. */
246         memory_order order;
247
248         /** @brief The original memory order parameter for this operation. */
249         memory_order original_order;
250
251         /** @brief The thread id that performed this action. */
252         thread_id_t tid;
253
254         /**
255          * @brief The sequence number of this action
256          *
257          * Except for non atomic write actions, this number should be unique and
258          * should represent the action's position in the execution order.
259          */
260         modelclock_t seq_number;
261 };
262
263 #endif  /* __ACTION_H__ */