action: refactor, move VALUE_TRY{SUCCESS,FAILED} out of header
[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
15 class ClockVector;
16 class Thread;
17 class Promise;
18
19 namespace std {
20         class mutex;
21 }
22
23 using std::memory_order;
24 using std::memory_order_relaxed;
25 using std::memory_order_acquire;
26 using std::memory_order_release;
27 using std::memory_order_acq_rel;
28 using std::memory_order_seq_cst;
29
30 /** Note that this value can be legitimately used by a program, and
31  *  hence by iteself does not indicate no value. */
32 #define VALUE_NONE 0xdeadbeef
33
34 /** @brief Represents an action type, identifying one of several types of
35  * ModelAction */
36 typedef enum action_type {
37         MODEL_FIXUP_RELSEQ,   /**< Special ModelAction: finalize a release
38                                *   sequence */
39         THREAD_CREATE,        /**< A thread creation action */
40         THREAD_START,         /**< First action in each thread */
41         THREAD_YIELD,         /**< A thread yield action */
42         THREAD_JOIN,          /**< A thread join action */
43         THREAD_FINISH,        /**< A thread completion action */
44         ATOMIC_UNINIT,        /**< Represents an uninitialized atomic */
45         ATOMIC_READ,          /**< An atomic read action */
46         ATOMIC_WRITE,         /**< An atomic write action */
47         ATOMIC_RMWR,          /**< The read part of an atomic RMW action */
48         ATOMIC_RMW,           /**< The write part of an atomic RMW action */
49         ATOMIC_RMWC,          /**< Convert an atomic RMW action into a READ */
50         ATOMIC_INIT,          /**< Initialization of an atomic object (e.g.,
51                                *   atomic_init()) */
52         ATOMIC_FENCE,         /**< A fence action */
53         ATOMIC_LOCK,          /**< A lock action */
54         ATOMIC_TRYLOCK,       /**< A trylock action */
55         ATOMIC_UNLOCK,        /**< An unlock action */
56         ATOMIC_NOTIFY_ONE,    /**< A notify_one action */
57         ATOMIC_NOTIFY_ALL,    /**< A notify all action */
58         ATOMIC_WAIT           /**< A wait action */
59 } action_type_t;
60
61 /* Forward declaration */
62 class Node;
63 class ClockVector;
64
65 /**
66  * The ModelAction class encapsulates an atomic action.
67  */
68 class ModelAction {
69 public:
70         ModelAction(action_type_t type, memory_order order, void *loc, uint64_t value = VALUE_NONE, Thread *thread = NULL);
71         ~ModelAction();
72         void print() const;
73
74         thread_id_t get_tid() const { return tid; }
75         action_type get_type() const { return type; }
76         memory_order get_mo() const { return order; }
77         void * get_location() const { return location; }
78         modelclock_t get_seq_number() const { return seq_number; }
79         uint64_t get_value() const { return value; }
80         uint64_t get_reads_from_value() const;
81         uint64_t get_write_value() const;
82         uint64_t get_return_value() const;
83         const ModelAction * get_reads_from() const { return reads_from; }
84         Promise * get_reads_from_promise() const { return reads_from_promise; }
85         std::mutex * get_mutex() const;
86
87         Node * get_node() const;
88         void set_node(Node *n) { node = n; }
89
90         void set_read_from(const ModelAction *act);
91         void set_read_from_promise(Promise *promise);
92
93         /** Store the most recent fence-release from the same thread
94          *  @param fence The fence-release that occured prior to this */
95         void set_last_fence_release(const ModelAction *fence) { last_fence_release = fence; }
96         /** @return The most recent fence-release from the same thread */
97         const ModelAction * get_last_fence_release() const { return last_fence_release; }
98
99         void copy_from_new(ModelAction *newaction);
100         void set_seq_number(modelclock_t num);
101         void set_try_lock(bool obtainedlock);
102         bool is_thread_start() const;
103         bool is_thread_join() const;
104         bool is_relseq_fixup() const;
105         bool is_mutex_op() const;
106         bool is_lock() const;
107         bool is_trylock() const;
108         bool is_unlock() const;
109         bool is_wait() const;
110         bool is_notify() const;
111         bool is_notify_one() const;
112         bool is_success_lock() const;
113         bool is_failed_trylock() const;
114         bool is_atomic_var() const;
115         bool is_uninitialized() const;
116         bool is_read() const;
117         bool is_write() const;
118         bool is_yield() const;
119         bool could_be_write() const;
120         bool is_rmwr() const;
121         bool is_rmwc() const;
122         bool is_rmw() const;
123         bool is_fence() const;
124         bool is_initialization() const;
125         bool is_relaxed() const;
126         bool is_acquire() const;
127         bool is_release() const;
128         bool is_seqcst() const;
129         bool same_var(const ModelAction *act) const;
130         bool same_thread(const ModelAction *act) const;
131         bool is_conflicting_lock(const ModelAction *act) const;
132         bool could_synchronize_with(const ModelAction *act) const;
133
134         Thread * get_thread_operand() const;
135
136         void create_cv(const ModelAction *parent = NULL);
137         ClockVector * get_cv() const { return cv; }
138         bool synchronize_with(const ModelAction *act);
139
140         bool has_synchronized_with(const ModelAction *act) const;
141         bool happens_before(const ModelAction *act) const;
142
143         inline bool operator <(const ModelAction& act) const {
144                 return get_seq_number() < act.get_seq_number();
145         }
146         inline bool operator >(const ModelAction& act) const {
147                 return get_seq_number() > act.get_seq_number();
148         }
149
150         void process_rmw(ModelAction * act);
151         void copy_typeandorder(ModelAction * act);
152
153         void set_sleep_flag() { sleep_flag=true; }
154         bool get_sleep_flag() { return sleep_flag; }
155         unsigned int hash() const;
156
157         bool equals(const ModelAction *x) const { return this == x; }
158         bool equals(const Promise *x) const { return false; }
159
160         bool may_read_from(const ModelAction *write) const;
161         bool may_read_from(const Promise *promise) const;
162         MEMALLOC
163 private:
164
165         /** Type of action (read, write, thread create, thread yield, thread join) */
166         action_type type;
167
168         /** The memory order for this operation. */
169         memory_order order;
170
171         /** A pointer to the memory location for this action. */
172         void *location;
173
174         /** The thread id that performed this action. */
175         thread_id_t tid;
176
177         /** The value written (for write or RMW; undefined for read) */
178         uint64_t value;
179
180         /** The action that this action reads from. Only valid for reads */
181         const ModelAction *reads_from;
182
183         /** The promise that this action reads from. Only valid for reads */
184         Promise *reads_from_promise;
185
186         /** The last fence release from the same thread */
187         const ModelAction *last_fence_release;
188
189         /** A back reference to a Node in NodeStack, if this ModelAction is
190          * saved on the NodeStack. */
191         Node *node;
192
193         modelclock_t seq_number;
194
195         /** The clock vector stored with this action; only needed if this
196          * action is a store release? */
197         ClockVector *cv;
198
199         bool sleep_flag;
200 };
201
202 #endif /* __ACTION_H__ */