mutex: change 'islocked' to hold Thread pointer
[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 struct fairness_info {
20         unsigned int enabled_count;
21         unsigned int turns;
22         bool priority;
23 };
24
25 typedef enum {
26         READ_FROM_PAST,
27         READ_FROM_PROMISE,
28         READ_FROM_FUTURE,
29         READ_FROM_NONE,
30 } read_from_type_t;
31
32 /**
33  * @brief A single node in a NodeStack
34  *
35  * Represents a single node in the NodeStack. Each Node is associated with up
36  * to one action and up to one parent node. A node holds information
37  * regarding the last action performed (the "associated action"), the thread
38  * choices that have been explored (explored_children) and should be explored
39  * (backtrack), and the actions that the last action may read from.
40  */
41 class Node {
42 public:
43         Node(ModelAction *act, Node *par, int nthreads, Node *prevfairness);
44         ~Node();
45         /* return true = thread choice has already been explored */
46         bool has_been_explored(thread_id_t tid) const;
47         /* return true = backtrack set is empty */
48         bool backtrack_empty() const;
49
50         void clear_backtracking();
51         void explore_child(ModelAction *act, enabled_type_t *is_enabled);
52         /* return false = thread was already in backtrack */
53         bool set_backtrack(thread_id_t id);
54         thread_id_t get_next_backtrack();
55         bool is_enabled(Thread *t) const;
56         bool is_enabled(thread_id_t tid) const;
57         enabled_type_t enabled_status(thread_id_t tid) const;
58
59         ModelAction * get_action() const { return action; }
60         bool has_priority(thread_id_t tid) const;
61         int get_num_threads() const { return num_threads; }
62         /** @return the parent Node to this Node; that is, the action that
63          * occurred previously in the stack. */
64         Node * get_parent() const { return parent; }
65
66         read_from_type_t get_read_from_status();
67         bool increment_read_from();
68         bool read_from_empty() const;
69         unsigned int read_from_size() const;
70
71         void print_read_from_past();
72         void add_read_from_past(const ModelAction *act);
73         const ModelAction * get_read_from_past() const;
74         const ModelAction * get_read_from_past(int i) const;
75         int get_read_from_past_size() const;
76
77         void add_read_from_promise(const ModelAction *reader);
78         Promise * get_read_from_promise() const;
79         Promise * get_read_from_promise(int i) const;
80         int get_read_from_promise_size() const;
81
82         bool add_future_value(struct future_value fv);
83         struct future_value get_future_value() const;
84
85         void set_promise(unsigned int i);
86         bool get_promise(unsigned int i) const;
87         bool increment_promise();
88         bool promise_empty() const;
89         void clear_promise_resolutions();
90
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
105         MEMALLOC
106 private:
107         void explore(thread_id_t tid);
108
109         bool read_from_past_empty() const;
110         bool increment_read_from_past();
111         bool read_from_promise_empty() const;
112         bool increment_read_from_promise();
113         bool future_value_empty() const;
114         bool increment_future_value();
115         read_from_type_t read_from_status;
116
117         ModelAction * const action;
118         Node * const parent;
119         const int num_threads;
120         std::vector< bool, ModelAlloc<bool> > explored_children;
121         std::vector< bool, ModelAlloc<bool> > backtrack;
122         std::vector< struct fairness_info, ModelAlloc< struct fairness_info> > fairness;
123         int numBacktracks;
124         enabled_type_t *enabled_array;
125
126         /**
127          * The set of past ModelActions that this the action at this Node may
128          * read from. Only meaningful if this Node represents a 'read' action.
129          */
130         std::vector< const ModelAction *, ModelAlloc< const ModelAction * > > read_from_past;
131         unsigned int read_from_past_idx;
132
133         std::vector< const ModelAction *, ModelAlloc<const ModelAction *> > read_from_promises;
134         int read_from_promise_idx;
135
136         std::vector< struct future_value, ModelAlloc<struct future_value> > future_values;
137         int future_index;
138
139         std::vector< bool, ModelAlloc<bool> > resolve_promise;
140         int resolve_promise_idx;
141
142         std::vector< const ModelAction *, ModelAlloc<const ModelAction *> > relseq_break_writes;
143         int relseq_break_index;
144
145         int misc_index;
146         int misc_max;
147 };
148
149 typedef std::vector< Node *, ModelAlloc< Node * > > node_list_t;
150
151 /**
152  * @brief A stack of nodes
153  *
154  * Holds a Node linked-list that can be used for holding backtracking,
155  * may-read-from, and replay information. It is used primarily as a
156  * stack-like structure, in that backtracking points and replay nodes are
157  * only removed from the top (most recent).
158  */
159 class NodeStack {
160 public:
161         NodeStack();
162         ~NodeStack();
163         ModelAction * explore_action(ModelAction *act, enabled_type_t * is_enabled);
164         Node * get_head() const;
165         Node * get_next() const;
166         void reset_execution();
167         void pop_restofstack(int numAhead);
168         int get_total_nodes() { return total_nodes; }
169
170         void print() const;
171
172         MEMALLOC
173 private:
174         node_list_t node_list;
175
176         /**
177          * @brief the index position of the current head Node
178          *
179          * This index is relative to node_list. The index should point to the
180          * current head Node. It is negative when the list is empty.
181          */
182         int head_idx;
183
184         int total_nodes;
185 };
186
187 #endif /* __NODESTACK_H__ */