df86090a2335ff3e17c69d33a98c9d370903f199
[model-checker.git] / promise.cc
1 #define __STDC_FORMAT_MACROS
2 #include <inttypes.h>
3
4 #include "promise.h"
5 #include "execution.h"
6 #include "schedule.h"
7 #include "action.h"
8 #include "threads-model.h"
9
10 /**
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
15  */
16 Promise::Promise(const ModelExecution *execution, ModelAction *read, struct future_value fv) :
17         execution(execution),
18         num_available_threads(0),
19         fv(fv),
20         readers(1, read),
21         write(NULL)
22 {
23         add_thread(fv.tid);
24         eliminate_thread(read->get_tid());
25 }
26
27 /**
28  * Add a reader that reads from this Promise. Must be added in an order
29  * consistent with execution order.
30  *
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
33  */
34 bool Promise::add_reader(ModelAction *reader)
35 {
36         readers.push_back(reader);
37         return eliminate_thread(reader->get_tid());
38 }
39
40 /**
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.
43  *
44  * @param i The index of the reader to return
45  * @return The i'th reader of this Promise
46  */
47 ModelAction * Promise::get_reader(unsigned int i) const
48 {
49         return i < readers.size() ? readers[i] : NULL;
50 }
51
52 /**
53  * Eliminate a thread which no longer can satisfy this promise. Once all
54  * enabled threads have been eliminated, this promise is unresolvable.
55  *
56  * @param tid The thread ID of the thread to eliminate
57  * @return True, if this elimination has invalidated the promise; false
58  * otherwise
59  */
60 bool Promise::eliminate_thread(thread_id_t tid)
61 {
62         unsigned int id = id_to_int(tid);
63         if (!thread_is_available(tid))
64                 return false;
65
66         available_thread[id] = false;
67         num_available_threads--;
68         return has_failed();
69 }
70
71 /**
72  * Add a thread which may resolve this promise
73  *
74  * @param tid The thread ID
75  */
76 void Promise::add_thread(thread_id_t tid)
77 {
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++;
84         }
85 }
86
87 /**
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.
91  *
92  * @param tid Thread ID of the thread to check
93  * @return True if the thread is available; false otherwise
94  */
95 bool Promise::thread_is_available(thread_id_t tid) const
96 {
97         unsigned int id = id_to_int(tid);
98         if (id >= available_thread.size())
99                 return false;
100         return available_thread[id];
101 }
102
103 /** @brief Print debug info about the Promise */
104 void Promise::print() const
105 {
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()));
108         bool failed = true;
109         for (unsigned int i = 0; i < available_thread.size(); i++)
110                 if (available_thread[i]) {
111                         model_print("[%d]", i);
112                         failed = false;
113                 }
114         if (failed)
115                 model_print("(none)");
116         model_print("\n");
117 }
118
119 /**
120  * Check if this promise has failed. A promise can fail when all threads which
121  * could possibly satisfy the promise have been eliminated.
122  *
123  * @return True, if this promise has failed; false otherwise
124  */
125 bool Promise::has_failed() const
126 {
127         return num_available_threads == 0;
128 }
129
130 /**
131  * @brief Check if an action's thread and location are compatible for resolving
132  * this promise
133  * @param act The action to check against
134  * @return True if we are compatible; false otherwise
135  */
136 bool Promise::is_compatible(const ModelAction *act) const
137 {
138         return thread_is_available(act->get_tid()) && get_reader(0)->same_var(act);
139 }
140
141 /**
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
146  */
147 bool Promise::is_compatible_exclusive(const ModelAction *act) const
148 {
149         return get_num_available_threads() == 1 && is_compatible(act);
150 }
151
152 /**
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
156  */
157 bool Promise::same_value(const ModelAction *write) const
158 {
159         return get_value() == write->get_write_value();
160 }
161
162 /**
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
166  */
167 bool Promise::same_location(const ModelAction *act) const
168 {
169         return get_reader(0)->same_var(act);
170 }
171
172 /** @brief Get this Promise's index within the execution's promise array */
173 int Promise::get_index() const
174 {
175         return execution->get_promise_number(this);
176 }