1 #define __STDC_FORMAT_MACROS
8 #include "threads-model.h"
11 * @brief Promise constructor
12 * @param read The read which reads from a promised future value
13 * @param fv The future value that is promised
15 Promise::Promise(ModelAction *read, struct future_value fv) :
16 num_available_threads(0),
22 eliminate_thread(read->get_tid());
26 * Eliminate a thread which no longer can satisfy this promise. Once all
27 * enabled threads have been eliminated, this promise is unresolvable.
29 * @param tid The thread ID of the thread to eliminate
30 * @return True, if this elimination has invalidated the promise; false
33 bool Promise::eliminate_thread(thread_id_t tid)
35 unsigned int id = id_to_int(tid);
36 if (!thread_is_available(tid))
39 available_thread[id] = false;
40 num_available_threads--;
45 * Add a thread which may resolve this promise
47 * @param tid The thread ID
49 void Promise::add_thread(thread_id_t tid)
51 unsigned int id = id_to_int(tid);
52 if (id >= available_thread.size())
53 available_thread.resize(id + 1, false);
54 if (!available_thread[id]) {
55 available_thread[id] = true;
56 num_available_threads++;
61 * Check if a thread is available for resolving this promise. That is, the
62 * thread must have been previously marked for resolving this promise, and it
63 * cannot have been eliminated due to synchronization, etc.
65 * @param tid Thread ID of the thread to check
66 * @return True if the thread is available; false otherwise
68 bool Promise::thread_is_available(thread_id_t tid) const
70 unsigned int id = id_to_int(tid);
71 if (id >= available_thread.size())
73 return available_thread[id];
76 /** @brief Print debug info about the Promise */
77 void Promise::print() const
79 model_print("Promised value %#" PRIx64 ", read from thread %d, available threads to resolve: ", fv.value, id_to_int(read->get_tid()));
80 for (unsigned int i = 0; i < available_thread.size(); i++)
81 if (available_thread[i])
82 model_print("[%d]", i);
87 * Check if this promise has failed. A promise can fail when all threads which
88 * could possibly satisfy the promise have been eliminated.
90 * @return True, if this promise has failed; false otherwise
92 bool Promise::has_failed() const
94 return num_available_threads == 0;
98 * @brief Check if an action's thread and location are compatible for resolving
100 * @param act The action to check against
101 * @return True if we are compatible; false otherwise
103 bool Promise::is_compatible(const ModelAction *act) const
105 return thread_is_available(act->get_tid()) && read->same_var(act);
109 * @brief Check if an action's thread and location are compatible for resolving
110 * this promise, and that the promise is thread-exclusive
111 * @param act The action to check against
112 * @return True if we are compatible and exclusive; false otherwise
114 bool Promise::is_compatible_exclusive(const ModelAction *act) const
116 return get_num_available_threads() == 1 && is_compatible(act);