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