model: add too_many_steps()
[cdsspec-compiler.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_acquire;
27 using std::memory_order_release;
28 using std::memory_order_acq_rel;
29 using std::memory_order_seq_cst;
30
31 /**
32  * @brief A recognizable don't-care value for use in the ModelAction::value
33  * field
34  *
35  * Note that this value can be legitimately used by a program, and hence by
36  * iteself does not indicate no value.
37  */
38 #define VALUE_NONE 0xdeadbeef
39
40 /** @brief Represents an action type, identifying one of several types of
41  * ModelAction */
42 typedef enum action_type {
43         MODEL_FIXUP_RELSEQ,   /**< Special ModelAction: finalize a release
44                                *   sequence */
45         THREAD_CREATE,        /**< A thread creation action */
46         THREAD_START,         /**< First action in each thread */
47         THREAD_YIELD,         /**< A thread yield action */
48         THREAD_JOIN,          /**< A thread join action */
49         THREAD_FINISH,        /**< A thread completion action */
50         ATOMIC_UNINIT,        /**< Represents an uninitialized atomic */
51         ATOMIC_READ,          /**< An atomic read action */
52         ATOMIC_WRITE,         /**< An atomic write action */
53         ATOMIC_RMWR,          /**< The read part of an atomic RMW action */
54         ATOMIC_RMW,           /**< The write part of an atomic RMW action */
55         ATOMIC_RMWC,          /**< Convert an atomic RMW action into a READ */
56         ATOMIC_INIT,          /**< Initialization of an atomic object (e.g.,
57                                *   atomic_init()) */
58         ATOMIC_FENCE,         /**< A fence action */
59         ATOMIC_LOCK,          /**< A lock action */
60         ATOMIC_TRYLOCK,       /**< A trylock action */
61         ATOMIC_UNLOCK,        /**< An unlock action */
62         ATOMIC_NOTIFY_ONE,    /**< A notify_one action */
63         ATOMIC_NOTIFY_ALL,    /**< A notify all action */
64         ATOMIC_WAIT           /**< A wait action */
65 } action_type_t;
66
67 /* Forward declaration */
68 class Node;
69 class ClockVector;
70
71 /**
72  * @brief Represents a single atomic action
73  *
74  * A ModelAction is always allocated as non-snapshotting, because it is used in
75  * multiple executions during backtracking. Except for fake uninitialized
76  * (ATOMIC_UNINIT) ModelActions, each action is assigned a unique sequence
77  * number.
78  */
79 class ModelAction {
80 public:
81         ModelAction(action_type_t type, memory_order order, void *loc, uint64_t value = VALUE_NONE, Thread *thread = NULL);
82         ~ModelAction();
83         void print() const;
84
85         thread_id_t get_tid() const { return tid; }
86         action_type get_type() const { return type; }
87         memory_order get_mo() const { return order; }
88         void * get_location() const { return location; }
89         modelclock_t get_seq_number() const { return seq_number; }
90         uint64_t get_value() const { return value; }
91         uint64_t get_reads_from_value() const;
92         uint64_t get_write_value() const;
93         uint64_t get_return_value() const;
94         const ModelAction * get_reads_from() const { return reads_from; }
95         Promise * get_reads_from_promise() const { return reads_from_promise; }
96         std::mutex * get_mutex() const;
97
98         Node * get_node() const;
99         void set_node(Node *n) { node = n; }
100
101         void set_read_from(const ModelAction *act);
102         void set_read_from_promise(Promise *promise);
103
104         /** Store the most recent fence-release from the same thread
105          *  @param fence The fence-release that occured prior to this */
106         void set_last_fence_release(const ModelAction *fence) { last_fence_release = fence; }
107         /** @return The most recent fence-release from the same thread */
108         const ModelAction * get_last_fence_release() const { return last_fence_release; }
109
110         void copy_from_new(ModelAction *newaction);
111         void set_seq_number(modelclock_t num);
112         void set_try_lock(bool obtainedlock);
113         bool is_thread_start() const;
114         bool is_thread_join() const;
115         bool is_relseq_fixup() const;
116         bool is_mutex_op() const;
117         bool is_lock() const;
118         bool is_trylock() const;
119         bool is_unlock() const;
120         bool is_wait() const;
121         bool is_notify() const;
122         bool is_notify_one() const;
123         bool is_success_lock() const;
124         bool is_failed_trylock() const;
125         bool is_atomic_var() const;
126         bool is_uninitialized() const;
127         bool is_read() const;
128         bool is_write() const;
129         bool is_yield() const;
130         bool could_be_write() const;
131         bool is_rmwr() const;
132         bool is_rmwc() const;
133         bool is_rmw() const;
134         bool is_fence() const;
135         bool is_initialization() const;
136         bool is_relaxed() const;
137         bool is_acquire() const;
138         bool is_release() const;
139         bool is_seqcst() const;
140         bool same_var(const ModelAction *act) const;
141         bool same_thread(const ModelAction *act) const;
142         bool is_conflicting_lock(const ModelAction *act) const;
143         bool could_synchronize_with(const ModelAction *act) const;
144
145         Thread * get_thread_operand() const;
146
147         void create_cv(const ModelAction *parent = NULL);
148         ClockVector * get_cv() const { return cv; }
149         bool synchronize_with(const ModelAction *act);
150
151         bool has_synchronized_with(const ModelAction *act) const;
152         bool happens_before(const ModelAction *act) const;
153
154         inline bool operator <(const ModelAction& act) const {
155                 return get_seq_number() < act.get_seq_number();
156         }
157         inline bool operator >(const ModelAction& act) const {
158                 return get_seq_number() > act.get_seq_number();
159         }
160
161         void process_rmw(ModelAction * act);
162         void copy_typeandorder(ModelAction * act);
163
164         void set_sleep_flag() { sleep_flag=true; }
165         bool get_sleep_flag() { return sleep_flag; }
166         unsigned int hash() const;
167
168         bool equals(const ModelAction *x) const { return this == x; }
169         bool equals(const Promise *x) const { return false; }
170
171         bool may_read_from(const ModelAction *write) const;
172         bool may_read_from(const Promise *promise) const;
173         MEMALLOC
174 private:
175
176         /** @brief Type of action (read, write, RMW, fence, thread create, etc.) */
177         action_type type;
178
179         /** @brief The memory order for this operation. */
180         memory_order order;
181
182         /** @brief A pointer to the memory location for this action. */
183         void *location;
184
185         /** @brief The thread id that performed this action. */
186         thread_id_t tid;
187
188         /** @brief The value written (for write or RMW; undefined for read) */
189         uint64_t value;
190
191         /**
192          * @brief The store that this action reads from
193          *
194          * Only valid for reads
195          */
196         const ModelAction *reads_from;
197
198         /**
199          * @brief The promise that this action reads from
200          *
201          * Only valid for reads
202          */
203         Promise *reads_from_promise;
204
205         /** @brief The last fence release from the same thread */
206         const ModelAction *last_fence_release;
207
208         /**
209          * @brief A back reference to a Node in NodeStack
210          *
211          * Only set if this ModelAction is saved on the NodeStack. (A
212          * ModelAction can be thrown away before it ever enters the NodeStack.)
213          */
214         Node *node;
215
216         /**
217          * @brief The sequence number of this action
218          *
219          * Except for ATOMIC_UNINIT actions, this number should be unique and
220          * should represent the action's position in the execution order.
221          */
222         modelclock_t seq_number;
223
224         /**
225          * @brief The clock vector for this operation
226          *
227          * Technically, this is only needed for potentially synchronizing
228          * (e.g., non-relaxed) operations, but it is very handy to have these
229          * vectors for all operations.
230          */
231         ClockVector *cv;
232
233         bool sleep_flag;
234 };
235
236 #endif /* __ACTION_H__ */