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