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