Merge branch 'sandbox' (remove finalize())
[c11tester.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() const
30 {
31         return type == ATOMIC_READ;
32 }
33
34 bool ModelAction::is_write() const
35 {
36         return type == ATOMIC_WRITE;
37 }
38
39 bool ModelAction::is_rmw() const
40 {
41         return type == ATOMIC_RMW;
42 }
43
44 bool ModelAction::is_acquire() const
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() const
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() const
69 {
70         return order==memory_order_seq_cst;
71 }
72
73 bool ModelAction::same_var(const ModelAction *act) const
74 {
75         return location == act->location;
76 }
77
78 bool ModelAction::same_thread(const ModelAction *act) const
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(const ModelAction *act) const
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         ASSERT(cv == NULL);
121
122         if (parent)
123                 cv = new ClockVector(parent->cv, this);
124         else
125                 cv = new ClockVector(NULL, this);
126 }
127
128 void ModelAction::read_from(ModelAction *act)
129 {
130         ASSERT(cv);
131         if (act->is_release() && this->is_acquire())
132                 cv->merge(act->cv);
133         value = act->value;
134 }
135
136 /**
137  * Check whether 'this' happens before act, according to the memory-model's
138  * happens before relation. This is checked via the ClockVector constructs.
139  * @return true if this action's thread has synchronized with act's thread
140  * since the execution of act, false otherwise.
141  */
142 bool ModelAction::happens_before(ModelAction *act)
143 {
144         return act->cv->synchronized_since(this);
145 }
146
147 void ModelAction::print(void)
148 {
149         const char *type_str;
150         switch (this->type) {
151         case THREAD_CREATE:
152                 type_str = "thread create";
153                 break;
154         case THREAD_YIELD:
155                 type_str = "thread yield";
156                 break;
157         case THREAD_JOIN:
158                 type_str = "thread join";
159                 break;
160         case ATOMIC_READ:
161                 type_str = "atomic read";
162                 break;
163         case ATOMIC_WRITE:
164                 type_str = "atomic write";
165                 break;
166         case ATOMIC_RMW:
167                 type_str = "atomic rmw";
168                 break;
169         default:
170                 type_str = "unknown type";
171         }
172
173         printf("(%3d) Thread: %-2d    Action: %-13s    MO: %d    Loc: %14p    Value: %d",
174                         seq_number, id_to_int(tid), type_str, order, location, value);
175         if (cv) {
176                 printf("\t");
177                 cv->print();
178         } else
179                 printf("\n");
180 }