9 #include "snapshot-interface.h"
10 #include "threads-model.h"
13 memory_order orders[8] = {
14 memory_order_relaxed, memory_order_consume, memory_order_acquire,
15 memory_order_release, memory_order_acq_rel, memory_order_seq_cst,
18 static void ensureModel() {
20 snapshot_system_init(10000, 1024, 1024, 40000);
21 model = new ModelChecker();
22 model->startChecker();
26 /** Performs a read action.*/
27 uint64_t model_read_action(void * obj, memory_order ord) {
28 return model->switch_to_master(new ModelAction(ATOMIC_READ, ord, obj));
31 /** Performs a write action.*/
32 void model_write_action(void * obj, memory_order ord, uint64_t val) {
33 model->switch_to_master(new ModelAction(ATOMIC_WRITE, ord, obj, val));
36 /** Performs an init action. */
37 void model_init_action(void * obj, uint64_t val) {
38 model->switch_to_master(new ModelAction(ATOMIC_INIT, memory_order_relaxed, obj, val));
42 * Performs the read part of a RMW action. The next action must either be the
43 * write part of the RMW action or an explicit close out of the RMW action w/o
46 uint64_t model_rmwr_action(void *obj, memory_order ord) {
47 return model->switch_to_master(new ModelAction(ATOMIC_RMWR, ord, obj));
51 * Performs the read part of a RMW CAS action. The next action must
52 * either be the write part of the RMW action or an explicit close out
53 * of the RMW action w/o a write.
55 uint64_t model_rmwrcas_action(void *obj, memory_order ord, uint64_t oldval, int size) {
56 return model->switch_to_master(new ModelAction(ATOMIC_RMWRCAS, ord, obj, oldval, size));
60 /** Performs the write part of a RMW action. */
61 void model_rmw_action(void *obj, memory_order ord, uint64_t val) {
62 model->switch_to_master(new ModelAction(ATOMIC_RMW, ord, obj, val));
65 /** Closes out a RMW action without doing a write. */
66 void model_rmwc_action(void *obj, memory_order ord) {
67 model->switch_to_master(new ModelAction(ATOMIC_RMWC, ord, obj));
70 /** Issues a fence operation. */
71 void model_fence_action(memory_order ord) {
72 model->switch_to_master(new ModelAction(ATOMIC_FENCE, ord, FENCE_LOCATION));
75 /* --- helper functions --- */
76 uint64_t model_rmwrcas_action_helper(void *obj, int atomic_index, uint64_t oldval, int size, const char *position) {
78 return model->switch_to_master(new ModelAction(ATOMIC_RMWRCAS, position, orders[atomic_index], obj, oldval, size));
81 uint64_t model_rmwr_action_helper(void *obj, int atomic_index, const char *position) {
83 return model->switch_to_master(new ModelAction(ATOMIC_RMWR, position, orders[atomic_index], obj));
86 void model_rmw_action_helper(void *obj, uint64_t val, int atomic_index, const char * position) {
88 model->switch_to_master(new ModelAction(ATOMIC_RMW, position, orders[atomic_index], obj, val));
91 void model_rmwc_action_helper(void *obj, int atomic_index, const char *position) {
93 model->switch_to_master(new ModelAction(ATOMIC_RMWC, position, orders[atomic_index], obj));
97 #define VOLATILELOAD(size) \
98 uint ## size ## _t cds_volatile_load ## size(void * obj, const char * position) { \
100 return (uint ## size ## _t)model->switch_to_master(new ModelAction(ATOMIC_READ, position, memory_order_relaxed, obj)); \
108 // cds volatile stores
109 #define VOLATILESTORE(size) \
110 void cds_volatile_store ## size (void * obj, uint ## size ## _t val, const char * position) { \
112 model->switch_to_master(new ModelAction(ATOMIC_WRITE, position, memory_order_relaxed, obj, (uint64_t) val)); \
113 *((volatile uint ## size ## _t *)obj) = val; \
122 #define CDSATOMICINT(size) \
123 void cds_atomic_init ## size (void * obj, uint ## size ## _t val, const char * position) { \
125 model->switch_to_master(new ModelAction(ATOMIC_INIT, position, memory_order_relaxed, obj, (uint64_t) val)); \
126 *((volatile uint ## size ## _t *)obj) = val; \
127 thread_id_t tid = thread_current()->get_id(); \
128 for(int i=0;i < size / 8;i++) { \
129 recordWrite(tid, (void *)(((char *)obj)+i)); \
139 #define CDSATOMICLOAD(size) \
140 uint ## size ## _t cds_atomic_load ## size(void * obj, int atomic_index, const char * position) { \
142 return (uint ## size ## _t)model->switch_to_master( \
143 new ModelAction(ATOMIC_READ, position, orders[atomic_index], obj)); \
152 #define CDSATOMICSTORE(size) \
153 void cds_atomic_store ## size(void * obj, uint ## size ## _t val, int atomic_index, const char * position) { \
155 model->switch_to_master(new ModelAction(ATOMIC_WRITE, position, orders[atomic_index], obj, (uint64_t) val)); \
156 *((volatile uint ## size ## _t *)obj) = val; \
157 thread_id_t tid = thread_current()->get_id(); \
158 for(int i=0;i < size / 8;i++) { \
159 recordWrite(tid, (void *)(((char *)obj)+i)); \
169 #define _ATOMIC_RMW_(__op__, size, addr, val, atomic_index, position) \
171 uint ## size ## _t _old = model_rmwr_action_helper(addr, atomic_index, position); \
172 uint ## size ## _t _copy = _old; \
173 uint ## size ## _t _val = val; \
175 model_rmw_action_helper(addr, (uint64_t) _copy, atomic_index, position); \
176 *((volatile uint ## size ## _t *)addr) = _copy; \
177 thread_id_t tid = thread_current()->get_id(); \
178 for(int i=0;i < size / 8;i++) { \
179 recordWrite(tid, (void *)(((char *)addr)+i)); \
184 // cds atomic exchange
185 #define CDSATOMICEXCHANGE(size) \
186 uint ## size ## _t cds_atomic_exchange ## size(void* addr, uint ## size ## _t val, int atomic_index, const char * position) { \
187 _ATOMIC_RMW_( =, size, addr, val, atomic_index, position); \
191 CDSATOMICEXCHANGE(16)
192 CDSATOMICEXCHANGE(32)
193 CDSATOMICEXCHANGE(64)
195 // cds atomic fetch add
196 #define CDSATOMICADD(size) \
197 uint ## size ## _t cds_atomic_fetch_add ## size(void* addr, uint ## size ## _t val, int atomic_index, const char * position) { \
198 _ATOMIC_RMW_( +=, size, addr, val, atomic_index, position); \
206 // cds atomic fetch sub
207 #define CDSATOMICSUB(size) \
208 uint ## size ## _t cds_atomic_fetch_sub ## size(void* addr, uint ## size ## _t val, int atomic_index, const char * position) { \
209 _ATOMIC_RMW_( -=, size, addr, val, atomic_index, position); \
217 // cds atomic fetch and
218 #define CDSATOMICAND(size) \
219 uint ## size ## _t cds_atomic_fetch_and ## size(void* addr, uint ## size ## _t val, int atomic_index, const char * position) { \
220 _ATOMIC_RMW_( &=, size, addr, val, atomic_index, position); \
228 // cds atomic fetch or
229 #define CDSATOMICOR(size) \
230 uint ## size ## _t cds_atomic_fetch_or ## size(void* addr, uint ## size ## _t val, int atomic_index, const char * position) { \
231 _ATOMIC_RMW_( |=, size, addr, val, atomic_index, position); \
239 // cds atomic fetch xor
240 #define CDSATOMICXOR(size) \
241 uint ## size ## _t cds_atomic_fetch_xor ## size(void* addr, uint ## size ## _t val, int atomic_index, const char * position) { \
242 _ATOMIC_RMW_( ^=, size, addr, val, atomic_index, position); \
250 // cds atomic compare and exchange
251 // In order to accomodate the LLVM PASS, the return values are not true or false.
253 #define _ATOMIC_CMPSWP_WEAK_ _ATOMIC_CMPSWP_
254 #define _ATOMIC_CMPSWP_(size, addr, expected, desired, atomic_index, position) \
256 uint ## size ## _t _desired = desired; \
257 uint ## size ## _t _expected = expected; \
258 uint ## size ## _t _old = model_rmwrcas_action_helper(addr, atomic_index, _expected, sizeof(_expected), position); \
259 if (_old == _expected) { \
260 model_rmw_action_helper(addr, (uint64_t) _desired, atomic_index, position); \
261 *((volatile uint ## size ## _t *)addr) = desired; \
262 thread_id_t tid = thread_current()->get_id(); \
263 for(int i=0;i < size / 8;i++) { \
264 recordWrite(tid, (void *)(((char *)addr)+i)); \
266 return _expected; } \
268 model_rmwc_action_helper(addr, atomic_index, position); _expected = _old; return _old; } \
271 // atomic_compare_exchange version 1: the CmpOperand (corresponds to expected)
272 // extracted from LLVM IR is an integer type.
273 #define CDSATOMICCASV1(size) \
274 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) { \
275 _ATOMIC_CMPSWP_(size, addr, expected, desired, atomic_index_succ, position); \
283 // atomic_compare_exchange version 2
284 #define CDSATOMICCASV2(size) \
285 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) { \
286 uint ## size ## _t ret = cds_atomic_compare_exchange ## size ## _v1(addr, *expected, desired, atomic_index_succ, atomic_index_fail, position); \
287 if (ret == *expected) {return true;} else {return false;} \
295 // cds atomic thread fence
297 void cds_atomic_thread_fence(int atomic_index, const char * position) {
298 model->switch_to_master(
299 new ModelAction(ATOMIC_FENCE, position, orders[atomic_index], FENCE_LOCATION)
304 #define _ATOMIC_CMPSWP_( __a__, __e__, __m__, __x__ ) \
305 ({ volatile __typeof__((__a__)->__f__)* __p__ = & ((__a__)->__f__); \
306 __typeof__(__e__) __q__ = (__e__); \
307 __typeof__(__m__) __v__ = (__m__); \
309 __typeof__((__a__)->__f__) __t__=(__typeof__((__a__)->__f__)) model_rmwr_action((void *)__p__, __x__); \
310 if (__t__ == * __q__ ) { \
311 model_rmw_action((void *)__p__, __x__, (uint64_t) __v__); __r__ = true; } \
312 else { model_rmwc_action((void *)__p__, __x__); *__q__ = __t__; __r__ = false;} \
315 #define _ATOMIC_FENCE_( __x__ ) \
316 ({ model_fence_action(__x__);})
321 #define _ATOMIC_MODIFY_( __a__, __o__, __m__, __x__ ) \
322 ({ volatile __typeof__((__a__)->__f__)* __p__ = & ((__a__)->__f__); \
323 __typeof__((__a__)->__f__) __old__=(__typeof__((__a__)->__f__)) model_rmwr_action((void *)__p__, __x__); \
324 __typeof__(__m__) __v__ = (__m__); \
325 __typeof__((__a__)->__f__) __copy__= __old__; \
326 __copy__ __o__ __v__; \
327 model_rmw_action((void *)__p__, __x__, (uint64_t) __copy__); \
328 __old__ = __old__; Silence clang (-Wunused-value) \
332 void cds_func_entry(const char * funcName) {
335 Thread * th = thread_current();
338 ModelHistory *history = model->get_history();
339 if ( !history->getFuncMap()->contains(funcName) ) {
340 // add func id to func map
341 func_id = history->get_func_counter();
342 history->incr_func_counter();
343 history->getFuncMap()->put(funcName, func_id);
345 // add func id to reverse func map
346 ModelVector<const char *> * func_map_rev = history->getFuncMapRev();
347 if ( func_map_rev->size() <= func_id )
348 func_map_rev->resize( func_id + 1 );
349 func_map_rev->at(func_id) = funcName;
351 func_id = history->getFuncMap()->get(funcName);
354 history->enter_function(func_id, th->get_id());
357 void cds_func_exit(const char * funcName) {
360 Thread * th = thread_current();
363 ModelHistory *history = model->get_history();
364 func_id = history->getFuncMap()->get(funcName);
366 /* func_id not found; this could happen in the case where a function calls cds_func_entry
367 * when the model has been defined yet, but then an atomic inside the function initializes
368 * the model. And then cds_func_exit is called upon the function exiting.
373 history->exit_function(func_id, th->get_id());