nodestack: bugfix - reset backtracking points on diverging paths
[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 /**
42  * @brief A single node in a NodeStack
43  *
44  * Represents a single node in the NodeStack. Each Node is associated with up
45  * to one action and up to one parent node. A node holds information
46  * regarding the last action performed (the "associated action"), the thread
47  * choices that have been explored (explored_children) and should be explored
48  * (backtrack), and the actions that the last action may read from.
49  */
50 class Node {
51 public:
52         Node(ModelAction *act, Node *par, int nthreads, Node *prevfairness);
53         ~Node();
54         /* return true = thread choice has already been explored */
55         bool has_been_explored(thread_id_t tid) const;
56         /* return true = backtrack set is empty */
57         bool backtrack_empty() const;
58
59         void clear_backtracking();
60         void explore_child(ModelAction *act, enabled_type_t *is_enabled);
61         /* return false = thread was already in backtrack */
62         bool set_backtrack(thread_id_t id);
63         thread_id_t get_next_backtrack();
64         bool is_enabled(Thread *t) const;
65         bool is_enabled(thread_id_t tid) const;
66         enabled_type_t enabled_status(thread_id_t tid) const;
67
68         ModelAction * get_action() const { return action; }
69         bool has_priority(thread_id_t tid) const;
70         int get_num_threads() const { return num_threads; }
71         /** @return the parent Node to this Node; that is, the action that
72          * occurred previously in the stack. */
73         Node * get_parent() const { return parent; }
74
75         bool add_future_value(struct future_value fv);
76         struct future_value get_future_value() const;
77         bool increment_future_value();
78         bool future_value_empty() const;
79
80         void add_read_from(const ModelAction *act);
81         const ModelAction * get_read_from() const;
82         bool increment_read_from();
83         bool read_from_empty() const;
84         int get_read_from_size() const;
85         const ModelAction * get_read_from_at(int i) const;
86
87         void set_promise(unsigned int i, bool is_rmw);
88         bool get_promise(unsigned int i) const;
89         bool increment_promise();
90         bool promise_empty() const;
91         enabled_type_t *get_enabled_array() {return enabled_array;}
92
93         void set_misc_max(int i);
94         int get_misc() const;
95         bool increment_misc();
96         bool misc_empty() const;
97
98         void add_relseq_break(const ModelAction *write);
99         const ModelAction * get_relseq_break() const;
100         bool increment_relseq_break();
101         bool relseq_break_empty() const;
102
103         void print() const;
104         void print_may_read_from();
105
106         MEMALLOC
107 private:
108         void explore(thread_id_t tid);
109
110         ModelAction * const action;
111         Node * const parent;
112         const int num_threads;
113         std::vector< bool, ModelAlloc<bool> > explored_children;
114         std::vector< bool, ModelAlloc<bool> > backtrack;
115         std::vector< struct fairness_info, ModelAlloc< struct fairness_info> > fairness;
116         int numBacktracks;
117         enabled_type_t *enabled_array;
118
119         /** The set of ModelActions that this the action at this Node may read
120          *  from. Only meaningful if this Node represents a 'read' action. */
121         std::vector< const ModelAction *, ModelAlloc< const ModelAction * > > may_read_from;
122
123         unsigned int read_from_index;
124
125         std::vector< struct future_value, ModelAlloc<struct future_value> > future_values;
126         std::vector< promise_t, ModelAlloc<promise_t> > promises;
127         int future_index;
128
129         std::vector< const ModelAction *, ModelAlloc<const ModelAction *> > relseq_break_writes;
130         int relseq_break_index;
131
132         int misc_index;
133         int misc_max;
134 };
135
136 typedef std::vector< Node *, ModelAlloc< Node * > > node_list_t;
137
138 /**
139  * @brief A stack of nodes
140  *
141  * Holds a Node linked-list that can be used for holding backtracking,
142  * may-read-from, and replay information. It is used primarily as a
143  * stack-like structure, in that backtracking points and replay nodes are
144  * only removed from the top (most recent).
145  */
146 class NodeStack {
147 public:
148         NodeStack();
149         ~NodeStack();
150         ModelAction * explore_action(ModelAction *act, enabled_type_t * is_enabled);
151         Node * get_head() const;
152         Node * get_next() const;
153         void reset_execution();
154         void pop_restofstack(int numAhead);
155         int get_total_nodes() { return total_nodes; }
156
157         void print() const;
158
159         MEMALLOC
160 private:
161         node_list_t node_list;
162
163         /**
164          * @brief the index position of the current head Node
165          *
166          * This index is relative to node_list. The index should point to the
167          * current head Node. It is negative when the list is empty.
168          */
169         int head_idx;
170
171         int total_nodes;
172 };
173
174 #endif /* __NODESTACK_H__ */