action: switch from "libatomic" to C++-spec "memory_order_*" macros
[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 /** @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_RMW,           /**< An atomic read-modify-write action */
38         ATOMIC_INIT           /**< Initialization of an atomic object (e.g.,
39                                *   atomic_init()) */
40 } action_type_t;
41
42 /* Forward declaration */
43 class Node;
44 class ClockVector;
45
46 /**
47  * The ModelAction class encapsulates an atomic action.
48  */
49 class ModelAction {
50 public:
51         ModelAction(action_type_t type, memory_order order, void *loc, uint64_t value = VALUE_NONE);
52         ~ModelAction();
53         void print(void) const;
54
55         thread_id_t get_tid() const { return tid; }
56         action_type get_type() const { return type; }
57         memory_order get_mo() const { return order; }
58         void * get_location() const { return location; }
59         modelclock_t get_seq_number() const { return seq_number; }
60         uint64_t get_value() const { return value; }
61         const ModelAction * get_reads_from() const { return reads_from; }
62
63         Node * get_node() const { return node; }
64         void set_node(Node *n) { node = n; }
65
66         bool is_read() const;
67         bool is_write() const;
68         bool is_rmw() const;
69         bool is_initialization() const;
70         bool is_acquire() const;
71         bool is_release() const;
72         bool is_seqcst() const;
73         bool same_var(const ModelAction *act) const;
74         bool same_thread(const ModelAction *act) const;
75         bool is_synchronizing(const ModelAction *act) const;
76
77         void create_cv(const ModelAction *parent = NULL);
78         ClockVector * get_cv() const { return cv; }
79         void read_from(const ModelAction *act);
80
81         bool happens_before(const ModelAction *act) const;
82
83         inline bool operator <(const ModelAction& act) const {
84                 return get_seq_number() < act.get_seq_number();
85         }
86         inline bool operator >(const ModelAction& act) const {
87                 return get_seq_number() > act.get_seq_number();
88         }
89
90         void upgrade_rmw(ModelAction * act);
91
92         MEMALLOC
93 private:
94
95         /** Type of action (read, write, thread create, thread yield, thread join) */
96         action_type type;
97
98         /** The memory order for this operation. */
99         memory_order order;
100
101         /** A pointer to the memory location for this action. */
102         void *location;
103
104         /** The thread id that performed this action. */
105         thread_id_t tid;
106
107         /** The value written (for write or RMW; undefined for read) */
108         uint64_t value;
109
110         /** The action that this action reads from. Only valid for reads */
111         const ModelAction *reads_from;
112
113         /** A back reference to a Node in NodeStack, if this ModelAction is
114          * saved on the NodeStack. */
115         Node *node;
116
117         modelclock_t seq_number;
118
119         /** The clock vector stored with this action; only needed if this
120          * action is a store release? */
121         ClockVector *cv;
122 };
123
124 typedef std::list<ModelAction *> action_list_t;
125
126 #endif /* __ACTION_H__ */