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