action: return synchronization status for ModelAction::read_from()
[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 <list>
9 #include <vector>
10 #include <cstddef>
11 #include <inttypes.h>
12
13 #include "mymemory.h"
14 #include "modeltypes.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 typedef enum {
27         PROMISE_IGNORE = 0, /**< This promise is inapplicable; ignore it */
28         PROMISE_UNFULFILLED, /**< This promise is applicable but unfulfilled */
29         PROMISE_FULFILLED /**< This promise is applicable and fulfilled */
30 } promise_t;
31
32 struct future_value {
33         uint64_t value;
34         modelclock_t expiration;
35 };
36
37 struct fairness_info {
38         unsigned int enabled_count;
39         unsigned int turns;
40         bool priority;
41 };
42
43
44 /**
45  * @brief A single node in a NodeStack
46  *
47  * Represents a single node in the NodeStack. Each Node is associated with up
48  * to one action and up to one parent node. A node holds information
49  * regarding the last action performed (the "associated action"), the thread
50  * choices that have been explored (explored_children) and should be explored
51  * (backtrack), and the actions that the last action may read from.
52  */
53 class Node {
54 public:
55         Node(ModelAction *act = NULL, Node *par = NULL, int nthreads = 1, Node *prevfairness = NULL);
56         ~Node();
57         /* return true = thread choice has already been explored */
58         bool has_been_explored(thread_id_t tid);
59         /* return true = backtrack set is empty */
60         bool backtrack_empty();
61
62         void explore_child(ModelAction *act, bool * is_enabled);
63         /* return false = thread was already in backtrack */
64         bool set_backtrack(thread_id_t id);
65         thread_id_t get_next_backtrack();
66         bool is_enabled(Thread *t);
67         bool is_enabled(thread_id_t tid);
68         ModelAction * get_action() { return action; }
69         bool has_priority(thread_id_t tid);
70         int get_num_threads() {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(uint64_t value, modelclock_t expiration);
76         uint64_t get_future_value();
77         modelclock_t get_future_value_expiration();
78         bool increment_future_value();
79         bool future_value_empty();
80
81         void add_read_from(const ModelAction *act);
82         const ModelAction * get_read_from();
83         bool increment_read_from();
84         bool read_from_empty();
85         int get_read_from_size();
86         const ModelAction * get_read_from_at(int i);
87
88         void set_promise(unsigned int i);
89         bool get_promise(unsigned int i);
90         bool increment_promise();
91         bool promise_empty();
92
93         void print();
94         void print_may_read_from();
95
96         MEMALLOC
97 private:
98         void explore(thread_id_t tid);
99
100         ModelAction *action;
101         Node *parent;
102         int num_threads;
103         std::vector< bool, ModelAlloc<bool> > explored_children;
104         std::vector< bool, ModelAlloc<bool> > backtrack;
105         std::vector< struct fairness_info, ModelAlloc< struct fairness_info> > fairness;
106         int numBacktracks;
107         bool *enabled_array;
108
109         /** The set of ModelActions that this the action at this Node may read
110          *  from. Only meaningful if this Node represents a 'read' action. */
111         std::vector< const ModelAction *, ModelAlloc< const ModelAction * > > may_read_from;
112
113         unsigned int read_from_index;
114
115         std::vector< struct future_value, ModelAlloc<struct future_value> > future_values;
116         std::vector< promise_t, ModelAlloc<promise_t> > promises;
117         int future_index;
118 };
119
120 typedef std::vector< Node *, ModelAlloc< Node * > > node_list_t;
121
122 /**
123  * @brief A stack of nodes
124  *
125  * Holds a Node linked-list that can be used for holding backtracking,
126  * may-read-from, and replay information. It is used primarily as a
127  * stack-like structure, in that backtracking points and replay nodes are
128  * only removed from the top (most recent).
129  */
130 class NodeStack {
131 public:
132         NodeStack();
133         ~NodeStack();
134         ModelAction * explore_action(ModelAction *act, bool * is_enabled);
135         Node * get_head();
136         Node * get_next();
137         void reset_execution();
138         void pop_restofstack(int numAhead);
139         int get_total_nodes() { return total_nodes; }
140
141         void print();
142
143         MEMALLOC
144 private:
145         node_list_t node_list;
146         unsigned int iter;
147
148         int total_nodes;
149 };
150
151 #endif /* __NODESTACK_H__ */