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