80a27aaebae6385842ccda77d1534c4537e789c1
[c11tester.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                 iter = actionTrace->begin();
53         }
54         ModelAction * get_diverge() { return diverge; }
55         action_list_t * get_trace() { return actionTrace; }
56         void advance_state() { iter++; }
57         ModelAction * get_state() {
58                 return iter == actionTrace->end() ? NULL : *iter;
59         }
60 private:
61         ModelAction *diverge;
62         action_list_t *actionTrace;
63         /* points to position in actionTrace as we replay */
64         action_list_t::iterator iter;
65 };
66
67 class ModelChecker {
68 public:
69         ModelChecker();
70         ~ModelChecker();
71         class Scheduler *scheduler;
72         Thread *system_thread;
73
74         void add_system_thread(Thread *t);
75
76         void set_current_action(ModelAction *act) { current_action = act; }
77         void check_current_action(void);
78         void print_summary(void);
79         Thread * schedule_next_thread();
80
81         int add_thread(Thread *t);
82         void remove_thread(Thread *t);
83         Thread * get_thread(thread_id_t tid) { return thread_map[tid]; }
84
85         int get_next_id();
86
87         int switch_to_master(ModelAction *act);
88
89         bool next_execution();
90 private:
91         int used_thread_id;
92         int num_executions;
93
94         ModelAction * get_last_conflict(ModelAction *act);
95         void set_backtracking(ModelAction *act);
96         thread_id_t advance_backtracking_state();
97         thread_id_t get_next_replay_thread();
98         Backtrack * get_next_backtrack();
99         void reset_to_initial_state();
100
101         class ModelAction *current_action;
102         Backtrack *exploring;
103         thread_id_t nextThread;
104
105         action_list_t *action_trace;
106         std::map<thread_id_t, class Thread *> thread_map;
107         class TreeNode *rootNode, *currentNode;
108         std::list<class Backtrack *> backtrack_list;
109 };
110
111 extern ModelChecker *model;
112
113 int thread_switch_to_master(ModelAction *act);
114
115 #endif /* __MODEL_H__ */