41dc44d34a8a0626e1a82de4bb59023e88aaa1e3
[model-checker.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 DefaultScheduler();
15
16         this->current_action = NULL;
17 }
18
19 ModelChecker::~ModelChecker()
20 {
21         delete this->scheduler;
22 }
23
24 void ModelChecker::assign_id(struct thread *t)
25 {
26         t->id = ++this->used_thread_id;
27 }
28
29 void ModelChecker::add_system_thread(struct thread *t)
30 {
31         this->system_thread = t;
32 }
33
34 void ModelChecker::check_current_action(void)
35 {
36         if (this->current_action)
37                 this->action_trace.push_back(this->current_action);
38         else
39                 DEBUG("trying to push NULL action...\n");
40 }
41
42 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc, int value)
43 {
44         struct thread *t = thread_current();
45         ModelAction *act = this;
46
47         act->type = type;
48         act->order = order;
49         act->location = loc;
50         act->tid = t->id;
51         act->value = value;
52 }
53
54 void ModelAction::print(void)
55 {
56         const char *type_str;
57         switch (this->type) {
58         case THREAD_CREATE:
59                 type_str = "thread create";
60                 break;
61         case THREAD_YIELD:
62                 type_str = "thread yield";
63                 break;
64         case THREAD_JOIN:
65                 type_str = "thread join";
66                 break;
67         case ATOMIC_READ:
68                 type_str = "atomic read";
69                 break;
70         case ATOMIC_WRITE:
71                 type_str = "atomic write";
72                 break;
73         default:
74                 type_str = "unknown type";
75         }
76
77         printf("Thread: %d\tAction: %s\tMO: %d\tLoc: %#014zx\tValue: %d\n", tid, type_str, order, (size_t)location, value);
78 }