promise: move constructor out of header
[model-checker.git] / promise.cc
1 #define __STDC_FORMAT_MACROS
2 #include <inttypes.h>
3
4 #include "promise.h"
5 #include "model.h"
6 #include "schedule.h"
7
8 /**
9  * @brief Promise constructor
10  * @param read The read which reads from a promised future value
11  * @param fv The future value that is promised
12  */
13 Promise::Promise(ModelAction *read, struct future_value fv) :
14         num_available_threads(0),
15         value(fv.value),
16         expiration(fv.expiration),
17         read(read),
18         write(NULL)
19 {
20         add_thread(fv.tid);
21         eliminate_thread(read->get_tid());
22 }
23
24 /**
25  * Eliminate a thread which no longer can satisfy this promise. Once all
26  * enabled threads have been eliminated, this promise is unresolvable.
27  *
28  * @param tid The thread ID of the thread to eliminate
29  * @return True, if this elimination has invalidated the promise; false
30  * otherwise
31  */
32 bool Promise::eliminate_thread(thread_id_t tid)
33 {
34         unsigned int id = id_to_int(tid);
35         if (!thread_is_available(tid))
36                 return false;
37
38         available_thread[id] = false;
39         num_available_threads--;
40         return has_failed();
41 }
42
43 /**
44  * Add a thread which may resolve this promise
45  *
46  * @param tid The thread ID
47  */
48 void Promise::add_thread(thread_id_t tid)
49 {
50         unsigned int id = id_to_int(tid);
51         if (id >= available_thread.size())
52                 available_thread.resize(id + 1, false);
53         if (!available_thread[id]) {
54                 available_thread[id] = true;
55                 num_available_threads++;
56         }
57 }
58
59 /**
60  * Check if a thread is available for resolving this promise. That is, the
61  * thread must have been previously marked for resolving this promise, and it
62  * cannot have been eliminated due to synchronization, etc.
63  *
64  * @param tid Thread ID of the thread to check
65  * @return True if the thread is available; false otherwise
66  */
67 bool Promise::thread_is_available(thread_id_t tid) const
68 {
69         unsigned int id = id_to_int(tid);
70         if (id >= available_thread.size())
71                 return false;
72         return available_thread[id];
73 }
74
75 /** @brief Print debug info about the Promise */
76 void Promise::print() const
77 {
78         model_print("Promised value %#" PRIx64 ", read from thread %d, available threads to resolve: ", value, id_to_int(read->get_tid()));
79         for (unsigned int i = 0; i < available_thread.size(); i++)
80                 if (available_thread[i])
81                         model_print("[%d]", i);
82         model_print("\n");
83 }
84
85 /**
86  * Check if this promise has failed. A promise can fail when all threads which
87  * could possibly satisfy the promise have been eliminated.
88  *
89  * @return True, if this promise has failed; false otherwise
90  */
91 bool Promise::has_failed() const
92 {
93         return num_available_threads == 0;
94 }
95
96 /**
97  * @brief Check if an action's thread and location are compatible for resolving
98  * this promise
99  * @param act The action to check against
100  * @return True if we are compatible; false otherwise
101  */
102 bool Promise::is_compatible(const ModelAction *act) const
103 {
104         return thread_is_available(act->get_tid()) && read->same_var(act);
105 }
106
107 /**
108  * @brief Check if an action's thread and location are compatible for resolving
109  * this promise, and that the promise is thread-exclusive
110  * @param act The action to check against
111  * @return True if we are compatible and exclusive; false otherwise
112  */
113 bool Promise::is_compatible_exclusive(const ModelAction *act) const
114 {
115         return get_num_available_threads() == 1 && is_compatible(act);
116 }