1 #define __STDC_FORMAT_MACROS
8 #include "threads-model.h"
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
16 Promise::Promise(const ModelExecution *execution, ModelAction *read, struct future_value fv) :
18 num_available_threads(0),
19 num_was_available_threads(0),
25 eliminate_thread(read->get_tid());
29 * Add a reader that reads from this Promise. Must be added in an order
30 * consistent with execution order.
32 * @param reader The ModelAction that reads from this promise. Must be a read.
33 * @return True if this new reader has invalidated the promise; false otherwise
35 bool Promise::add_reader(ModelAction *reader)
37 readers.push_back(reader);
38 return eliminate_thread(reader->get_tid());
42 * Access a reader that read from this Promise. Readers must be inserted in
43 * order by execution order, so they can be returned in this order.
45 * @param i The index of the reader to return
46 * @return The i'th reader of this Promise
48 ModelAction * Promise::get_reader(unsigned int i) const
50 return i < readers.size() ? readers[i] : NULL;
54 * Eliminate a thread which no longer can satisfy this promise. Once all
55 * enabled threads have been eliminated, this promise is unresolvable.
57 * @param tid The thread ID of the thread to eliminate
58 * @return True, if this elimination has invalidated the promise; false
61 bool Promise::eliminate_thread(thread_id_t tid)
63 unsigned int id = id_to_int(tid);
64 if (!thread_is_available(tid))
67 available_thread[id] = false;
68 num_available_threads--;
73 * Add a thread which may resolve this promise
75 * @param tid The thread ID
77 void Promise::add_thread(thread_id_t tid)
79 unsigned int id = id_to_int(tid);
80 if (id >= available_thread.size())
81 available_thread.resize(id + 1, false);
82 if (!available_thread[id]) {
83 available_thread[id] = true;
84 num_available_threads++;
86 if (id >= was_available_thread.size())
87 was_available_thread.resize(id + 1, false);
88 if (!was_available_thread[id]) {
89 was_available_thread[id] = true;
90 num_was_available_threads++;
95 * Check if a thread is available for resolving this promise. That is, the
96 * thread must have been previously marked for resolving this promise, and it
97 * cannot have been eliminated due to synchronization, etc.
99 * @param tid Thread ID of the thread to check
100 * @return True if the thread is available; false otherwise
102 bool Promise::thread_is_available(thread_id_t tid) const
104 unsigned int id = id_to_int(tid);
105 if (id >= available_thread.size())
107 return available_thread[id];
110 bool Promise::thread_was_available(thread_id_t tid) const
112 unsigned int id = id_to_int(tid);
113 if (id >= was_available_thread.size())
115 return was_available_thread[id];
119 * @brief Get an upper bound on the number of available threads
121 * Gets an upper bound on the number of threads in the available threads set,
122 * useful for iterating over "thread_is_available()".
124 * @return The upper bound
126 unsigned int Promise::max_available_thread_idx() const
128 return available_thread.size();
131 /** @brief Print debug info about the Promise */
132 void Promise::print() const
134 model_print("Promised value %#" PRIx64 ", first read from thread %d, available threads to resolve: ",
135 fv.value, id_to_int(get_reader(0)->get_tid()));
137 for (unsigned int i = 0; i < available_thread.size(); i++)
138 if (available_thread[i]) {
139 model_print("[%d]", i);
143 model_print("(none)");
148 * Check if this promise has failed. A promise can fail when all threads which
149 * could possibly satisfy the promise have been eliminated.
151 * @return True, if this promise has failed; false otherwise
153 bool Promise::has_failed() const
155 return num_available_threads == 0;
159 * @brief Check if an action's thread and location are compatible for resolving
161 * @param act The action to check against
162 * @return True if we are compatible; false otherwise
164 bool Promise::is_compatible(const ModelAction *act) const
166 return thread_is_available(act->get_tid()) && get_reader(0)->same_var(act);
170 * @brief Check if an action's thread and location are compatible for resolving
171 * this promise, and that the promise is thread-exclusive
172 * @param act The action to check against
173 * @return True if we are compatible and exclusive; false otherwise
175 bool Promise::is_compatible_exclusive(const ModelAction *act) const
177 return get_num_available_threads() == 1 && is_compatible(act);
181 * @brief Check if a store's value matches this Promise
182 * @param write The store to check
183 * @return True if the store's written value matches this Promise
185 bool Promise::same_value(const ModelAction *write) const
187 return get_value() == write->get_write_value();
191 * @brief Check if a ModelAction's location matches this Promise
192 * @param act The ModelAction to check
193 * @return True if the action's location matches this Promise
195 bool Promise::same_location(const ModelAction *act) const
197 return get_reader(0)->same_var(act);
200 /** @brief Get this Promise's index within the execution's promise array */
201 int Promise::get_index() const
203 return execution->get_promise_number(this);