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