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