fix tabbing comment issue
[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         PTHREAD_CREATE, // < A pthread creation action
56         PTHREAD_JOIN,   // < A pthread join action
57         ATOMIC_UNINIT,  // < Represents an uninitialized atomic
58         ATOMIC_READ,    // < An atomic read action
59         ATOMIC_WRITE,   // < An atomic write action
60         ATOMIC_RMWR,    // < The read part of an atomic RMW action
61         ATOMIC_RMWRCAS, // < The read part of an atomic RMW action
62         ATOMIC_RMW,     // < The write part of an atomic RMW action
63         ATOMIC_RMWC,    // < Convert an atomic RMW action into a READ
64         ATOMIC_INIT,    // < Initialization of an atomic object (e.g., atomic_init())
65         ATOMIC_FENCE,   // < A fence action
66         ATOMIC_LOCK,    // < A lock action
67         ATOMIC_TRYLOCK, // < A trylock action
68         ATOMIC_UNLOCK,  // < An unlock action
69         ATOMIC_NOTIFY_ONE,      // < A notify_one action
70         ATOMIC_NOTIFY_ALL,      // < A notify all action
71         ATOMIC_WAIT,    // < A wait action
72         ATOMIC_ANNOTATION,      // < An annotation action to pass information to a trace analysis
73         NOOP
74 } action_type_t;
75
76
77 /**
78  * @brief Represents a single atomic action
79  *
80  * A ModelAction is always allocated as non-snapshotting, because it is used in
81  * multiple executions during backtracking. Except for fake uninitialized
82  * (ATOMIC_UNINIT) ModelActions, each action is assigned a unique sequence
83  * number.
84  */
85 class ModelAction {
86 public:
87         ModelAction(action_type_t type, memory_order order, void *loc, uint64_t value = VALUE_NONE, Thread *thread = NULL);
88         ModelAction(action_type_t type, memory_order order, void *loc, uint64_t value, int size);
89         ~ModelAction();
90         void print() const;
91
92         thread_id_t get_tid() const { return tid; }
93         action_type get_type() const { return type; }
94         memory_order get_mo() const { return order; }
95         memory_order get_original_mo() const { return original_order; }
96         void set_mo(memory_order order) { this->order = 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         cdsc::mutex * get_mutex() const;
105
106         Node * get_node() const;
107         void set_node(Node *n) { node = n; }
108
109         void set_read_from(const ModelAction *act);
110
111         /** Store the most recent fence-release from the same thread
112          *  @param fence The fence-release that occured prior to this */
113         void set_last_fence_release(const ModelAction *fence) { last_fence_release = fence; }
114         /** @return The most recent fence-release from the same thread */
115         const ModelAction * get_last_fence_release() const { return last_fence_release; }
116
117         void copy_from_new(ModelAction *newaction);
118         void set_seq_number(modelclock_t num);
119         void set_try_lock(bool obtainedlock);
120         bool is_thread_start() const;
121         bool is_thread_join() const;
122         bool is_mutex_op() const;
123         bool is_lock() const;
124         bool is_trylock() const;
125         bool is_unlock() const;
126         bool is_wait() const;
127         bool is_notify() const;
128         bool is_notify_one() const;
129         bool is_success_lock() const;
130         bool is_failed_trylock() const;
131         bool is_atomic_var() const;
132         bool is_uninitialized() const;
133         bool is_read() const;
134         bool is_write() const;
135         bool is_yield() const;
136         bool could_be_write() const;
137         bool is_rmwr() const;
138         bool is_rmwrcas() const;
139         bool is_rmwc() const;
140         bool is_rmw() const;
141         bool is_fence() const;
142         bool is_initialization() const;
143         bool is_annotation() const;
144         bool is_relaxed() const;
145         bool is_acquire() const;
146         bool is_release() const;
147         bool is_seqcst() const;
148         bool same_var(const ModelAction *act) const;
149         bool same_thread(const ModelAction *act) const;
150         bool is_conflicting_lock(const ModelAction *act) const;
151         bool could_synchronize_with(const ModelAction *act) const;
152         int getSize() const;
153         Thread * get_thread_operand() const;
154         void create_cv(const ModelAction *parent = NULL);
155         ClockVector * get_cv() const { return cv; }
156         bool synchronize_with(const ModelAction *act);
157
158         bool has_synchronized_with(const ModelAction *act) const;
159         bool happens_before(const ModelAction *act) const;
160
161         inline bool operator <(const ModelAction& act) const {
162                 return get_seq_number() < act.get_seq_number();
163         }
164         inline bool operator >(const ModelAction& act) const {
165                 return get_seq_number() > act.get_seq_number();
166         }
167
168         void process_rmw(ModelAction * act);
169         void copy_typeandorder(ModelAction * act);
170         unsigned int hash() const;
171         bool equals(const ModelAction *x) const { return this == x; }
172         void set_value(uint64_t val) { value = val; }
173
174         /* to accomodate pthread create and join */
175         Thread * thread_operand;
176         void set_thread_operand(Thread *th) { thread_operand = th; }
177         MEMALLOC
178 private:
179         const char * get_type_str() const;
180         const char * get_mo_str() const;
181
182         /** @brief A pointer to the memory location for this action. */
183         void *location;
184
185         union {
186                 /**
187                  * @brief The store that this action reads from
188                  *
189                  * Only valid for reads
190                  */
191                 const ModelAction *reads_from;
192                 int size;
193         };
194
195         /** @brief The last fence release from the same thread */
196         const ModelAction *last_fence_release;
197
198         /**
199          * @brief A back reference to a Node in NodeStack
200          *
201          * Only set if this ModelAction is saved on the NodeStack. (A
202          * ModelAction can be thrown away before it ever enters the NodeStack.)
203          */
204         Node *node;
205
206         /**
207          * @brief The clock vector for this operation
208          *
209          * Technically, this is only needed for potentially synchronizing
210          * (e.g., non-relaxed) operations, but it is very handy to have these
211          * vectors for all operations.
212          */
213         ClockVector *cv;
214
215         /** @brief The value written (for write or RMW; undefined for read) */
216         uint64_t value;
217
218         /** @brief Type of action (read, write, RMW, fence, thread create, etc.) */
219         action_type type;
220
221         /** @brief The memory order for this operation. */
222         memory_order order;
223
224         /** @brief The original memory order parameter for this operation. */
225         memory_order original_order;
226
227         /** @brief The thread id that performed this action. */
228         thread_id_t tid;
229
230         /**
231          * @brief The sequence number of this action
232          *
233          * Except for ATOMIC_UNINIT actions, this number should be unique and
234          * should represent the action's position in the execution order.
235          */
236         modelclock_t seq_number;
237 };
238
239 #endif  /* __ACTION_H__ */