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