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