nodestack: register ModelExecution class w/in NodeStack
[cdsspec-compiler.git] / promise.cc
1 #define __STDC_FORMAT_MACROS
2 #include <inttypes.h>
3
4 #include "promise.h"
5 #include "execution.h"
6 #include "schedule.h"
7 #include "action.h"
8 #include "threads-model.h"
9
10 /**
11  * @brief Promise constructor
12  * @param execution The execution which is creating this Promise
13  * @param read The read which reads from a promised future value
14  * @param fv The future value that is promised
15  */
16 Promise::Promise(const ModelExecution *execution, ModelAction *read, struct future_value fv) :
17         execution(execution),
18         num_available_threads(0),
19         fv(fv),
20         readers(1, read),
21         write(NULL)
22 {
23         add_thread(fv.tid);
24         eliminate_thread(read->get_tid());
25 }
26
27 /**
28  * Add a reader that reads from this Promise. Must be added in an order
29  * consistent with execution order.
30  *
31  * @param reader The ModelAction that reads from this promise. Must be a read.
32  * @return True if this new reader has invalidated the promise; false otherwise
33  */
34 bool Promise::add_reader(ModelAction *reader)
35 {
36         readers.push_back(reader);
37         return eliminate_thread(reader->get_tid());
38 }
39
40 /**
41  * Access a reader that read from this Promise. Readers must be inserted in
42  * order by execution order, so they can be returned in this order.
43  *
44  * @param i The index of the reader to return
45  * @return The i'th reader of this Promise
46  */
47 ModelAction * Promise::get_reader(unsigned int i) const
48 {
49         return i < readers.size() ? readers[i] : NULL;
50 }
51
52 /**
53  * Eliminate a thread which no longer can satisfy this promise. Once all
54  * enabled threads have been eliminated, this promise is unresolvable.
55  *
56  * @param tid The thread ID of the thread to eliminate
57  * @return True, if this elimination has invalidated the promise; false
58  * otherwise
59  */
60 bool Promise::eliminate_thread(thread_id_t tid)
61 {
62         unsigned int id = id_to_int(tid);
63         if (!thread_is_available(tid))
64                 return false;
65
66         available_thread[id] = false;
67         num_available_threads--;
68         return has_failed();
69 }
70
71 /**
72  * Add a thread which may resolve this promise
73  *
74  * @param tid The thread ID
75  */
76 void Promise::add_thread(thread_id_t tid)
77 {
78         unsigned int id = id_to_int(tid);
79         if (id >= available_thread.size())
80                 available_thread.resize(id + 1, false);
81         if (!available_thread[id]) {
82                 available_thread[id] = true;
83                 num_available_threads++;
84         }
85 }
86
87 /**
88  * Check if a thread is available for resolving this promise. That is, the
89  * thread must have been previously marked for resolving this promise, and it
90  * cannot have been eliminated due to synchronization, etc.
91  *
92  * @param tid Thread ID of the thread to check
93  * @return True if the thread is available; false otherwise
94  */
95 bool Promise::thread_is_available(thread_id_t tid) const
96 {
97         unsigned int id = id_to_int(tid);
98         if (id >= available_thread.size())
99                 return false;
100         return available_thread[id];
101 }
102
103 /**
104  * @brief Get an upper bound on the number of available threads
105  *
106  * Gets an upper bound on the number of threads in the available threads set,
107  * useful for iterating over "thread_is_available()".
108  *
109  * @return The upper bound
110  */
111 unsigned int Promise::max_available_thread_idx() const
112 {
113         return available_thread.size();
114 }
115
116 /** @brief Print debug info about the Promise */
117 void Promise::print() const
118 {
119         model_print("Promised value %#" PRIx64 ", first read from thread %d, available threads to resolve: ",
120                         fv.value, id_to_int(get_reader(0)->get_tid()));
121         bool failed = true;
122         for (unsigned int i = 0; i < available_thread.size(); i++)
123                 if (available_thread[i]) {
124                         model_print("[%d]", i);
125                         failed = false;
126                 }
127         if (failed)
128                 model_print("(none)");
129         model_print("\n");
130 }
131
132 /**
133  * Check if this promise has failed. A promise can fail when all threads which
134  * could possibly satisfy the promise have been eliminated.
135  *
136  * @return True, if this promise has failed; false otherwise
137  */
138 bool Promise::has_failed() const
139 {
140         return num_available_threads == 0;
141 }
142
143 /**
144  * @brief Check if an action's thread and location are compatible for resolving
145  * this promise
146  * @param act The action to check against
147  * @return True if we are compatible; false otherwise
148  */
149 bool Promise::is_compatible(const ModelAction *act) const
150 {
151         return thread_is_available(act->get_tid()) && get_reader(0)->same_var(act);
152 }
153
154 /**
155  * @brief Check if an action's thread and location are compatible for resolving
156  * this promise, and that the promise is thread-exclusive
157  * @param act The action to check against
158  * @return True if we are compatible and exclusive; false otherwise
159  */
160 bool Promise::is_compatible_exclusive(const ModelAction *act) const
161 {
162         return get_num_available_threads() == 1 && is_compatible(act);
163 }
164
165 /**
166  * @brief Check if a store's value matches this Promise
167  * @param write The store to check
168  * @return True if the store's written value matches this Promise
169  */
170 bool Promise::same_value(const ModelAction *write) const
171 {
172         return get_value() == write->get_write_value();
173 }
174
175 /**
176  * @brief Check if a ModelAction's location matches this Promise
177  * @param act The ModelAction to check
178  * @return True if the action's location matches this Promise
179  */
180 bool Promise::same_location(const ModelAction *act) const
181 {
182         return get_reader(0)->same_var(act);
183 }
184
185 /** @brief Get this Promise's index within the execution's promise array */
186 int Promise::get_index() const
187 {
188         return execution->get_promise_number(this);
189 }