make scheduler choose fair schedules when threads with priority are sleeping...
[model-checker.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 #include <inttypes.h>
11
12 #include "mymemory.h"
13 #include "memoryorder.h"
14 #include "modeltypes.h"
15
16 class ClockVector;
17 class Thread;
18
19 using std::memory_order;
20 using std::memory_order_relaxed;
21 using std::memory_order_acquire;
22 using std::memory_order_release;
23 using std::memory_order_acq_rel;
24 using std::memory_order_seq_cst;
25
26 /** Note that this value can be legitimately used by a program, and
27                 hence by iteself does not indicate no value. */
28
29 #define VALUE_NONE 1234567890
30
31 /** A special value to represent a successful trylock */
32
33 #define VALUE_TRYSUCCESS 1
34
35 /** A special value to represent a failed trylock */
36 #define VALUE_TRYFAILED 0
37
38 /** @brief Represents an action type, identifying one of several types of
39  * ModelAction */
40 typedef enum action_type {
41         MODEL_FIXUP_RELSEQ,   /**< Special ModelAction: finalize a release
42                                *   sequence */
43         THREAD_CREATE,        /**< A thread creation action */
44         THREAD_START,         /**< First action in each thread */
45         THREAD_YIELD,         /**< A thread yield action */
46         THREAD_JOIN,          /**< A thread join action */
47         THREAD_FINISH,        /**< A thread completion action */
48         ATOMIC_READ,          /**< An atomic read action */
49         ATOMIC_WRITE,         /**< An atomic write action */
50         ATOMIC_RMWR,          /**< The read part of an atomic RMW action */
51         ATOMIC_RMW,           /**< The write part of an atomic RMW action */
52         ATOMIC_RMWC,          /**< Convert an atomic RMW action into a READ */
53         ATOMIC_INIT,          /**< Initialization of an atomic object (e.g.,
54                                *   atomic_init()) */
55         ATOMIC_FENCE,         /**< A fence action */
56         ATOMIC_LOCK,          /**< A lock action */
57         ATOMIC_TRYLOCK,       /**< A trylock action */
58         ATOMIC_UNLOCK         /**< An unlock action */
59 } action_type_t;
60
61 /* Forward declaration */
62 class Node;
63 class ClockVector;
64
65 /**
66  * The ModelAction class encapsulates an atomic action.
67  */
68 class ModelAction {
69 public:
70         ModelAction(action_type_t type, memory_order order, void *loc, uint64_t value = VALUE_NONE, Thread *thread = NULL);
71         ~ModelAction();
72         void print() const;
73
74         thread_id_t get_tid() const { return tid; }
75         action_type get_type() const { return type; }
76         memory_order get_mo() const { return order; }
77         void * get_location() const { return location; }
78         modelclock_t get_seq_number() const { return seq_number; }
79         uint64_t get_value() const { return value; }
80         const ModelAction * get_reads_from() const { return reads_from; }
81
82         Node * get_node() const { return node; }
83         void set_node(Node *n) { node = n; }
84
85         void copy_from_new(ModelAction *newaction);
86         void set_seq_number(modelclock_t num);
87         void set_try_lock(bool obtainedlock);
88         bool is_relseq_fixup() const;
89         bool is_mutex_op() const;
90         bool is_lock() const;
91         bool is_trylock() const;
92         bool is_unlock() const;
93         bool is_success_lock() const;
94         bool is_failed_trylock() const;
95         bool is_read() const;
96         bool is_write() const;
97         bool could_be_write() const;
98         bool is_rmwr() const;
99         bool is_rmwc() const;
100         bool is_rmw() const;
101         bool is_fence() const;
102         bool is_initialization() const;
103         bool is_acquire() const;
104         bool is_release() const;
105         bool is_seqcst() const;
106         bool same_var(const ModelAction *act) const;
107         bool same_thread(const ModelAction *act) const;
108         bool is_conflicting_lock(const ModelAction *act) const;
109         bool could_synchronize_with(const ModelAction *act) const;
110
111         void create_cv(const ModelAction *parent = NULL);
112         ClockVector * get_cv() const { return cv; }
113         bool read_from(const ModelAction *act);
114         bool synchronize_with(const ModelAction *act);
115
116         bool has_synchronized_with(const ModelAction *act) const;
117         bool happens_before(const ModelAction *act) const;
118
119         inline bool operator <(const ModelAction& act) const {
120                 return get_seq_number() < act.get_seq_number();
121         }
122         inline bool operator >(const ModelAction& act) const {
123                 return get_seq_number() > act.get_seq_number();
124         }
125
126         void process_rmw(ModelAction * act);
127         void copy_typeandorder(ModelAction * act);
128
129         void set_sleep_flag() { sleep_flag=true; }
130         bool get_sleep_flag() { return sleep_flag; }
131         unsigned int hash() const;
132
133         MEMALLOC
134 private:
135
136         /** Type of action (read, write, thread create, thread yield, thread join) */
137         action_type type;
138
139         /** The memory order for this operation. */
140         memory_order order;
141
142         /** A pointer to the memory location for this action. */
143         void *location;
144
145         /** The thread id that performed this action. */
146         thread_id_t tid;
147
148         /** The value written (for write or RMW; undefined for read) */
149         uint64_t value;
150
151         /** The action that this action reads from. Only valid for reads */
152         const ModelAction *reads_from;
153
154         /** A back reference to a Node in NodeStack, if this ModelAction is
155          * saved on the NodeStack. */
156         Node *node;
157
158         modelclock_t seq_number;
159
160         /** The clock vector stored with this action; only needed if this
161          * action is a store release? */
162         ClockVector *cv;
163
164         bool sleep_flag;
165 };
166
167 typedef std::list< ModelAction *, SnapshotAlloc<ModelAction *> > action_list_t;
168
169 #endif /* __ACTION_H__ */