model: fix the maxreads support
[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 "threads.h"
12 #include "mymemory.h"
13
14 class ModelAction;
15
16 /**
17  * A flag used for the promise counting/combination problem within a node,
18  * denoting whether a particular Promise is
19  * <ol><li>@b applicable: can be satisfied by this Node's ModelAction and</li>
20  * <li>@b fulfilled: satisfied by this Node's ModelAction under the current
21  * configuration.</li></ol>
22  */
23 typedef enum {
24         PROMISE_IGNORE = 0, /**< This promise is inapplicable; ignore it */
25         PROMISE_UNFULFILLED, /**< This promise is applicable but unfulfilled */
26         PROMISE_FULFILLED /**< This promise is applicable and fulfilled */
27 } promise_t;
28
29 /**
30  * @brief A single node in a NodeStack
31  *
32  * Represents a single node in the NodeStack. Each Node is associated with up
33  * to one action and up to one parent node. A node holds information
34  * regarding the last action performed (the "associated action"), the thread
35  * choices that have been explored (explored_children) and should be explored
36  * (backtrack), and the actions that the last action may read from.
37  */
38 class Node {
39 public:
40         Node(ModelAction *act = NULL, Node *par = NULL, int nthreads = 1);
41         ~Node();
42         /* return true = thread choice has already been explored */
43         bool has_been_explored(thread_id_t tid);
44         /* return true = backtrack set is empty */
45         bool backtrack_empty();
46
47         void explore_child(ModelAction *act);
48         /* return false = thread was already in backtrack */
49         bool set_backtrack(thread_id_t id);
50         thread_id_t get_next_backtrack();
51         bool is_enabled(Thread *t);
52         ModelAction * get_action() { return action; }
53
54         /** @return the parent Node to this Node; that is, the action that
55          * occurred previously in the stack. */
56         Node * get_parent() const { return parent; }
57
58         bool add_future_value(uint64_t value);
59         uint64_t get_future_value();
60         bool increment_future_value();
61         bool future_value_empty();
62
63         void add_read_from(const ModelAction *act);
64         const ModelAction * get_read_from();
65         bool increment_read_from();
66         bool read_from_empty();
67         int get_read_from_size();
68         const ModelAction * get_read_from_at(int i);
69
70         void set_promise(unsigned int i);
71         bool get_promise(unsigned int i);
72         bool increment_promise();
73         bool promise_empty();
74
75         void print();
76         void print_may_read_from();
77
78         MEMALLOC
79 private:
80         void explore(thread_id_t tid);
81
82         ModelAction *action;
83         Node *parent;
84         int num_threads;
85         std::vector< bool, MyAlloc<bool> > explored_children;
86         std::vector< bool, MyAlloc<bool> > backtrack;
87         int numBacktracks;
88
89         /** The set of ModelActions that this the action at this Node may read
90          *  from. Only meaningful if this Node represents a 'read' action. */
91         std::vector< const ModelAction *, MyAlloc< const ModelAction * > > may_read_from;
92
93         unsigned int read_from_index;
94
95         std::vector< uint64_t, MyAlloc< uint64_t > > future_values;
96         std::vector< promise_t, MyAlloc<promise_t> > promises;
97         unsigned int future_index;
98 };
99
100 typedef std::list< Node *, MyAlloc< Node * > > node_list_t;
101
102 /**
103  * @brief A stack of nodes
104  *
105  * Holds a Node linked-list that can be used for holding backtracking,
106  * may-read-from, and replay information. It is used primarily as a
107  * stack-like structure, in that backtracking points and replay nodes are
108  * only removed from the top (most recent).
109  */
110 class NodeStack {
111 public:
112         NodeStack();
113         ~NodeStack();
114         ModelAction * explore_action(ModelAction *act);
115         Node * get_head();
116         Node * get_next();
117         void reset_execution();
118         void pop_restofstack(int numAhead);
119         int get_total_nodes() { return total_nodes; }
120
121         void print();
122
123         MEMALLOC
124 private:
125         node_list_t node_list;
126         node_list_t::iterator iter;
127
128         int total_nodes;
129 };
130
131 #endif /* __NODESTACK_H__ */