threads: prepare system to loop over many executions
[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_trace(void);
79         Thread *schedule_next_thread();
80
81         int add_thread(Thread *t);
82         Thread *get_thread(thread_id_t tid) { return thread_map[tid]; }
83
84         void assign_id(Thread *t);
85
86         int switch_to_master(ModelAction *act);
87
88         bool next_execution();
89 private:
90         int used_thread_id;
91
92         ModelAction *get_last_conflict(ModelAction *act);
93         void set_backtracking(ModelAction *act);
94         thread_id_t advance_backtracking_state();
95         thread_id_t get_next_replay_thread();
96
97         class ModelAction *current_action;
98         Backtrack *exploring;
99         thread_id_t nextThread;
100
101         action_list_t *action_trace;
102         std::map<thread_id_t, class Thread *> thread_map;
103         class TreeNode *rootNode, *currentNode;
104         std::list<class Backtrack *> backtrack_list;
105 };
106
107 extern ModelChecker *model;
108
109 int thread_switch_to_master(ModelAction *act);
110
111 #endif /* __MODEL_H__ */