model: delete Threads on destruction
[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 /** @brief The central structure for model-checking */
27 class ModelChecker {
28 public:
29         ModelChecker();
30         ~ModelChecker();
31
32         /** The scheduler to use: tracks the running/ready Threads */
33         Scheduler *scheduler;
34
35         /** Stores the context for the main model-checking system thread (call
36          * once)
37          * @param ctxt The system context structure
38          */
39         void set_system_context(ucontext_t *ctxt) { system_context = ctxt; }
40
41         /** @returns the context for the main model-checking system thread */
42         ucontext_t * get_system_context(void) { return system_context; }
43
44         void check_current_action(void);
45         void print_summary(void);
46         Thread * schedule_next_thread();
47
48         int add_thread(Thread *t);
49         void remove_thread(Thread *t);
50         Thread * get_thread(thread_id_t tid) { return thread_map->get(id_to_int(tid)); }
51
52         thread_id_t get_next_id();
53         int get_num_threads();
54         modelclock_t get_next_seq_num();
55
56         int switch_to_master(ModelAction *act);
57         ClockVector * get_cv(thread_id_t tid);
58         bool next_execution();
59         bool isfeasible();
60         bool isfinalfeasible();
61         void check_promises(ClockVector *old_cv, ClockVector * merge_cv);
62
63         MEMALLOC
64 private:
65         int next_thread_id;
66         modelclock_t used_sequence_numbers;
67         int num_executions;
68
69         /**
70          * Stores the ModelAction for the current thread action.  Call this
71          * immediately before switching from user- to system-context to pass
72          * data between them.
73          * @param act The ModelAction created by the user-thread action
74          */
75         void set_current_action(ModelAction *act) { current_action = act; }
76
77         ModelAction * get_last_conflict(ModelAction *act);
78         void set_backtracking(ModelAction *act);
79         thread_id_t get_next_replay_thread();
80         ModelAction * get_next_backtrack();
81         void reset_to_initial_state();
82         void resolve_promises(ModelAction *curr);
83         void compute_promises(ModelAction *curr);
84
85         void add_action_to_lists(ModelAction *act);
86         ModelAction * get_last_action(thread_id_t tid);
87         ModelAction * get_parent_action(thread_id_t tid);
88         ModelAction * get_last_seq_cst(const void *location);
89         void build_reads_from_past(ModelAction *curr);
90         ModelAction * process_rmw(ModelAction * curr);
91         void post_r_modification_order(ModelAction * curr, const ModelAction *rf);
92         void r_modification_order(ModelAction * curr, const ModelAction *rf);
93         void w_modification_order(ModelAction * curr);
94
95         ModelAction *current_action;
96         ModelAction *diverge;
97         thread_id_t nextThread;
98
99         ucontext_t *system_context;
100         action_list_t *action_trace;
101         HashTable<int, Thread *, int> *thread_map;
102
103         /** Per-object list of actions. Maps an object (i.e., memory location)
104          * to a trace of all actions performed on the object. */
105         HashTable<const void *, action_list_t, uintptr_t, 4> *obj_map;
106
107         HashTable<void *, std::vector<action_list_t>, uintptr_t, 4 > *obj_thrd_map;
108         std::vector<Promise *> * promises;
109         std::vector<ModelAction *> *thrd_last_action;
110         NodeStack *node_stack;
111         ModelAction *next_backtrack;
112         CycleGraph * cyclegraph;
113         bool failed_promise;
114 };
115
116 extern ModelChecker *model;
117
118 #endif /* __MODEL_H__ */