hashtable: make more flexible
[model-checker.git] / action.h
1 /** @file action.h
2  *  @brief Models actions taken by threads.
3  */
4
5 #ifndef __ACTION_H__
6 #define __ACTION_H__
7
8 #include <list>
9 #include <cstddef>
10
11 #include "threads.h"
12 #include "mymemory.h"
13 #include "clockvector.h"
14 #include "memoryorder.h"
15
16 using std::memory_order;
17 using std::memory_order_relaxed;
18 using std::memory_order_acquire;
19 using std::memory_order_release;
20 using std::memory_order_acq_rel;
21 using std::memory_order_seq_cst;
22
23 /** Note that this value can be legitimately used by a program, and
24                 hence by iteself does not indicate no value. */
25
26 #define VALUE_NONE 1234567890
27
28 /** @brief Represents an action type, identifying one of several types of
29  * ModelAction */
30 typedef enum action_type {
31         THREAD_CREATE,        /**< A thread creation action */
32         THREAD_START,         /**< First action in each thread */
33         THREAD_YIELD,         /**< A thread yield action */
34         THREAD_JOIN,          /**< A thread join action */
35         ATOMIC_READ,          /**< An atomic read action */
36         ATOMIC_WRITE,         /**< An atomic write action */
37         ATOMIC_RMWR,          /**< The read part of an atomic RMW action */
38         ATOMIC_RMW,           /**< The write part of an atomic RMW action */
39         ATOMIC_RMWC,          /**< Convert an atomic RMW action into a READ */
40         ATOMIC_INIT           /**< Initialization of an atomic object (e.g.,
41                                *   atomic_init()) */
42 } action_type_t;
43
44 /* Forward declaration */
45 class Node;
46 class ClockVector;
47
48 /**
49  * The ModelAction class encapsulates an atomic action.
50  */
51 class ModelAction {
52 public:
53         ModelAction(action_type_t type, memory_order order, void *loc, uint64_t value = VALUE_NONE);
54         ~ModelAction();
55         void print(void) const;
56
57         thread_id_t get_tid() const { return tid; }
58         action_type get_type() const { return type; }
59         memory_order get_mo() const { return order; }
60         void * get_location() const { return location; }
61         modelclock_t get_seq_number() const { return seq_number; }
62         uint64_t get_value() const { return value; }
63         const ModelAction * get_reads_from() const { return reads_from; }
64
65         Node * get_node() const { return node; }
66         void set_node(Node *n) { node = n; }
67
68         bool is_read() const;
69         bool is_write() const;
70         bool is_rmwr() const;
71         bool is_rmwc() const;
72         bool is_rmw() const;
73         bool is_initialization() const;
74         bool is_acquire() const;
75         bool is_release() const;
76         bool is_seqcst() const;
77         bool same_var(const ModelAction *act) const;
78         bool same_thread(const ModelAction *act) const;
79         bool is_synchronizing(const ModelAction *act) const;
80
81         void create_cv(const ModelAction *parent = NULL);
82         ClockVector * get_cv() const { return cv; }
83         void read_from(const ModelAction *act);
84
85         bool happens_before(const ModelAction *act) const;
86
87         inline bool operator <(const ModelAction& act) const {
88                 return get_seq_number() < act.get_seq_number();
89         }
90         inline bool operator >(const ModelAction& act) const {
91                 return get_seq_number() > act.get_seq_number();
92         }
93
94         void process_rmw(ModelAction * act);
95         void copy_typeandorder(ModelAction * act);
96
97         MEMALLOC
98 private:
99
100         /** Type of action (read, write, thread create, thread yield, thread join) */
101         action_type type;
102
103         /** The memory order for this operation. */
104         memory_order order;
105
106         /** A pointer to the memory location for this action. */
107         void *location;
108
109         /** The thread id that performed this action. */
110         thread_id_t tid;
111
112         /** The value written (for write or RMW; undefined for read) */
113         uint64_t value;
114
115         /** The action that this action reads from. Only valid for reads */
116         const ModelAction *reads_from;
117
118         /** A back reference to a Node in NodeStack, if this ModelAction is
119          * saved on the NodeStack. */
120         Node *node;
121
122         modelclock_t seq_number;
123
124         /** The clock vector stored with this action; only needed if this
125          * action is a store release? */
126         ClockVector *cv;
127 };
128
129 typedef std::list<ModelAction *> action_list_t;
130
131 #endif /* __ACTION_H__ */