Merge branch 'master' of ssh://demsky.eecs.uci.edu/home/git/model-checker
[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 <utility>
17 #include "libthreads.h"
18 #include "libatomic.h"
19 #include "threads.h"
20 #include "action.h"
21
22 /* Forward declaration */
23 class NodeStack;
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         class 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         /**
44          * Stores the ModelAction for the current thread action.  Call this
45          * immediately before switching from user- to system-context to pass
46          * data between them.
47          * @param act The ModelAction created by the user-thread action
48          */
49         void set_current_action(ModelAction *act) { current_action = act; }
50         void check_current_action(void);
51         void print_summary(void);
52         Thread * schedule_next_thread();
53
54         int add_thread(Thread *t);
55         void remove_thread(Thread *t);
56         Thread * get_thread(thread_id_t tid) { return (*thread_map)[id_to_int(tid)]; }
57
58         thread_id_t get_next_id();
59         int get_num_threads();
60         int get_next_seq_num();
61
62         int switch_to_master(ModelAction *act);
63
64         bool next_execution();
65
66         MEMALLOC
67 private:
68         int next_thread_id;
69         int used_sequence_numbers;
70         int num_executions;
71
72         ModelAction * get_last_conflict(ModelAction *act);
73         void set_backtracking(ModelAction *act);
74         thread_id_t get_next_replay_thread();
75         ModelAction * get_next_backtrack();
76         void reset_to_initial_state();
77
78         void add_action_to_lists(ModelAction *act);
79         ModelAction * get_last_action(thread_id_t tid);
80         ModelAction * get_parent_action(thread_id_t tid);
81
82         void print_list(action_list_t *list);
83
84         ModelAction *current_action;
85         ModelAction *diverge;
86         thread_id_t nextThread;
87
88         ucontext_t *system_context;
89         action_list_t *action_trace;
90         std::map<int, class Thread *> *thread_map;
91         std::map<void *, std::vector<action_list_t> > *obj_thrd_map;
92         std::vector<ModelAction *> *thrd_last_action;
93         class NodeStack *node_stack;
94         ModelAction *next_backtrack;
95 };
96
97 extern ModelChecker *model;
98
99 #endif /* __MODEL_H__ */