8a20c45cb2c03e53bdeaf53ee8e80a89b3882014
[model-checker.git] / nodestack.h
1 /** @file nodestack.h
2  *  @brief Stack of operations for use in backtracking.
3 */
4
5 #ifndef __NODESTACK_H__
6 #define __NODESTACK_H__
7
8 #include <vector>
9 #include <cstddef>
10 #include <inttypes.h>
11
12 #include "mymemory.h"
13 #include "schedule.h"
14 #include "promise.h"
15
16 class ModelAction;
17 class Thread;
18
19 /**
20  * A flag used for the promise counting/combination problem within a node,
21  * denoting whether a particular Promise is
22  * <ol><li>@b applicable: can be satisfied by this Node's ModelAction and</li>
23  * <li>@b fulfilled: satisfied by this Node's ModelAction under the current
24  * configuration.</li></ol>
25  */
26
27 #define PROMISE_IGNORE 0 /**< This promise is inapplicable; ignore it */
28 #define PROMISE_UNFULFILLED 1 /**< This promise is applicable but unfulfilled */
29 #define PROMISE_FULFILLED 2 /**< This promise is applicable and fulfilled */
30 #define PROMISE_MASK 0xf
31 #define PROMISE_RMW 0x10
32
33 typedef int promise_t;
34
35 struct fairness_info {
36         unsigned int enabled_count;
37         unsigned int turns;
38         bool priority;
39 };
40
41 typedef enum {
42         READ_FROM_PAST,
43         READ_FROM_PROMISE,
44         READ_FROM_FUTURE,
45         READ_FROM_NONE,
46 } read_from_type_t;
47
48 /**
49  * @brief A single node in a NodeStack
50  *
51  * Represents a single node in the NodeStack. Each Node is associated with up
52  * to one action and up to one parent node. A node holds information
53  * regarding the last action performed (the "associated action"), the thread
54  * choices that have been explored (explored_children) and should be explored
55  * (backtrack), and the actions that the last action may read from.
56  */
57 class Node {
58 public:
59         Node(ModelAction *act, Node *par, int nthreads, Node *prevfairness);
60         ~Node();
61         /* return true = thread choice has already been explored */
62         bool has_been_explored(thread_id_t tid) const;
63         /* return true = backtrack set is empty */
64         bool backtrack_empty() const;
65
66         void clear_backtracking();
67         void explore_child(ModelAction *act, enabled_type_t *is_enabled);
68         /* return false = thread was already in backtrack */
69         bool set_backtrack(thread_id_t id);
70         thread_id_t get_next_backtrack();
71         bool is_enabled(Thread *t) const;
72         bool is_enabled(thread_id_t tid) const;
73         enabled_type_t enabled_status(thread_id_t tid) const;
74
75         ModelAction * get_action() const { return action; }
76         bool has_priority(thread_id_t tid) const;
77         int get_num_threads() const { return num_threads; }
78         /** @return the parent Node to this Node; that is, the action that
79          * occurred previously in the stack. */
80         Node * get_parent() const { return parent; }
81
82         read_from_type_t get_read_from_status();
83         bool increment_read_from();
84         bool read_from_empty() const;
85         unsigned int read_from_size() const;
86
87         void print_read_from_past();
88         void add_read_from_past(const ModelAction *act);
89         const ModelAction * get_read_from_past() const;
90         const ModelAction * get_read_from_past(int i) const;
91         int get_read_from_past_size() const;
92
93         void add_read_from_promise(const ModelAction *reader);
94         const Promise * get_read_from_promise() const;
95
96         bool add_future_value(struct future_value fv);
97         struct future_value get_future_value() const;
98
99         void set_promise(unsigned int i, bool is_rmw);
100         bool get_promise(unsigned int i) const;
101         bool increment_promise();
102         bool promise_empty() const;
103         enabled_type_t *get_enabled_array() {return enabled_array;}
104
105         void set_misc_max(int i);
106         int get_misc() const;
107         bool increment_misc();
108         bool misc_empty() const;
109
110         void add_relseq_break(const ModelAction *write);
111         const ModelAction * get_relseq_break() const;
112         bool increment_relseq_break();
113         bool relseq_break_empty() const;
114
115         void print() const;
116
117         MEMALLOC
118 private:
119         void explore(thread_id_t tid);
120
121         bool read_from_past_empty() const;
122         bool increment_read_from_past();
123         bool read_from_promise_empty() const;
124         bool increment_read_from_promise();
125         bool future_value_empty() const;
126         bool increment_future_value();
127         read_from_type_t read_from_status;
128
129         ModelAction * const action;
130         Node * const parent;
131         const int num_threads;
132         std::vector< bool, ModelAlloc<bool> > explored_children;
133         std::vector< bool, ModelAlloc<bool> > backtrack;
134         std::vector< struct fairness_info, ModelAlloc< struct fairness_info> > fairness;
135         int numBacktracks;
136         enabled_type_t *enabled_array;
137
138         /**
139          * The set of past ModelActions that this the action at this Node may
140          * read from. Only meaningful if this Node represents a 'read' action.
141          */
142         std::vector< const ModelAction *, ModelAlloc< const ModelAction * > > read_from_past;
143         unsigned int read_from_past_idx;
144
145         std::vector< const ModelAction *, ModelAlloc<const ModelAction *> > read_from_promises;
146         int read_from_promise_idx;
147
148         std::vector< struct future_value, ModelAlloc<struct future_value> > future_values;
149         std::vector< promise_t, ModelAlloc<promise_t> > promises;
150         int future_index;
151
152         std::vector< const ModelAction *, ModelAlloc<const ModelAction *> > relseq_break_writes;
153         int relseq_break_index;
154
155         int misc_index;
156         int misc_max;
157 };
158
159 typedef std::vector< Node *, ModelAlloc< Node * > > node_list_t;
160
161 /**
162  * @brief A stack of nodes
163  *
164  * Holds a Node linked-list that can be used for holding backtracking,
165  * may-read-from, and replay information. It is used primarily as a
166  * stack-like structure, in that backtracking points and replay nodes are
167  * only removed from the top (most recent).
168  */
169 class NodeStack {
170 public:
171         NodeStack();
172         ~NodeStack();
173         ModelAction * explore_action(ModelAction *act, enabled_type_t * is_enabled);
174         Node * get_head() const;
175         Node * get_next() const;
176         void reset_execution();
177         void pop_restofstack(int numAhead);
178         int get_total_nodes() { return total_nodes; }
179
180         void print() const;
181
182         MEMALLOC
183 private:
184         node_list_t node_list;
185
186         /**
187          * @brief the index position of the current head Node
188          *
189          * This index is relative to node_list. The index should point to the
190          * current head Node. It is negative when the list is empty.
191          */
192         int head_idx;
193
194         int total_nodes;
195 };
196
197 #endif /* __NODESTACK_H__ */