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