9 #include "snapshot-interface.h"
10 #include "threads-model.h"
13 memory_order orders[6] = {
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 CDSATOMICINT(size) \
98 void cds_atomic_init ## size (void * obj, uint ## size ## _t val, const char * position) { \
100 model->switch_to_master(new ModelAction(ATOMIC_INIT, position, memory_order_relaxed, obj, (uint64_t) val)); \
101 *((uint ## size ## _t *)obj) = val; \
102 thread_id_t tid = thread_current()->get_id(); \
103 for(int i=0;i < size / 8;i++) { \
104 recordWrite(tid, (void *)(((char *)obj)+i)); \
114 #define CDSATOMICLOAD(size) \
115 uint ## size ## _t cds_atomic_load ## size(void * obj, int atomic_index, const char * position) { \
117 return (uint ## size ## _t) model->switch_to_master( \
118 new ModelAction(ATOMIC_READ, position, orders[atomic_index], obj)); \
127 #define CDSATOMICSTORE(size) \
128 void cds_atomic_store ## size(void * obj, uint ## size ## _t val, int atomic_index, const char * position) { \
130 model->switch_to_master(new ModelAction(ATOMIC_WRITE, position, orders[atomic_index], obj, (uint64_t) val)); \
131 *((uint ## size ## _t *)obj) = val; \
132 thread_id_t tid = thread_current()->get_id(); \
133 for(int i=0;i < size / 8;i++) { \
134 recordWrite(tid, (void *)(((char *)obj)+i)); \
144 #define _ATOMIC_RMW_(__op__, size, addr, val, atomic_index, position) \
146 uint ## size ## _t _old = model_rmwr_action_helper(addr, atomic_index, position); \
147 uint ## size ## _t _copy = _old; \
148 uint ## size ## _t _val = val; \
150 model_rmw_action_helper(addr, (uint64_t) _copy, atomic_index, position); \
151 *((uint ## size ## _t *)addr) = _copy; \
152 thread_id_t tid = thread_current()->get_id(); \
153 for(int i=0;i < size / 8;i++) { \
154 recordWrite(tid, (void *)(((char *)addr)+i)); \
159 // cds atomic exchange
160 #define CDSATOMICEXCHANGE(size) \
161 uint ## size ## _t cds_atomic_exchange ## size(void* addr, uint ## size ## _t val, int atomic_index, const char * position) { \
162 _ATOMIC_RMW_( =, size, addr, val, atomic_index, position); \
166 CDSATOMICEXCHANGE(16)
167 CDSATOMICEXCHANGE(32)
168 CDSATOMICEXCHANGE(64)
170 // cds atomic fetch add
171 #define CDSATOMICADD(size) \
172 uint ## size ## _t cds_atomic_fetch_add ## size(void* addr, uint ## size ## _t val, int atomic_index, const char * position) { \
173 _ATOMIC_RMW_( +=, size, addr, val, atomic_index, position); \
181 // cds atomic fetch sub
182 #define CDSATOMICSUB(size) \
183 uint ## size ## _t cds_atomic_fetch_sub ## size(void* addr, uint ## size ## _t val, int atomic_index, const char * position) { \
184 _ATOMIC_RMW_( -=, size, addr, val, atomic_index, position); \
192 // cds atomic fetch and
193 #define CDSATOMICAND(size) \
194 uint ## size ## _t cds_atomic_fetch_and ## size(void* addr, uint ## size ## _t val, int atomic_index, const char * position) { \
195 _ATOMIC_RMW_( &=, size, addr, val, atomic_index, position); \
203 // cds atomic fetch or
204 #define CDSATOMICOR(size) \
205 uint ## size ## _t cds_atomic_fetch_or ## size(void* addr, uint ## size ## _t val, int atomic_index, const char * position) { \
206 _ATOMIC_RMW_( |=, size, addr, val, atomic_index, position); \
214 // cds atomic fetch xor
215 #define CDSATOMICXOR(size) \
216 uint ## size ## _t cds_atomic_fetch_xor ## size(void* addr, uint ## size ## _t val, int atomic_index, const char * position) { \
217 _ATOMIC_RMW_( ^=, size, addr, val, atomic_index, position); \
225 // cds atomic compare and exchange
226 // In order to accomodate the LLVM PASS, the return values are not true or false.
228 #define _ATOMIC_CMPSWP_WEAK_ _ATOMIC_CMPSWP_
229 #define _ATOMIC_CMPSWP_(size, addr, expected, desired, atomic_index, position) \
231 uint ## size ## _t _desired = desired; \
232 uint ## size ## _t _expected = expected; \
233 uint ## size ## _t _old = model_rmwrcas_action_helper(addr, atomic_index, _expected, sizeof(_expected), position); \
234 if (_old == _expected) { \
235 model_rmw_action_helper(addr, (uint64_t) _desired, atomic_index, position); \
236 *((uint ## size ## _t *)addr) = desired; \
237 thread_id_t tid = thread_current()->get_id(); \
238 for(int i=0;i < size / 8;i++) { \
239 recordWrite(tid, (void *)(((char *)addr)+i)); \
241 return _expected; } \
243 model_rmwc_action_helper(addr, atomic_index, position); _expected = _old; return _old; } \
246 // atomic_compare_exchange version 1: the CmpOperand (corresponds to expected)
247 // extracted from LLVM IR is an integer type.
248 #define CDSATOMICCASV1(size) \
249 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) { \
250 _ATOMIC_CMPSWP_(size, addr, expected, desired, atomic_index_succ, position); \
258 // atomic_compare_exchange version 2
259 #define CDSATOMICCASV2(size) \
260 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) { \
261 uint ## size ## _t ret = cds_atomic_compare_exchange ## size ## _v1(addr, *expected, desired, atomic_index_succ, atomic_index_fail, position); \
262 if (ret == *expected) {return true;} else {return false;} \
270 // cds atomic thread fence
272 void cds_atomic_thread_fence(int atomic_index, const char * position) {
273 model->switch_to_master(
274 new ModelAction(ATOMIC_FENCE, position, orders[atomic_index], FENCE_LOCATION)
279 #define _ATOMIC_CMPSWP_( __a__, __e__, __m__, __x__ ) \
280 ({ volatile __typeof__((__a__)->__f__)* __p__ = & ((__a__)->__f__); \
281 __typeof__(__e__) __q__ = (__e__); \
282 __typeof__(__m__) __v__ = (__m__); \
284 __typeof__((__a__)->__f__) __t__=(__typeof__((__a__)->__f__)) model_rmwr_action((void *)__p__, __x__); \
285 if (__t__ == * __q__ ) { \
286 model_rmw_action((void *)__p__, __x__, (uint64_t) __v__); __r__ = true; } \
287 else { model_rmwc_action((void *)__p__, __x__); *__q__ = __t__; __r__ = false;} \
290 #define _ATOMIC_FENCE_( __x__ ) \
291 ({ model_fence_action(__x__);})
296 #define _ATOMIC_MODIFY_( __a__, __o__, __m__, __x__ ) \
297 ({ volatile __typeof__((__a__)->__f__)* __p__ = & ((__a__)->__f__); \
298 __typeof__((__a__)->__f__) __old__=(__typeof__((__a__)->__f__)) model_rmwr_action((void *)__p__, __x__); \
299 __typeof__(__m__) __v__ = (__m__); \
300 __typeof__((__a__)->__f__) __copy__= __old__; \
301 __copy__ __o__ __v__; \
302 model_rmw_action((void *)__p__, __x__, (uint64_t) __copy__); \
303 __old__ = __old__; Silence clang (-Wunused-value) \
307 void cds_func_entry(const char * funcName) {
310 Thread * th = thread_current();
313 ModelHistory *history = model->get_history();
314 if ( !history->getFuncMap()->contains(funcName) ) {
315 // add func id to func map
316 func_id = history->get_func_counter();
317 history->incr_func_counter();
318 history->getFuncMap()->put(funcName, func_id);
320 // add func id to reverse func map
321 ModelVector<const char *> * func_map_rev = history->getFuncMapRev();
322 if ( func_map_rev->size() <= func_id )
323 func_map_rev->resize( func_id + 1 );
324 func_map_rev->at(func_id) = funcName;
326 func_id = history->getFuncMap()->get(funcName);
329 history->enter_function(func_id, th->get_id());
332 void cds_func_exit(const char * funcName) {
335 Thread * th = thread_current();
338 ModelHistory *history = model->get_history();
339 func_id = history->getFuncMap()->get(funcName);
341 /* func_id not found; this could happen in the case where a function calls cds_func_entry
342 * when the model has been defined yet, but then an atomic inside the function initializes
343 * the model. And then cds_func_exit is called upon the function exiting.
348 history->exit_function(func_id, th->get_id());