9 #include "snapshot-interface.h"
10 #include "threads-model.h"
12 memory_order orders[6] = {
13 memory_order_relaxed, memory_order_consume, memory_order_acquire,
14 memory_order_release, memory_order_acq_rel, memory_order_seq_cst
17 static void ensureModel() {
19 snapshot_system_init(10000, 1024, 1024, 40000);
20 model = new ModelChecker();
21 model->startChecker();
25 /** Performs a read action.*/
26 uint64_t model_read_action(void * obj, memory_order ord) {
27 return model->switch_to_master(new ModelAction(ATOMIC_READ, ord, obj));
30 /** Performs a write action.*/
31 void model_write_action(void * obj, memory_order ord, uint64_t val) {
32 model->switch_to_master(new ModelAction(ATOMIC_WRITE, ord, obj, val));
35 /** Performs an init action. */
36 void model_init_action(void * obj, uint64_t val) {
37 model->switch_to_master(new ModelAction(ATOMIC_INIT, memory_order_relaxed, obj, val));
41 * Performs the read part of a RMW action. The next action must either be the
42 * write part of the RMW action or an explicit close out of the RMW action w/o
45 uint64_t model_rmwr_action(void *obj, memory_order ord) {
46 return model->switch_to_master(new ModelAction(ATOMIC_RMWR, ord, obj));
50 * Performs the read part of a RMW CAS action. The next action must
51 * either be the write part of the RMW action or an explicit close out
52 * of the RMW action w/o a write.
54 uint64_t model_rmwrcas_action(void *obj, memory_order ord, uint64_t oldval, int size) {
55 return model->switch_to_master(new ModelAction(ATOMIC_RMWRCAS, ord, obj, oldval, size));
59 /** Performs the write part of a RMW action. */
60 void model_rmw_action(void *obj, memory_order ord, uint64_t val) {
61 model->switch_to_master(new ModelAction(ATOMIC_RMW, ord, obj, val));
64 /** Closes out a RMW action without doing a write. */
65 void model_rmwc_action(void *obj, memory_order ord) {
66 model->switch_to_master(new ModelAction(ATOMIC_RMWC, ord, obj));
69 /** Issues a fence operation. */
70 void model_fence_action(memory_order ord) {
71 model->switch_to_master(new ModelAction(ATOMIC_FENCE, ord, FENCE_LOCATION));
74 /* --- helper functions --- */
75 uint64_t model_rmwrcas_action_helper(void *obj, int atomic_index, uint64_t oldval, int size, const char *position) {
77 return model->switch_to_master(new ModelAction(ATOMIC_RMWRCAS, position, orders[atomic_index], obj, oldval, size));
80 uint64_t model_rmwr_action_helper(void *obj, int atomic_index, const char *position) {
82 return model->switch_to_master(new ModelAction(ATOMIC_RMWR, position, orders[atomic_index], obj));
85 void model_rmw_action_helper(void *obj, uint64_t val, int atomic_index, const char * position) {
87 model->switch_to_master(new ModelAction(ATOMIC_RMW, position, orders[atomic_index], obj, val));
90 void model_rmwc_action_helper(void *obj, int atomic_index, const char *position) {
92 model->switch_to_master(new ModelAction(ATOMIC_RMWC, position, orders[atomic_index], obj));
96 #define CDSATOMICINT(size) \
97 void cds_atomic_init ## size (void * obj, uint ## size ## _t val, const char * position) { \
99 model->switch_to_master(new ModelAction(ATOMIC_INIT, position, memory_order_relaxed, obj, (uint64_t) val)); \
100 *((uint ## size ## _t *)obj) = val; \
109 #define CDSATOMICLOAD(size) \
110 uint ## size ## _t cds_atomic_load ## size(void * obj, int atomic_index, const char * position) { \
112 return (uint ## size ## _t) model->switch_to_master( \
113 new ModelAction(ATOMIC_READ, position, orders[atomic_index], obj)); \
123 #define CDSATOMICSTORE(size) \
124 void cds_atomic_store ## size(void * obj, uint ## size ## _t val, int atomic_index, const char * position) { \
126 model->switch_to_master(new ModelAction(ATOMIC_WRITE, position, orders[atomic_index], obj, (uint64_t) val)); \
127 *((uint ## size ## _t *)obj) = val; \
136 #define _ATOMIC_RMW_(__op__, size, addr, val, atomic_index, position) \
138 uint ## size ## _t _old = model_rmwr_action_helper(addr, atomic_index, position); \
139 uint ## size ## _t _copy = _old; \
140 uint ## size ## _t _val = val; \
142 model_rmw_action_helper(addr, (uint64_t) _copy, atomic_index, position); \
146 // cds atomic exchange
147 #define CDSATOMICEXCHANGE(size) \
148 uint ## size ## _t cds_atomic_exchange ## size(void* addr, uint ## size ## _t val, int atomic_index, const char * position) { \
149 _ATOMIC_RMW_( =, size, addr, val, atomic_index, position); \
153 CDSATOMICEXCHANGE(16)
154 CDSATOMICEXCHANGE(32)
155 CDSATOMICEXCHANGE(64)
157 // cds atomic fetch add
158 #define CDSATOMICADD(size) \
159 uint ## size ## _t cds_atomic_fetch_add ## size(void* addr, uint ## size ## _t val, int atomic_index, const char * position) { \
160 _ATOMIC_RMW_( +=, size, addr, val, atomic_index, position); \
168 // cds atomic fetch sub
169 #define CDSATOMICSUB(size) \
170 uint ## size ## _t cds_atomic_fetch_sub ## size(void* addr, uint ## size ## _t val, int atomic_index, const char * position) { \
171 _ATOMIC_RMW_( -=, size, addr, val, atomic_index, position); \
179 // cds atomic fetch and
180 #define CDSATOMICAND(size) \
181 uint ## size ## _t cds_atomic_fetch_and ## size(void* addr, uint ## size ## _t val, int atomic_index, const char * position) { \
182 _ATOMIC_RMW_( &=, size, addr, val, atomic_index, position); \
190 // cds atomic fetch or
191 #define CDSATOMICOR(size) \
192 uint ## size ## _t cds_atomic_fetch_or ## size(void* addr, uint ## size ## _t val, int atomic_index, const char * position) { \
193 _ATOMIC_RMW_( |=, size, addr, val, atomic_index, position); \
201 // cds atomic fetch xor
202 #define CDSATOMICXOR(size) \
203 uint ## size ## _t cds_atomic_fetch_xor ## size(void* addr, uint ## size ## _t val, int atomic_index, const char * position) { \
204 _ATOMIC_RMW_( ^=, size, addr, val, atomic_index, position); \
212 // cds atomic compare and exchange
213 // In order to accomodate the LLVM PASS, the return values are not true or false.
215 #define _ATOMIC_CMPSWP_WEAK_ _ATOMIC_CMPSWP_
216 #define _ATOMIC_CMPSWP_(size, addr, expected, desired, atomic_index, position) \
218 uint ## size ## _t _desired = desired; \
219 uint ## size ## _t _expected = expected; \
220 uint ## size ## _t _old = model_rmwrcas_action_helper(addr, atomic_index, _expected, sizeof(_expected), position); \
221 if (_old == _expected) { \
222 model_rmw_action_helper(addr, (uint64_t) _desired, atomic_index, position); return _expected; } \
224 model_rmwc_action_helper(addr, atomic_index, position); _expected = _old; return _old; } \
227 // atomic_compare_exchange version 1: the CmpOperand (corresponds to expected)
228 // extracted from LLVM IR is an integer type.
229 #define CDSATOMICCASV1(size) \
230 uint ## size ## _t cds_atomic_compare_exchange ## size ## _v1(void* addr, uint ## size ## _t expected, uint ## size ## _t desired, int atomic_index_succ, int atomic_index_fail, const char *position) { \
231 _ATOMIC_CMPSWP_(size, addr, expected, desired, atomic_index_succ, position); \
239 // atomic_compare_exchange version 2
240 #define CDSATOMICCASV2(size) \
241 bool cds_atomic_compare_exchange ## size ## _v2(void* addr, uint ## size ## _t* expected, uint ## size ## _t desired, int atomic_index_succ, int atomic_index_fail, const char *position) { \
242 uint ## size ## _t ret = cds_atomic_compare_exchange ## size ## _v1(addr, *expected, desired, atomic_index_succ, atomic_index_fail, position); \
243 if (ret == *expected) {return true;} else {return false;} \
251 // cds atomic thread fence
253 void cds_atomic_thread_fence(int atomic_index, const char * position) {
254 model->switch_to_master(
255 new ModelAction(ATOMIC_FENCE, position, orders[atomic_index], FENCE_LOCATION)
260 #define _ATOMIC_CMPSWP_( __a__, __e__, __m__, __x__ ) \
261 ({ volatile __typeof__((__a__)->__f__)* __p__ = & ((__a__)->__f__); \
262 __typeof__(__e__) __q__ = (__e__); \
263 __typeof__(__m__) __v__ = (__m__); \
265 __typeof__((__a__)->__f__) __t__=(__typeof__((__a__)->__f__)) model_rmwr_action((void *)__p__, __x__); \
266 if (__t__ == * __q__ ) { \
267 model_rmw_action((void *)__p__, __x__, (uint64_t) __v__); __r__ = true; } \
268 else { model_rmwc_action((void *)__p__, __x__); *__q__ = __t__; __r__ = false;} \
271 #define _ATOMIC_FENCE_( __x__ ) \
272 ({ model_fence_action(__x__);})
277 #define _ATOMIC_MODIFY_( __a__, __o__, __m__, __x__ ) \
278 ({ volatile __typeof__((__a__)->__f__)* __p__ = & ((__a__)->__f__); \
279 __typeof__((__a__)->__f__) __old__=(__typeof__((__a__)->__f__)) model_rmwr_action((void *)__p__, __x__); \
280 __typeof__(__m__) __v__ = (__m__); \
281 __typeof__((__a__)->__f__) __copy__= __old__; \
282 __copy__ __o__ __v__; \
283 model_rmw_action((void *)__p__, __x__, (uint64_t) __copy__); \
284 __old__ = __old__; Silence clang (-Wunused-value) \
288 void cds_func_entry(const char * funcName) {
291 Thread * th = thread_current();
294 ModelHistory *history = model->get_history();
295 if ( !history->getFuncMap()->contains(funcName) ) {
296 // add func id to func map
297 func_id = history->get_func_counter();
298 history->incr_func_counter();
299 history->getFuncMap()->put(funcName, func_id);
301 // add func id to reverse func map
302 ModelVector<const char *> * func_map_rev = history->getFuncMapRev();
303 if ( func_map_rev->size() <= func_id )
304 func_map_rev->resize( func_id + 1 );
305 func_map_rev->at(func_id) = funcName;
307 func_id = history->getFuncMap()->get(funcName);
310 history->enter_function(func_id, th->get_id());
313 void cds_func_exit(const char * funcName) {
316 Thread * th = thread_current();
319 ModelHistory *history = model->get_history();
320 func_id = history->getFuncMap()->get(funcName);
322 /* func_id not found; this could happen in the case where a function calls cds_func_entry
323 * when the model has been defined yet, but then an atomic inside the function initializes
324 * the model. And then cds_func_exit is called upon the function exiting.
329 history->exit_function(func_id, th->get_id());