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