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