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