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