cyclegraph: add documentation
[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  * Eliminate a thread which no longer can satisfy this promise. Once all
10  * enabled threads have been eliminated, this promise is unresolvable.
11  *
12  * @param tid The thread ID of the thread to eliminate
13  * @return True, if this elimination has invalidated the promise; false
14  * otherwise
15  */
16 bool Promise::eliminate_thread(thread_id_t tid)
17 {
18         unsigned int id = id_to_int(tid);
19         if (!thread_is_available(tid))
20                 return false;
21
22         available_thread[id] = false;
23         num_available_threads--;
24         return has_failed();
25 }
26
27 /**
28  * Add a thread which may resolve this promise
29  *
30  * @param tid The thread ID
31  */
32 void Promise::add_thread(thread_id_t tid)
33 {
34         unsigned int id = id_to_int(tid);
35         if (id >= available_thread.size())
36                 available_thread.resize(id + 1, false);
37         if (!available_thread[id]) {
38                 available_thread[id] = true;
39                 num_available_threads++;
40         }
41 }
42
43 /**
44  * Check if a thread is available for resolving this promise. That is, the
45  * thread must have been previously marked for resolving this promise, and it
46  * cannot have been eliminated due to synchronization, etc.
47  *
48  * @param tid Thread ID of the thread to check
49  * @return True if the thread is available; false otherwise
50  */
51 bool Promise::thread_is_available(thread_id_t tid) const
52 {
53         unsigned int id = id_to_int(tid);
54         if (id >= available_thread.size())
55                 return false;
56         return available_thread[id];
57 }
58
59 /** @brief Print debug info about the Promise */
60 void Promise::print() const
61 {
62         model_print("Promised value %#" PRIx64 ", read from thread %d, available threads to resolve: ", value, read->get_tid());
63         for (unsigned int i = 0; i < available_thread.size(); i++)
64                 if (available_thread[i])
65                         model_print("[%d]", i);
66         model_print("\n");
67 }
68
69 /**
70  * Check if this promise has failed. A promise can fail when all threads which
71  * could possibly satisfy the promise have been eliminated.
72  *
73  * @return True, if this promise has failed; false otherwise
74  */
75 bool Promise::has_failed() const
76 {
77         return num_available_threads == 0;
78 }
79
80 /**
81  * @param write A store which could satisfy this Promise
82  * @return True if the store can satisfy this Promise; false otherwise
83  */
84 bool Promise::is_compatible(const ModelAction *write) const
85 {
86         return thread_is_available(write->get_tid());
87 }