Merge branch 'master' of ssh://demsky.eecs.uci.edu/home/git/model-checker
[cdsspec-compiler.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 /**
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         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(ModelAction *act);
48
49         void print();
50
51         MEMALLOC
52 private:
53         void explore(thread_id_t tid);
54
55         ModelAction *action;
56         Node *parent;
57         int num_threads;
58         std::vector< bool, MyAlloc<bool> > explored_children;
59         std::vector< bool, MyAlloc<bool> > backtrack;
60         int numBacktracks;
61
62         /** The set of ModelActions that this the action at this Node may read
63          *  from. Only meaningful if this Node represents a 'read' action. */
64         action_set_t may_read_from;
65 };
66
67 typedef std::list<class Node *, MyAlloc< class Node * > > node_list_t;
68
69 /**
70  * @brief A stack of nodes
71  *
72  * Holds a Node linked-list that can be used for holding backtracking,
73  * may-read-from, and replay information. It is used primarily as a
74  * stack-like structure, in that backtracking points and replay nodes are
75  * only removed from the top (most recent).
76  */
77 class NodeStack {
78 public:
79         NodeStack();
80         ~NodeStack();
81         ModelAction * explore_action(ModelAction *act);
82         Node * get_head();
83         Node * get_next();
84         void reset_execution();
85
86         int get_total_nodes() { return total_nodes; }
87
88         void print();
89
90         MEMALLOC
91 private:
92         node_list_t node_list;
93         node_list_t::iterator iter;
94
95         int total_nodes;
96 };
97
98 #endif /* __NODESTACK_H__ */