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