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