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];
103 /** @brief Print debug info about the Promise */
104 void Promise::print() const
106 model_print("Promised value %#" PRIx64 ", first read from thread %d, available threads to resolve: ",
107 fv.value, id_to_int(get_reader(0)->get_tid()));
109 for (unsigned int i = 0; i < available_thread.size(); i++)
110 if (available_thread[i]) {
111 model_print("[%d]", i);
115 model_print("(none)");
120 * Check if this promise has failed. A promise can fail when all threads which
121 * could possibly satisfy the promise have been eliminated.
123 * @return True, if this promise has failed; false otherwise
125 bool Promise::has_failed() const
127 return num_available_threads == 0;
131 * @brief Check if an action's thread and location are compatible for resolving
133 * @param act The action to check against
134 * @return True if we are compatible; false otherwise
136 bool Promise::is_compatible(const ModelAction *act) const
138 return thread_is_available(act->get_tid()) && get_reader(0)->same_var(act);
142 * @brief Check if an action's thread and location are compatible for resolving
143 * this promise, and that the promise is thread-exclusive
144 * @param act The action to check against
145 * @return True if we are compatible and exclusive; false otherwise
147 bool Promise::is_compatible_exclusive(const ModelAction *act) const
149 return get_num_available_threads() == 1 && is_compatible(act);
153 * @brief Check if a store's value matches this Promise
154 * @param write The store to check
155 * @return True if the store's written value matches this Promise
157 bool Promise::same_value(const ModelAction *write) const
159 return get_value() == write->get_write_value();
163 * @brief Check if a ModelAction's location matches this Promise
164 * @param act The ModelAction to check
165 * @return True if the action's location matches this Promise
167 bool Promise::same_location(const ModelAction *act) const
169 return get_reader(0)->same_var(act);
172 /** @brief Get this Promise's index within the execution's promise array */
173 int Promise::get_index() const
175 return execution->get_promise_number(this);