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