wow, this is a nasty bug...
[model-checker.git] / action.cc
1 #include <stdio.h>
2
3 #include "model.h"
4 #include "action.h"
5
6 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc, int value)
7 {
8         Thread *t = thread_current();
9         ModelAction *act = this;
10
11         act->type = type;
12         act->order = order;
13         act->location = loc;
14         act->tid = t->get_id();
15         act->value = value;
16         act->seq_number = model->get_next_seq_num();
17 }
18
19 bool ModelAction::is_read()
20 {
21         return type == ATOMIC_READ;
22 }
23
24 bool ModelAction::is_write()
25 {
26         return type == ATOMIC_WRITE;
27 }
28
29 bool ModelAction::is_acquire()
30 {
31         switch (order) {
32         case memory_order_acquire:
33         case memory_order_acq_rel:
34         case memory_order_seq_cst:
35                 return true;
36         default:
37                 return false;
38         }
39 }
40
41 bool ModelAction::is_release()
42 {
43         switch (order) {
44         case memory_order_release:
45         case memory_order_acq_rel:
46         case memory_order_seq_cst:
47                 return true;
48         default:
49                 return false;
50         }
51 }
52
53 bool ModelAction::same_var(ModelAction *act)
54 {
55         return true;
56         // TODO: fix stack allocation... return location == act->location;
57 }
58
59 bool ModelAction::same_thread(ModelAction *act)
60 {
61         return tid == act->tid;
62 }
63
64 bool ModelAction::is_dependent(ModelAction *act)
65 {
66         if (!is_read() && !is_write())
67                 return false;
68         if (!act->is_read() && !act->is_write())
69                 return false;
70         if (same_var(act) && !same_thread(act) &&
71                         (is_write() || act->is_write()))
72                 return true;
73         return false;
74 }
75
76 void ModelAction::print(void)
77 {
78         const char *type_str;
79         switch (this->type) {
80         case THREAD_CREATE:
81                 type_str = "thread create";
82                 break;
83         case THREAD_YIELD:
84                 type_str = "thread yield";
85                 break;
86         case THREAD_JOIN:
87                 type_str = "thread join";
88                 break;
89         case ATOMIC_READ:
90                 type_str = "atomic read";
91                 break;
92         case ATOMIC_WRITE:
93                 type_str = "atomic write";
94                 break;
95         default:
96                 type_str = "unknown type";
97         }
98
99         printf("(%4d) Thread: %d\tAction: %s\tMO: %d\tLoc: %14p\tValue: %d\n",
100                         seq_number, id_to_int(tid), type_str, order, location, value);
101 }