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