Merge branch 'norris'
[c11tester.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 /** A special value to represent a successful trylock */
29
30 #define VALUE_TRYSUCCESS 1
31
32 /** A special value to represent a failed trylock */
33 #define VALUE_TRYFAILED 0
34
35 /** @brief Represents an action type, identifying one of several types of
36  * ModelAction */
37 typedef enum action_type {
38         THREAD_CREATE,        /**< A thread creation action */
39         THREAD_START,         /**< First action in each thread */
40         THREAD_YIELD,         /**< A thread yield action */
41         THREAD_JOIN,          /**< A thread join action */
42         THREAD_FINISH,        /**< A thread completion action */
43         ATOMIC_READ,          /**< An atomic read action */
44         ATOMIC_WRITE,         /**< An atomic write action */
45         ATOMIC_RMWR,          /**< The read part of an atomic RMW action */
46         ATOMIC_RMW,           /**< The write part of an atomic RMW action */
47         ATOMIC_RMWC,          /**< Convert an atomic RMW action into a READ */
48         ATOMIC_INIT,          /**< Initialization of an atomic object (e.g.,
49                                *   atomic_init()) */
50         ATOMIC_FENCE,         /**< A fence action */
51         ATOMIC_LOCK,          /**< A lock action */
52         ATOMIC_TRYLOCK,       /**< A trylock action */
53         ATOMIC_UNLOCK         /**< An unlock action */
54 } action_type_t;
55
56 /* Forward declaration */
57 class Node;
58 class ClockVector;
59
60 /**
61  * The ModelAction class encapsulates an atomic action.
62  */
63 class ModelAction {
64 public:
65         ModelAction(action_type_t type, memory_order order, void *loc, uint64_t value = VALUE_NONE);
66         ~ModelAction();
67         void print(void) const;
68
69         thread_id_t get_tid() const { return tid; }
70         action_type get_type() const { return type; }
71         memory_order get_mo() const { return order; }
72         void * get_location() const { return location; }
73         modelclock_t get_seq_number() const { return seq_number; }
74         uint64_t get_value() const { return value; }
75         const ModelAction * get_reads_from() const { return reads_from; }
76
77         Node * get_node() const { return node; }
78         void set_node(Node *n) { node = n; }
79
80         void copy_from_new(ModelAction *newaction);
81         void set_try_lock(bool obtainedlock);
82         bool is_mutex_op() const;
83         bool is_lock() const;
84         bool is_trylock() const;
85         bool is_unlock() const;
86         bool is_success_lock() const;
87         bool is_failed_trylock() const;
88         bool is_read() const;
89         bool is_write() const;
90         bool is_rmwr() const;
91         bool is_rmwc() const;
92         bool is_rmw() const;
93         bool is_fence() const;
94         bool is_initialization() const;
95         bool is_acquire() const;
96         bool is_release() const;
97         bool is_seqcst() const;
98         bool same_var(const ModelAction *act) const;
99         bool same_thread(const ModelAction *act) const;
100         bool is_conflicting_lock(const ModelAction *act) const;
101         bool is_synchronizing(const ModelAction *act) const;
102
103         void create_cv(const ModelAction *parent = NULL);
104         ClockVector * get_cv() const { return cv; }
105         void read_from(const ModelAction *act);
106         void synchronize_with(const ModelAction *act);
107
108         bool has_synchronized_with(const ModelAction *act) const;
109         bool happens_before(const ModelAction *act) const;
110
111         inline bool operator <(const ModelAction& act) const {
112                 return get_seq_number() < act.get_seq_number();
113         }
114         inline bool operator >(const ModelAction& act) const {
115                 return get_seq_number() > act.get_seq_number();
116         }
117
118         void process_rmw(ModelAction * act);
119         void copy_typeandorder(ModelAction * act);
120
121         MEMALLOC
122 private:
123
124         /** Type of action (read, write, thread create, thread yield, thread join) */
125         action_type type;
126
127         /** The memory order for this operation. */
128         memory_order order;
129
130         /** A pointer to the memory location for this action. */
131         void *location;
132
133         /** The thread id that performed this action. */
134         thread_id_t tid;
135
136         /** The value written (for write or RMW; undefined for read) */
137         uint64_t value;
138
139         /** The action that this action reads from. Only valid for reads */
140         const ModelAction *reads_from;
141
142         /** A back reference to a Node in NodeStack, if this ModelAction is
143          * saved on the NodeStack. */
144         Node *node;
145
146         modelclock_t seq_number;
147
148         /** The clock vector stored with this action; only needed if this
149          * action is a store release? */
150         ClockVector *cv;
151 };
152
153 typedef std::list<ModelAction *> action_list_t;
154
155 #endif /* __ACTION_H__ */