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