model: use TreeNode()
[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         if (this->current_action)
41                 this->action_trace.push_back(this->current_action);
42         else
43                 DEBUG("trying to push NULL action...\n");
44 }
45
46 void ModelChecker::print_trace(void)
47 {
48         std::list<class ModelAction *>::iterator it;
49
50         for (it = action_trace.begin(); it != action_trace.end(); it++) {
51                 DBG();
52                 (*it)->print();
53         }
54 }
55
56 int ModelChecker::add_thread(Thread *t)
57 {
58         thread_map[t->get_id()] = t;
59         return 0;
60 }
61
62 int ModelChecker::switch_to_master(ModelAction *act)
63 {
64         Thread *old, *next;
65
66         DBG();
67         old = thread_current();
68         set_current_action(act);
69         old->set_state(THREAD_READY);
70         next = system_thread;
71         return old->swap(next);
72 }
73
74 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc, int value)
75 {
76         Thread *t = thread_current();
77         ModelAction *act = this;
78
79         act->type = type;
80         act->order = order;
81         act->location = loc;
82         act->tid = t->get_id();
83         act->value = value;
84 }
85
86 void ModelAction::print(void)
87 {
88         const char *type_str;
89         switch (this->type) {
90         case THREAD_CREATE:
91                 type_str = "thread create";
92                 break;
93         case THREAD_YIELD:
94                 type_str = "thread yield";
95                 break;
96         case THREAD_JOIN:
97                 type_str = "thread join";
98                 break;
99         case ATOMIC_READ:
100                 type_str = "atomic read";
101                 break;
102         case ATOMIC_WRITE:
103                 type_str = "atomic write";
104                 break;
105         default:
106                 type_str = "unknown type";
107         }
108
109         printf("Thread: %d\tAction: %s\tMO: %d\tLoc: %#014zx\tValue: %d\n", tid, type_str, order, (size_t)location, value);
110 }