Merge branch 'master' of ssh://demsky.eecs.uci.edu/home/git/model-checker
[c11tester.git] / action.cc
1 #include <stdio.h>
2 #define __STDC_FORMAT_MACROS
3 #include <inttypes.h>
4
5 #include "model.h"
6 #include "action.h"
7 #include "clockvector.h"
8 #include "common.h"
9
10 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc, uint64_t value) :
11         type(type),
12         order(order),
13         location(loc),
14         value(value),
15         reads_from(NULL),
16         cv(NULL)
17 {
18         Thread *t = thread_current();
19         this->tid = t->get_id();
20         this->seq_number = model->get_next_seq_num();
21 }
22
23 ModelAction::~ModelAction()
24 {
25         if (cv)
26                 delete cv;
27 }
28
29 bool ModelAction::is_read() const
30 {
31         return type == ATOMIC_READ;
32 }
33
34 bool ModelAction::is_write() const
35 {
36         return type == ATOMIC_WRITE || type == ATOMIC_INIT;
37 }
38
39 bool ModelAction::is_rmw() const
40 {
41         return type == ATOMIC_RMW;
42 }
43
44 bool ModelAction::is_initialization() const
45 {
46         return type == ATOMIC_INIT;
47 }
48
49 bool ModelAction::is_acquire() const
50 {
51         switch (order) {
52         case memory_order_acquire:
53         case memory_order_acq_rel:
54         case memory_order_seq_cst:
55                 return true;
56         default:
57                 return false;
58         }
59 }
60
61 bool ModelAction::is_release() const
62 {
63         switch (order) {
64         case memory_order_release:
65         case memory_order_acq_rel:
66         case memory_order_seq_cst:
67                 return true;
68         default:
69                 return false;
70         }
71 }
72
73 bool ModelAction::is_seqcst() const
74 {
75         return order==memory_order_seq_cst;
76 }
77
78 bool ModelAction::same_var(const ModelAction *act) const
79 {
80         return location == act->location;
81 }
82
83 bool ModelAction::same_thread(const ModelAction *act) const
84 {
85         return tid == act->tid;
86 }
87
88 /** The is_synchronizing method should only explore interleavings if:
89  *  (1) the operations are seq_cst and don't commute or
90  *  (2) the reordering may establish or break a synchronization relation.
91  *  Other memory operations will be dealt with by using the reads_from
92  *  relation.
93  *
94  *  @param act is the action to consider exploring a reordering.
95  *  @return tells whether we have to explore a reordering.
96  */
97
98 bool ModelAction::is_synchronizing(const ModelAction *act) const
99 {
100         //Same thread can't be reordered
101         if (same_thread(act))
102                 return false;
103
104         // Different locations commute
105         if (!same_var(act))
106                 return false;
107
108         // Explore interleavings of seqcst writes to guarantee total order
109         // of seq_cst operations that don't commute
110         if (is_write() && is_seqcst() && act->is_write() && act->is_seqcst())
111                 return true;
112
113         // Explore synchronizing read/write pairs
114         if (is_read() && is_acquire() && act->is_write() && act->is_release())
115                 return true;
116         if (is_write() && is_release() && act->is_read() && act->is_acquire())
117                 return true;
118
119         // Otherwise handle by reads_from relation
120         return false;
121 }
122
123 void ModelAction::create_cv(const ModelAction *parent)
124 {
125         ASSERT(cv == NULL);
126
127         if (parent)
128                 cv = new ClockVector(parent->cv, this);
129         else
130                 cv = new ClockVector(NULL, this);
131 }
132
133 void ModelAction::read_from(const ModelAction *act)
134 {
135         ASSERT(cv);
136         if (act->is_release() && this->is_acquire())
137                 cv->merge(act->cv);
138         reads_from = act;
139         value = act->value;
140 }
141
142 /**
143  * Check whether 'this' happens before act, according to the memory-model's
144  * happens before relation. This is checked via the ClockVector constructs.
145  * @return true if this action's thread has synchronized with act's thread
146  * since the execution of act, false otherwise.
147  */
148 bool ModelAction::happens_before(const ModelAction *act) const
149 {
150         return act->cv->synchronized_since(this);
151 }
152
153 void ModelAction::print(void) const
154 {
155         const char *type_str;
156         switch (this->type) {
157         case THREAD_CREATE:
158                 type_str = "thread create";
159                 break;
160         case THREAD_START:
161                 type_str = "thread start";
162                 break;
163         case THREAD_YIELD:
164                 type_str = "thread yield";
165                 break;
166         case THREAD_JOIN:
167                 type_str = "thread join";
168                 break;
169         case ATOMIC_READ:
170                 type_str = "atomic read";
171                 break;
172         case ATOMIC_WRITE:
173                 type_str = "atomic write";
174                 break;
175         case ATOMIC_RMW:
176                 type_str = "atomic rmw";
177                 break;
178         case ATOMIC_INIT:
179                 type_str = "init atomic";
180                 break;
181         default:
182                 type_str = "unknown type";
183         }
184
185         printf("(%3d) Thread: %-2d    Action: %-13s    MO: %d    Loc: %14p    Value: %-12" PRIu64,
186                         seq_number, id_to_int(tid), type_str, order, location, value);
187         if (reads_from)
188                 printf(" Rf: %d", reads_from->get_seq_number());
189         if (cv) {
190                 printf("\t");
191                 cv->print();
192         } else
193                 printf("\n");
194 }