add basic parameter handling
[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 <vector>
10 #include <cstddef>
11 #include <ucontext.h>
12
13 #include "schedule.h"
14 #include "mymemory.h"
15 #include "libthreads.h"
16 #include "threads.h"
17 #include "action.h"
18 #include "clockvector.h"
19 #include "hashtable.h"
20
21 /* Forward declaration */
22 class NodeStack;
23 class CycleGraph;
24 class Promise;
25
26 /**
27  * Model checker parameter structure. Holds run-time configuration options for
28  * the model checker.
29  */
30 struct model_params {
31 };
32
33 /** @brief The central structure for model-checking */
34 class ModelChecker {
35 public:
36         ModelChecker(struct model_params params);
37         ~ModelChecker();
38
39         /** The scheduler to use: tracks the running/ready Threads */
40         Scheduler *scheduler;
41
42         /** Stores the context for the main model-checking system thread (call
43          * once)
44          * @param ctxt The system context structure
45          */
46         void set_system_context(ucontext_t *ctxt) { system_context = ctxt; }
47
48         /** @returns the context for the main model-checking system thread */
49         ucontext_t * get_system_context(void) { return system_context; }
50
51         void check_current_action(void);
52
53         /**
54          * Prints an execution summary with trace information.
55          * @param feasible Formats outputting according to whether or not the
56          * current trace is feasible. Defaults to feasible = true.
57          */
58         void print_summary(bool feasible = true);
59
60         Thread * schedule_next_thread();
61
62         int add_thread(Thread *t);
63         void remove_thread(Thread *t);
64         Thread * get_thread(thread_id_t tid) { return thread_map->get(id_to_int(tid)); }
65
66         thread_id_t get_next_id();
67         int get_num_threads();
68         modelclock_t get_next_seq_num();
69
70         int switch_to_master(ModelAction *act);
71         ClockVector * get_cv(thread_id_t tid);
72         bool next_execution();
73         bool isfeasible();
74         bool isfinalfeasible();
75         void check_promises(ClockVector *old_cv, ClockVector * merge_cv);
76
77         MEMALLOC
78 private:
79         int next_thread_id;
80         modelclock_t used_sequence_numbers;
81         int num_executions;
82
83         const model_params params;
84
85         /**
86          * Stores the ModelAction for the current thread action.  Call this
87          * immediately before switching from user- to system-context to pass
88          * data between them.
89          * @param act The ModelAction created by the user-thread action
90          */
91         void set_current_action(ModelAction *act) { current_action = act; }
92
93         ModelAction * get_last_conflict(ModelAction *act);
94         void set_backtracking(ModelAction *act);
95         thread_id_t get_next_replay_thread();
96         ModelAction * get_next_backtrack();
97         void reset_to_initial_state();
98         void resolve_promises(ModelAction *curr);
99         void compute_promises(ModelAction *curr);
100
101         void add_action_to_lists(ModelAction *act);
102         ModelAction * get_last_action(thread_id_t tid);
103         ModelAction * get_parent_action(thread_id_t tid);
104         ModelAction * get_last_seq_cst(const void *location);
105         void build_reads_from_past(ModelAction *curr);
106         ModelAction * process_rmw(ModelAction * curr);
107         void post_r_modification_order(ModelAction * curr, const ModelAction *rf);
108         void r_modification_order(ModelAction * curr, const ModelAction *rf);
109         void w_modification_order(ModelAction * curr);
110
111         ModelAction *current_action;
112         ModelAction *diverge;
113         thread_id_t nextThread;
114
115         ucontext_t *system_context;
116         action_list_t *action_trace;
117         HashTable<int, Thread *, int> *thread_map;
118
119         /** Per-object list of actions. Maps an object (i.e., memory location)
120          * to a trace of all actions performed on the object. */
121         HashTable<const void *, action_list_t, uintptr_t, 4> *obj_map;
122
123         HashTable<void *, std::vector<action_list_t>, uintptr_t, 4 > *obj_thrd_map;
124         std::vector<Promise *> * promises;
125         std::vector<ModelAction *> *thrd_last_action;
126         NodeStack *node_stack;
127         ModelAction *next_backtrack;
128         CycleGraph * cyclegraph;
129         bool failed_promise;
130 };
131
132 extern ModelChecker *model;
133
134 #endif /* __MODEL_H__ */