nodestack: add Node::get_parent() function
[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 <set>
11 #include <cstddef>
12 #include "threads.h"
13 #include "mymemory.h"
14
15 class ModelAction;
16
17 typedef std::set< ModelAction *, std::less< ModelAction *>, MyAlloc< ModelAction * > > action_set_t;
18
19 class Node {
20 public:
21         Node(ModelAction *act = NULL, Node *par = NULL, int nthreads = 1);
22         ~Node();
23         /* return true = thread choice has already been explored */
24         bool has_been_explored(thread_id_t tid);
25         /* return true = backtrack set is empty */
26         bool backtrack_empty();
27         void explore_child(ModelAction *act);
28         /* return false = thread was already in backtrack */
29         bool set_backtrack(thread_id_t id);
30         thread_id_t get_next_backtrack();
31         bool is_enabled(Thread *t);
32         ModelAction * get_action() { return action; }
33
34         /** @return the parent Node to this Node; that is, the action that
35          * occurred previously in the stack. */
36         Node * get_parent() const { return parent; }
37
38         void add_read_from(ModelAction *act);
39
40         void print();
41
42         MEMALLOC
43 private:
44         void explore(thread_id_t tid);
45
46         ModelAction *action;
47         Node *parent;
48         int num_threads;
49         std::vector< bool, MyAlloc<bool> > explored_children;
50         std::vector< bool, MyAlloc<bool> > backtrack;
51         int numBacktracks;
52
53         /** The set of ModelActions that this the action at this Node may read
54          *  from. Only meaningful if this Node represents a 'read' action. */
55         action_set_t may_read_from;
56 };
57
58 typedef std::list<class Node *, MyAlloc< class Node * > > node_list_t;
59
60 class NodeStack {
61 public:
62         NodeStack();
63         ~NodeStack();
64         ModelAction * explore_action(ModelAction *act);
65         Node * get_head();
66         Node * get_next();
67         void reset_execution();
68
69         int get_total_nodes() { return total_nodes; }
70
71         void print();
72
73         MEMALLOC
74 private:
75         node_list_t node_list;
76         node_list_t::iterator iter;
77
78         int total_nodes;
79 };
80
81 #endif /* __NODESTACK_H__ */