model: include <cstddef>
[model-checker.git] / model.h
1 #ifndef __MODEL_H__
2 #define __MODEL_H__
3
4 #include <list>
5 #include <map>
6 #include <cstddef>
7
8 #include "schedule.h"
9 #include "libthreads.h"
10 #include "libatomic.h"
11 #include "threads.h"
12 #include "tree.h"
13
14 #define VALUE_NONE -1
15
16 typedef enum action_type {
17         THREAD_CREATE,
18         THREAD_YIELD,
19         THREAD_JOIN,
20         ATOMIC_READ,
21         ATOMIC_WRITE
22 } action_type_t;
23
24 typedef std::list<class ModelAction *> action_list_t;
25
26 class ModelAction {
27 public:
28         ModelAction(action_type_t type, memory_order order, void *loc, int value);
29         void print(void);
30
31         thread_id_t get_tid() { return tid; }
32         action_type get_type() { return type; }
33         memory_order get_mo() { return order; }
34         void *get_location() { return location; }
35
36         TreeNode *get_node() { return node; }
37         void set_node(TreeNode *n) { node = n; }
38 private:
39         action_type type;
40         memory_order order;
41         void *location;
42         thread_id_t tid;
43         int value;
44         TreeNode *node;
45 };
46
47 class Backtrack {
48 public:
49         Backtrack(ModelAction *d, action_list_t *t) {
50                 diverge = d;
51                 actionTrace = t;
52                 //currentIterator = actionTrace->getFirst();
53         }
54         ModelAction *get_diverge() { return diverge; }
55         action_list_t *get_trace() { return actionTrace; }
56 private:
57         ModelAction *diverge;
58         /* unused for now; will be used when re-exploring this path? */
59         //MyListElement *currentIterator;
60         action_list_t *actionTrace;
61 };
62
63 class ModelChecker {
64 public:
65         ModelChecker();
66         ~ModelChecker();
67         class Scheduler *scheduler;
68         Thread *system_thread;
69
70         void add_system_thread(Thread *t);
71
72         void set_current_action(ModelAction *act) { current_action = act; }
73         ModelAction *get_last_conflict(ModelAction *act);
74         void check_current_action(void);
75         void set_backtracking(ModelAction *act);
76         void print_trace(void);
77
78         int add_thread(Thread *t);
79         Thread *get_thread(thread_id_t tid) { return thread_map[tid]; }
80
81         void assign_id(Thread *t);
82
83         int switch_to_master(ModelAction *act);
84 private:
85         int used_thread_id;
86         class ModelAction *current_action;
87         action_list_t *action_trace;
88         std::map<thread_id_t, class Thread *> thread_map;
89         class TreeNode *rootNode, *currentNode;
90         std::list<class Backtrack *> backtrack_list;
91 };
92
93 extern ModelChecker *model;
94
95 int thread_switch_to_master(ModelAction *act);
96
97 #endif /* __MODEL_H__ */