clock: add modelclock_t typedef, use 'unsigned int'
[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 #include "clockvector.h"
22
23 /* Forward declaration */
24 class NodeStack;
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         class 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         /**
45          * Stores the ModelAction for the current thread action.  Call this
46          * immediately before switching from user- to system-context to pass
47          * data between them.
48          * @param act The ModelAction created by the user-thread action
49          */
50         void set_current_action(ModelAction *act) { current_action = act; }
51         void check_current_action(void);
52         void print_summary(void);
53         Thread * schedule_next_thread();
54
55         int add_thread(Thread *t);
56         void remove_thread(Thread *t);
57         Thread * get_thread(thread_id_t tid) { return (*thread_map)[id_to_int(tid)]; }
58
59         thread_id_t get_next_id();
60         int get_num_threads();
61         modelclock_t get_next_seq_num();
62
63         int switch_to_master(ModelAction *act);
64
65         bool next_execution();
66
67         MEMALLOC
68 private:
69         int next_thread_id;
70         modelclock_t used_sequence_numbers;
71         int num_executions;
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         void build_reads_from_past(ModelAction *curr);
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__ */