rmw example works
[model-checker.git] / model.h
1 /** @file model.h
2  *  @brief Core model checker.
3  */
4
5 #ifndef __MODEL_H__
6 #define __MODEL_H__
7
8 #include <list>
9 #include <map>
10 #include <vector>
11 #include <cstddef>
12 #include <ucontext.h>
13
14 #include "schedule.h"
15 #include "mymemory.h"
16 #include "libthreads.h"
17 #include "threads.h"
18 #include "action.h"
19 #include "clockvector.h"
20
21 /* Forward declaration */
22 class NodeStack;
23 class CycleGraph;
24
25 /** @brief The central structure for model-checking */
26 class ModelChecker {
27 public:
28         ModelChecker();
29         ~ModelChecker();
30
31         /** The scheduler to use: tracks the running/ready Threads */
32         Scheduler *scheduler;
33
34         /** Stores the context for the main model-checking system thread (call
35          * once)
36          * @param ctxt The system context structure
37          */
38         void set_system_context(ucontext_t *ctxt) { system_context = ctxt; }
39
40         /** @returns the context for the main model-checking system thread */
41         ucontext_t * get_system_context(void) { return system_context; }
42
43         void check_current_action(void);
44         void print_summary(void);
45         Thread * schedule_next_thread();
46
47         int add_thread(Thread *t);
48         void remove_thread(Thread *t);
49         Thread * get_thread(thread_id_t tid) { return (*thread_map)[id_to_int(tid)]; }
50
51         thread_id_t get_next_id();
52         int get_num_threads();
53         modelclock_t get_next_seq_num();
54
55         int switch_to_master(ModelAction *act);
56         ClockVector * get_cv(thread_id_t tid);
57         bool next_execution();
58         bool isfeasible();
59
60         MEMALLOC
61 private:
62         int next_thread_id;
63         modelclock_t used_sequence_numbers;
64         int num_executions;
65
66         /**
67          * Stores the ModelAction for the current thread action.  Call this
68          * immediately before switching from user- to system-context to pass
69          * data between them.
70          * @param act The ModelAction created by the user-thread action
71          */
72         void set_current_action(ModelAction *act) { current_action = act; }
73
74         ModelAction * get_last_conflict(ModelAction *act);
75         void set_backtracking(ModelAction *act);
76         thread_id_t get_next_replay_thread();
77         ModelAction * get_next_backtrack();
78         void reset_to_initial_state();
79
80         void add_action_to_lists(ModelAction *act);
81         ModelAction * get_last_action(thread_id_t tid);
82         ModelAction * get_parent_action(thread_id_t tid);
83         ModelAction * get_last_seq_cst(const void *location);
84         void build_reads_from_past(ModelAction *curr);
85         ModelAction * process_rmw(ModelAction * curr);
86         void r_modification_order(ModelAction * curr, const ModelAction *rf);
87         void w_modification_order(ModelAction * curr);
88
89
90         ModelAction *current_action;
91         ModelAction *diverge;
92         thread_id_t nextThread;
93
94         ucontext_t *system_context;
95         action_list_t *action_trace;
96         std::map<int, Thread *> *thread_map;
97
98         /** Per-object list of actions. Maps an object (i.e., memory location)
99          * to a trace of all actions performed on the object. */
100         std::map<const void *, action_list_t> *obj_map;
101
102         std::map<void *, std::vector<action_list_t> > *obj_thrd_map;
103         std::vector<ModelAction *> *thrd_last_action;
104         NodeStack *node_stack;
105         ModelAction *next_backtrack;
106         CycleGraph * cyclegraph;
107 };
108
109 extern ModelChecker *model;
110
111 #endif /* __MODEL_H__ */