add support for pthread_mutex
[c11tester.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         num_was_available_threads(0),
20         fv(fv),
21         readers(1, read),
22         write(NULL)
23 {
24         add_thread(fv.tid);
25         eliminate_thread(read->get_tid());
26 }
27
28 /**
29  * Add a reader that reads from this Promise. Must be added in an order
30  * consistent with execution order.
31  *
32  * @param reader The ModelAction that reads from this promise. Must be a read.
33  * @return True if this new reader has invalidated the promise; false otherwise
34  */
35 bool Promise::add_reader(ModelAction *reader)
36 {
37         readers.push_back(reader);
38         return eliminate_thread(reader->get_tid());
39 }
40
41 /**
42  * Access a reader that read from this Promise. Readers must be inserted in
43  * order by execution order, so they can be returned in this order.
44  *
45  * @param i The index of the reader to return
46  * @return The i'th reader of this Promise
47  */
48 ModelAction * Promise::get_reader(unsigned int i) const
49 {
50         return i < readers.size() ? readers[i] : NULL;
51 }
52
53 /**
54  * Eliminate a thread which no longer can satisfy this promise. Once all
55  * enabled threads have been eliminated, this promise is unresolvable.
56  *
57  * @param tid The thread ID of the thread to eliminate
58  * @return True, if this elimination has invalidated the promise; false
59  * otherwise
60  */
61 bool Promise::eliminate_thread(thread_id_t tid)
62 {
63         unsigned int id = id_to_int(tid);
64         if (!thread_is_available(tid))
65                 return false;
66
67         available_thread[id] = false;
68         num_available_threads--;
69         return has_failed();
70 }
71
72 /**
73  * Add a thread which may resolve this promise
74  *
75  * @param tid The thread ID
76  */
77 void Promise::add_thread(thread_id_t tid)
78 {
79         unsigned int id = id_to_int(tid);
80         if (id >= available_thread.size())
81                 available_thread.resize(id + 1, false);
82         if (!available_thread[id]) {
83                 available_thread[id] = true;
84                 num_available_threads++;
85         }
86         if (id >= was_available_thread.size())
87                 was_available_thread.resize(id + 1, false);
88         if (!was_available_thread[id]) {
89                 was_available_thread[id] = true;
90                 num_was_available_threads++;
91         }
92 }
93
94 /**
95  * Check if a thread is available for resolving this promise. That is, the
96  * thread must have been previously marked for resolving this promise, and it
97  * cannot have been eliminated due to synchronization, etc.
98  *
99  * @param tid Thread ID of the thread to check
100  * @return True if the thread is available; false otherwise
101  */
102 bool Promise::thread_is_available(thread_id_t tid) const
103 {
104         unsigned int id = id_to_int(tid);
105         if (id >= available_thread.size())
106                 return false;
107         return available_thread[id];
108 }
109
110 bool Promise::thread_was_available(thread_id_t tid) const
111 {
112         unsigned int id = id_to_int(tid);
113         if (id >= was_available_thread.size())
114                 return false;
115         return was_available_thread[id];
116 }
117
118 /**
119  * @brief Get an upper bound on the number of available threads
120  *
121  * Gets an upper bound on the number of threads in the available threads set,
122  * useful for iterating over "thread_is_available()".
123  *
124  * @return The upper bound
125  */
126 unsigned int Promise::max_available_thread_idx() const
127 {
128         return available_thread.size();
129 }
130
131 /** @brief Print debug info about the Promise */
132 void Promise::print() const
133 {
134         model_print("Promised value %#" PRIx64 ", first read from thread %d, available threads to resolve: ",
135                         fv.value, id_to_int(get_reader(0)->get_tid()));
136         bool failed = true;
137         for (unsigned int i = 0; i < available_thread.size(); i++)
138                 if (available_thread[i]) {
139                         model_print("[%d]", i);
140                         failed = false;
141                 }
142         if (failed)
143                 model_print("(none)");
144         model_print("\n");
145 }
146
147 /**
148  * Check if this promise has failed. A promise can fail when all threads which
149  * could possibly satisfy the promise have been eliminated.
150  *
151  * @return True, if this promise has failed; false otherwise
152  */
153 bool Promise::has_failed() const
154 {
155         return num_available_threads == 0;
156 }
157
158 /**
159  * @brief Check if an action's thread and location are compatible for resolving
160  * this promise
161  * @param act The action to check against
162  * @return True if we are compatible; false otherwise
163  */
164 bool Promise::is_compatible(const ModelAction *act) const
165 {
166         return thread_is_available(act->get_tid()) && get_reader(0)->same_var(act);
167 }
168
169 /**
170  * @brief Check if an action's thread and location are compatible for resolving
171  * this promise, and that the promise is thread-exclusive
172  * @param act The action to check against
173  * @return True if we are compatible and exclusive; false otherwise
174  */
175 bool Promise::is_compatible_exclusive(const ModelAction *act) const
176 {
177         return get_num_available_threads() == 1 && is_compatible(act);
178 }
179
180 /**
181  * @brief Check if a store's value matches this Promise
182  * @param write The store to check
183  * @return True if the store's written value matches this Promise
184  */
185 bool Promise::same_value(const ModelAction *write) const
186 {
187         return get_value() == write->get_write_value();
188 }
189
190 /**
191  * @brief Check if a ModelAction's location matches this Promise
192  * @param act The ModelAction to check
193  * @return True if the action's location matches this Promise
194  */
195 bool Promise::same_location(const ModelAction *act) const
196 {
197         return get_reader(0)->same_var(act);
198 }
199
200 /** @brief Get this Promise's index within the execution's promise array */
201 int Promise::get_index() const
202 {
203         return execution->get_promise_number(this);
204 }