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),
24 eliminate_thread(read->get_tid());
28 * Add a reader that reads from this Promise. Must be added in an order
29 * consistent with execution order.
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
34 bool Promise::add_reader(ModelAction *reader)
36 readers.push_back(reader);
37 return eliminate_thread(reader->get_tid());
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.
44 * @param i The index of the reader to return
45 * @return The i'th reader of this Promise
47 ModelAction * Promise::get_reader(unsigned int i) const
49 return i < readers.size() ? readers[i] : NULL;
53 * Eliminate a thread which no longer can satisfy this promise. Once all
54 * enabled threads have been eliminated, this promise is unresolvable.
56 * @param tid The thread ID of the thread to eliminate
57 * @return True, if this elimination has invalidated the promise; false
60 bool Promise::eliminate_thread(thread_id_t tid)
62 unsigned int id = id_to_int(tid);
63 if (!thread_is_available(tid))
66 available_thread[id] = false;
67 num_available_threads--;
72 * Add a thread which may resolve this promise
74 * @param tid The thread ID
76 void Promise::add_thread(thread_id_t tid)
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++;
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.
92 * @param tid Thread ID of the thread to check
93 * @return True if the thread is available; false otherwise
95 bool Promise::thread_is_available(thread_id_t tid) const
97 unsigned int id = id_to_int(tid);
98 if (id >= available_thread.size())
100 return available_thread[id];
104 * @brief Get an upper bound on the number of available threads
106 * Gets an upper bound on the number of threads in the available threads set,
107 * useful for iterating over "thread_is_available()".
109 * @return The upper bound
111 unsigned int Promise::max_available_thread_idx() const
113 return available_thread.size();
116 /** @brief Print debug info about the Promise */
117 void Promise::print() const
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()));
122 for (unsigned int i = 0; i < available_thread.size(); i++)
123 if (available_thread[i]) {
124 model_print("[%d]", i);
128 model_print("(none)");
133 * Check if this promise has failed. A promise can fail when all threads which
134 * could possibly satisfy the promise have been eliminated.
136 * @return True, if this promise has failed; false otherwise
138 bool Promise::has_failed() const
140 return num_available_threads == 0;
144 * @brief Check if an action's thread and location are compatible for resolving
146 * @param act The action to check against
147 * @return True if we are compatible; false otherwise
149 bool Promise::is_compatible(const ModelAction *act) const
151 return thread_is_available(act->get_tid()) && get_reader(0)->same_var(act);
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
160 bool Promise::is_compatible_exclusive(const ModelAction *act) const
162 return get_num_available_threads() == 1 && is_compatible(act);
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
170 bool Promise::same_value(const ModelAction *write) const
172 return get_value() == write->get_write_value();
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
180 bool Promise::same_location(const ModelAction *act) const
182 return get_reader(0)->same_var(act);
185 /** @brief Get this Promise's index within the execution's promise array */
186 int Promise::get_index() const
188 return execution->get_promise_number(this);