model: flesh out check_current_action()
[c11tester.git] / model.cc
1 #include <stdio.h>
2
3 #include "model.h"
4 #include "schedule.h"
5 #include "common.h"
6
7 ModelChecker *model;
8
9 ModelChecker::ModelChecker()
10 {
11         /* First thread created (system_thread) will have id 1 */
12         this->used_thread_id = 0;
13         /* Initialize default scheduler */
14         this->scheduler = new Scheduler();
15
16         this->current_action = NULL;
17
18         rootNode = new TreeNode(NULL);
19         currentNode = rootNode;
20 }
21
22 ModelChecker::~ModelChecker()
23 {
24         delete this->scheduler;
25         delete rootNode;
26 }
27
28 void ModelChecker::assign_id(Thread *t)
29 {
30         t->set_id(++used_thread_id);
31 }
32
33 void ModelChecker::add_system_thread(Thread *t)
34 {
35         this->system_thread = t;
36 }
37
38 void ModelChecker::check_current_action(void)
39 {
40         ModelAction *next = this->current_action;
41
42         if (!next) {
43                 DEBUG("trying to push NULL action...\n");
44                 return;
45         }
46         next->set_node(currentNode);
47         set_backtracking(next);
48         currentNode = currentNode->exploreChild(next->get_tid());
49         this->action_trace.push_back(next);
50 }
51
52 void ModelChecker::print_trace(void)
53 {
54         std::list<class ModelAction *>::iterator it;
55
56         for (it = action_trace.begin(); it != action_trace.end(); it++) {
57                 DBG();
58                 (*it)->print();
59         }
60 }
61
62 int ModelChecker::add_thread(Thread *t)
63 {
64         thread_map[t->get_id()] = t;
65         return 0;
66 }
67
68 int ModelChecker::switch_to_master(ModelAction *act)
69 {
70         Thread *old, *next;
71
72         DBG();
73         old = thread_current();
74         set_current_action(act);
75         old->set_state(THREAD_READY);
76         next = system_thread;
77         return old->swap(next);
78 }
79
80 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc, int value)
81 {
82         Thread *t = thread_current();
83         ModelAction *act = this;
84
85         act->type = type;
86         act->order = order;
87         act->location = loc;
88         act->tid = t->get_id();
89         act->value = value;
90 }
91
92 void ModelAction::print(void)
93 {
94         const char *type_str;
95         switch (this->type) {
96         case THREAD_CREATE:
97                 type_str = "thread create";
98                 break;
99         case THREAD_YIELD:
100                 type_str = "thread yield";
101                 break;
102         case THREAD_JOIN:
103                 type_str = "thread join";
104                 break;
105         case ATOMIC_READ:
106                 type_str = "atomic read";
107                 break;
108         case ATOMIC_WRITE:
109                 type_str = "atomic write";
110                 break;
111         default:
112                 type_str = "unknown type";
113         }
114
115         printf("Thread: %d\tAction: %s\tMO: %d\tLoc: %#014zx\tValue: %d\n", tid, type_str, order, (size_t)location, value);
116 }