nodestack: move "behaviors" increment all into Node wrapper function
[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 <cstddef>
9 #include <inttypes.h>
10
11 #include "mymemory.h"
12 #include "schedule.h"
13 #include "promise.h"
14 #include "stl-model.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 #define YIELD_E 1
33 #define YIELD_D 2
34 #define YIELD_S 4
35 #define YIELD_P 8
36 #define YIELD_INDEX(tid1, tid2, num_threads) (tid1*num_threads+tid2)
37
38
39 /**
40  * @brief A single node in a NodeStack
41  *
42  * Represents a single node in the NodeStack. Each Node is associated with up
43  * to one action and up to one parent node. A node holds information
44  * regarding the last action performed (the "associated action"), the thread
45  * choices that have been explored (explored_children) and should be explored
46  * (backtrack), and the actions that the last action may read from.
47  */
48 class Node {
49 public:
50         Node(ModelAction *act, Node *par, int nthreads, Node *prevfairness);
51         ~Node();
52         /* return true = thread choice has already been explored */
53         bool has_been_explored(thread_id_t tid) const;
54         /* return true = backtrack set is empty */
55         bool backtrack_empty() const;
56
57         void clear_backtracking();
58         void explore_child(ModelAction *act, enabled_type_t *is_enabled);
59         /* return false = thread was already in backtrack */
60         bool set_backtrack(thread_id_t id);
61         thread_id_t get_next_backtrack();
62         bool is_enabled(Thread *t) const;
63         bool is_enabled(thread_id_t tid) const;
64         enabled_type_t enabled_status(thread_id_t tid) const;
65
66         ModelAction * get_action() const { return action; }
67         void set_uninit_action(ModelAction *act) { uninit_action = act; }
68         ModelAction * get_uninit_action() const { return uninit_action; }
69
70         bool has_priority(thread_id_t tid) const;
71         void update_yield(Scheduler *);
72         bool has_priority_over(thread_id_t tid, thread_id_t tid2) const;
73         int get_num_threads() const { return num_threads; }
74         /** @return the parent Node to this Node; that is, the action that
75          * occurred previously in the stack. */
76         Node * get_parent() const { return parent; }
77
78         read_from_type_t get_read_from_status();
79         bool increment_read_from();
80         bool read_from_empty() const;
81         unsigned int read_from_size() const;
82
83         void print_read_from_past();
84         void add_read_from_past(const ModelAction *act);
85         const ModelAction * get_read_from_past() const;
86         const ModelAction * get_read_from_past(int i) const;
87         int get_read_from_past_size() const;
88
89         void add_read_from_promise(const ModelAction *reader);
90         Promise * get_read_from_promise() const;
91         Promise * get_read_from_promise(int i) const;
92         int get_read_from_promise_size() const;
93
94         bool add_future_value(struct future_value fv);
95         struct future_value get_future_value() const;
96
97         void set_promise(unsigned int i);
98         bool get_promise(unsigned int i) const;
99         bool increment_promise();
100         bool promise_empty() const;
101         void clear_promise_resolutions();
102
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         bool increment_behaviors();
116
117         void print() const;
118
119         MEMALLOC
120 private:
121         void explore(thread_id_t tid);
122         int get_yield_data(int tid1, int tid2) const;
123         bool read_from_past_empty() const;
124         bool increment_read_from_past();
125         bool read_from_promise_empty() const;
126         bool increment_read_from_promise();
127         bool future_value_empty() const;
128         bool increment_future_value();
129         read_from_type_t read_from_status;
130
131         ModelAction * const action;
132
133         /** @brief ATOMIC_UNINIT action which was created at this Node */
134         ModelAction *uninit_action;
135
136         Node * const parent;
137         const int num_threads;
138         ModelVector<bool> explored_children;
139         ModelVector<bool> backtrack;
140         ModelVector<struct fairness_info> fairness;
141         int numBacktracks;
142         enabled_type_t *enabled_array;
143
144         /**
145          * The set of past ModelActions that this the action at this Node may
146          * read from. Only meaningful if this Node represents a 'read' action.
147          */
148         ModelVector<const ModelAction *> read_from_past;
149         unsigned int read_from_past_idx;
150
151         ModelVector<const ModelAction *> read_from_promises;
152         int read_from_promise_idx;
153
154         ModelVector<struct future_value> future_values;
155         int future_index;
156
157         ModelVector<bool> resolve_promise;
158         int resolve_promise_idx;
159
160         ModelVector<const ModelAction *> relseq_break_writes;
161         int relseq_break_index;
162
163         int misc_index;
164         int misc_max;
165         int * yield_data;
166 };
167
168 typedef ModelVector<Node *> node_list_t;
169
170 /**
171  * @brief A stack of nodes
172  *
173  * Holds a Node linked-list that can be used for holding backtracking,
174  * may-read-from, and replay information. It is used primarily as a
175  * stack-like structure, in that backtracking points and replay nodes are
176  * only removed from the top (most recent).
177  */
178 class NodeStack {
179 public:
180         NodeStack();
181         ~NodeStack();
182         ModelAction * explore_action(ModelAction *act, enabled_type_t * is_enabled);
183         Node * get_head() const;
184         Node * get_next() const;
185         void reset_execution();
186         void pop_restofstack(int numAhead);
187         int get_total_nodes() { return total_nodes; }
188
189         void print() const;
190
191         MEMALLOC
192 private:
193         node_list_t node_list;
194
195         /**
196          * @brief the index position of the current head Node
197          *
198          * This index is relative to node_list. The index should point to the
199          * current head Node. It is negative when the list is empty.
200          */
201         int head_idx;
202
203         int total_nodes;
204 };
205
206 #endif /* __NODESTACK_H__ */