model: bugfix - infinite loop in resolve_release_sequences()
[c11tester.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 #include "clockvector.h"
14
15 class ModelAction;
16
17 /**
18  * A flag used for the promise counting/combination problem within a node,
19  * denoting whether a particular Promise is
20  * <ol><li>@b applicable: can be satisfied by this Node's ModelAction and</li>
21  * <li>@b fulfilled: satisfied by this Node's ModelAction under the current
22  * configuration.</li></ol>
23  */
24 typedef enum {
25         PROMISE_IGNORE = 0, /**< This promise is inapplicable; ignore it */
26         PROMISE_UNFULFILLED, /**< This promise is applicable but unfulfilled */
27         PROMISE_FULFILLED /**< This promise is applicable and fulfilled */
28 } promise_t;
29
30 struct future_value {
31         uint64_t value;
32         modelclock_t expiration;
33 };
34
35
36 /**
37  * @brief A single node in a NodeStack
38  *
39  * Represents a single node in the NodeStack. Each Node is associated with up
40  * to one action and up to one parent node. A node holds information
41  * regarding the last action performed (the "associated action"), the thread
42  * choices that have been explored (explored_children) and should be explored
43  * (backtrack), and the actions that the last action may read from.
44  */
45 class Node {
46 public:
47         Node(ModelAction *act = NULL, Node *par = NULL, int nthreads = 1, bool *enabled_array = NULL);
48         ~Node();
49         /* return true = thread choice has already been explored */
50         bool has_been_explored(thread_id_t tid);
51         /* return true = backtrack set is empty */
52         bool backtrack_empty();
53
54         void explore_child(ModelAction *act);
55         /* return false = thread was already in backtrack */
56         bool set_backtrack(thread_id_t id);
57         thread_id_t get_next_backtrack();
58         bool is_enabled(Thread *t);
59         bool is_enabled(thread_id_t tid);
60         ModelAction * get_action() { return action; }
61
62         /** @return the parent Node to this Node; that is, the action that
63          * occurred previously in the stack. */
64         Node * get_parent() const { return parent; }
65
66         bool add_future_value(uint64_t value, modelclock_t expiration);
67         uint64_t get_future_value();
68         modelclock_t get_future_value_expiration();
69         bool increment_future_value();
70         bool future_value_empty();
71
72         void add_read_from(const ModelAction *act);
73         const ModelAction * get_read_from();
74         bool increment_read_from();
75         bool read_from_empty();
76         int get_read_from_size();
77         const ModelAction * get_read_from_at(int i);
78
79         void set_promise(unsigned int i);
80         bool get_promise(unsigned int i);
81         bool increment_promise();
82         bool promise_empty();
83
84         void print();
85         void print_may_read_from();
86
87         MEMALLOC
88 private:
89         void explore(thread_id_t tid);
90
91         ModelAction *action;
92         Node *parent;
93         int num_threads;
94         std::vector< bool, MyAlloc<bool> > explored_children;
95         std::vector< bool, MyAlloc<bool> > backtrack;
96         int numBacktracks;
97         bool *enabled_array;
98
99         /** The set of ModelActions that this the action at this Node may read
100          *  from. Only meaningful if this Node represents a 'read' action. */
101         std::vector< const ModelAction *, MyAlloc< const ModelAction * > > may_read_from;
102
103         unsigned int read_from_index;
104
105         std::vector< struct future_value, MyAlloc<struct future_value> > future_values;
106         std::vector< promise_t, MyAlloc<promise_t> > promises;
107         unsigned int future_index;
108 };
109
110 typedef std::list< Node *, MyAlloc< Node * > > node_list_t;
111
112 /**
113  * @brief A stack of nodes
114  *
115  * Holds a Node linked-list that can be used for holding backtracking,
116  * may-read-from, and replay information. It is used primarily as a
117  * stack-like structure, in that backtracking points and replay nodes are
118  * only removed from the top (most recent).
119  */
120 class NodeStack {
121 public:
122         NodeStack();
123         ~NodeStack();
124         ModelAction * explore_action(ModelAction *act, bool * is_enabled);
125         Node * get_head();
126         Node * get_next();
127         void reset_execution();
128         void pop_restofstack(int numAhead);
129         int get_total_nodes() { return total_nodes; }
130
131         void print();
132
133         MEMALLOC
134 private:
135         node_list_t node_list;
136         node_list_t::iterator iter;
137
138         int total_nodes;
139 };
140
141 #endif /* __NODESTACK_H__ */