9 #include "threads-model.h"
11 memory_order orders[6] = {
12 memory_order_relaxed, memory_order_consume, memory_order_acquire,
13 memory_order_release, memory_order_acq_rel, memory_order_seq_cst
16 /** Performs a read action.*/
17 uint64_t model_read_action(void * obj, memory_order ord) {
18 return model->switch_to_master(new ModelAction(ATOMIC_READ, ord, obj));
21 /** Performs a write action.*/
22 void model_write_action(void * obj, memory_order ord, uint64_t val) {
23 model->switch_to_master(new ModelAction(ATOMIC_WRITE, ord, obj, val));
26 /** Performs an init action. */
27 void model_init_action(void * obj, uint64_t val) {
28 model->switch_to_master(new ModelAction(ATOMIC_INIT, memory_order_relaxed, obj, val));
32 * Performs the read part of a RMW action. The next action must either be the
33 * write part of the RMW action or an explicit close out of the RMW action w/o
36 uint64_t model_rmwr_action(void *obj, memory_order ord) {
37 return model->switch_to_master(new ModelAction(ATOMIC_RMWR, ord, obj));
41 * Performs the read part of a RMW CAS action. The next action must
42 * either be the write part of the RMW action or an explicit close out
43 * of the RMW action w/o a write.
45 uint64_t model_rmwrcas_action(void *obj, memory_order ord, uint64_t oldval, int size) {
46 return model->switch_to_master(new ModelAction(ATOMIC_RMWRCAS, ord, obj, oldval, size));
50 /** Performs the write part of a RMW action. */
51 void model_rmw_action(void *obj, memory_order ord, uint64_t val) {
52 model->switch_to_master(new ModelAction(ATOMIC_RMW, ord, obj, val));
55 /** Closes out a RMW action without doing a write. */
56 void model_rmwc_action(void *obj, memory_order ord) {
57 model->switch_to_master(new ModelAction(ATOMIC_RMWC, ord, obj));
60 /** Issues a fence operation. */
61 void model_fence_action(memory_order ord) {
62 model->switch_to_master(new ModelAction(ATOMIC_FENCE, ord, FENCE_LOCATION));
65 /* --- helper functions --- */
66 uint64_t model_rmwrcas_action_helper(void *obj, int atomic_index, uint64_t oldval, int size, const char *position) {
67 return model->switch_to_master(
68 new ModelAction(ATOMIC_RMWRCAS, position, orders[atomic_index], obj, oldval, size)
72 uint64_t model_rmwr_action_helper(void *obj, int atomic_index, const char *position) {
73 return model->switch_to_master(
74 new ModelAction(ATOMIC_RMWR, position, orders[atomic_index], obj)
78 void model_rmw_action_helper(void *obj, uint64_t val, int atomic_index, const char * position) {
79 model->switch_to_master(
80 new ModelAction(ATOMIC_RMW, position, orders[atomic_index], obj, val)
84 void model_rmwc_action_helper(void *obj, int atomic_index, const char *position) {
85 model->switch_to_master(
86 new ModelAction(ATOMIC_RMWC, position, orders[atomic_index], obj)
91 void cds_atomic_init8(void * obj, uint8_t val, const char * position) {
92 model->switch_to_master(
93 new ModelAction(ATOMIC_INIT, position, memory_order_relaxed, obj, (uint64_t) val)
96 void cds_atomic_init16(void * obj, uint16_t val, const char * position) {
97 model->switch_to_master(
98 new ModelAction(ATOMIC_INIT, position, memory_order_relaxed, obj, (uint64_t) val)
101 void cds_atomic_init32(void * obj, uint32_t val, const char * position) {
102 model->switch_to_master(
103 new ModelAction(ATOMIC_INIT, position, memory_order_relaxed, obj, (uint64_t) val)
106 void cds_atomic_init64(void * obj, uint64_t val, const char * position) {
107 model->switch_to_master(
108 new ModelAction(ATOMIC_INIT, position, memory_order_relaxed, obj, val)
114 uint8_t cds_atomic_load8(void * obj, int atomic_index, const char * position) {
115 return (uint8_t) ( model->switch_to_master(
116 new ModelAction(ATOMIC_READ, position, orders[atomic_index], obj))
119 uint16_t cds_atomic_load16(void * obj, int atomic_index, const char * position) {
120 return (uint16_t) ( model->switch_to_master(
121 new ModelAction(ATOMIC_READ, position, orders[atomic_index], obj))
124 uint32_t cds_atomic_load32(void * obj, int atomic_index, const char * position) {
125 return (uint32_t) ( model->switch_to_master(
126 new ModelAction(ATOMIC_READ, position, orders[atomic_index], obj))
129 uint64_t cds_atomic_load64(void * obj, int atomic_index, const char * position) {
130 return model->switch_to_master(
131 new ModelAction(ATOMIC_READ, position, orders[atomic_index], obj)
136 void cds_atomic_store8(void * obj, uint8_t val, int atomic_index, const char * position) {
137 model->switch_to_master(
138 new ModelAction(ATOMIC_WRITE, position, orders[atomic_index], obj, (uint64_t) val)
141 void cds_atomic_store16(void * obj, uint16_t val, int atomic_index, const char * position) {
142 model->switch_to_master(
143 new ModelAction(ATOMIC_WRITE, position, orders[atomic_index], obj, (uint64_t) val)
146 void cds_atomic_store32(void * obj, uint32_t val, int atomic_index, const char * position) {
147 model->switch_to_master(
148 new ModelAction(ATOMIC_WRITE, position, orders[atomic_index], obj, (uint64_t) val)
151 void cds_atomic_store64(void * obj, uint64_t val, int atomic_index, const char * position) {
152 model->switch_to_master(
153 new ModelAction(ATOMIC_WRITE, position, orders[atomic_index], obj, val)
157 #define _ATOMIC_RMW_(__op__, size, addr, val, atomic_index, position) \
159 uint ## size ## _t _old = model_rmwr_action_helper(addr, atomic_index, position); \
160 uint ## size ## _t _copy = _old; \
161 uint ## size ## _t _val = val; \
163 model_rmw_action_helper(addr, (uint64_t) _copy, atomic_index, position); \
167 // cds atomic exchange
168 uint8_t cds_atomic_exchange8(void* addr, uint8_t val, int atomic_index, const char * position) {
169 _ATOMIC_RMW_( =, 8, addr, val, atomic_index, position);
171 uint16_t cds_atomic_exchange16(void* addr, uint16_t val, int atomic_index, const char * position) {
172 _ATOMIC_RMW_( =, 16, addr, val, atomic_index, position);
174 uint32_t cds_atomic_exchange32(void* addr, uint32_t val, int atomic_index, const char * position) {
175 _ATOMIC_RMW_( =, 32, addr, val, atomic_index, position);
177 uint64_t cds_atomic_exchange64(void* addr, uint64_t val, int atomic_index, const char * position) {
178 _ATOMIC_RMW_( =, 64, addr, val, atomic_index, position);
181 // cds atomic fetch add
182 uint8_t cds_atomic_fetch_add8(void* addr, uint8_t val, int atomic_index, const char * position) {
183 _ATOMIC_RMW_( +=, 8, addr, val, atomic_index, position);
185 uint16_t cds_atomic_fetch_add16(void* addr, uint16_t val, int atomic_index, const char * position) {
186 _ATOMIC_RMW_( +=, 16, addr, val, atomic_index, position);
188 uint32_t cds_atomic_fetch_add32(void* addr, uint32_t val, int atomic_index, const char * position) {
189 _ATOMIC_RMW_( +=, 32, addr, val, atomic_index, position);
191 uint64_t cds_atomic_fetch_add64(void* addr, uint64_t val, int atomic_index, const char * position) {
192 _ATOMIC_RMW_( +=, 64, addr, val, atomic_index, position);
195 // cds atomic fetch sub
196 uint8_t cds_atomic_fetch_sub8(void* addr, uint8_t val, int atomic_index, const char * position) {
197 _ATOMIC_RMW_( -=, 8, addr, val, atomic_index, position);
199 uint16_t cds_atomic_fetch_sub16(void* addr, uint16_t val, int atomic_index, const char * position) {
200 _ATOMIC_RMW_( -=, 16, addr, val, atomic_index, position);
202 uint32_t cds_atomic_fetch_sub32(void* addr, uint32_t val, int atomic_index, const char * position) {
203 _ATOMIC_RMW_( -=, 32, addr, val, atomic_index, position);
205 uint64_t cds_atomic_fetch_sub64(void* addr, uint64_t val, int atomic_index, const char * position) {
206 _ATOMIC_RMW_( -=, 64, addr, val, atomic_index, position);
209 // cds atomic fetch and
210 uint8_t cds_atomic_fetch_and8(void* addr, uint8_t val, int atomic_index, const char * position) {
211 _ATOMIC_RMW_( &=, 8, addr, val, atomic_index, position);
213 uint16_t cds_atomic_fetch_and16(void* addr, uint16_t val, int atomic_index, const char * position) {
214 _ATOMIC_RMW_( &=, 16, addr, val, atomic_index, position);
216 uint32_t cds_atomic_fetch_and32(void* addr, uint32_t val, int atomic_index, const char * position) {
217 _ATOMIC_RMW_( &=, 32, addr, val, atomic_index, position);
219 uint64_t cds_atomic_fetch_and64(void* addr, uint64_t val, int atomic_index, const char * position) {
220 _ATOMIC_RMW_( &=, 64, addr, val, atomic_index, position);
223 // cds atomic fetch or
224 uint8_t cds_atomic_fetch_or8(void* addr, uint8_t val, int atomic_index, const char * position) {
225 _ATOMIC_RMW_( |=, 8, addr, val, atomic_index, position);
227 uint16_t cds_atomic_fetch_or16(void* addr, uint16_t val, int atomic_index, const char * position) {
228 _ATOMIC_RMW_( |=, 16, addr, val, atomic_index, position);
230 uint32_t cds_atomic_fetch_or32(void* addr, uint32_t val, int atomic_index, const char * position) {
231 _ATOMIC_RMW_( |=, 32, addr, val, atomic_index, position);
233 uint64_t cds_atomic_fetch_or64(void* addr, uint64_t val, int atomic_index, const char * position) {
234 _ATOMIC_RMW_( |=, 64, addr, val, atomic_index, position);
237 // cds atomic fetch xor
238 uint8_t cds_atomic_fetch_xor8(void* addr, uint8_t val, int atomic_index, const char * position) {
239 _ATOMIC_RMW_( ^=, 8, addr, val, atomic_index, position);
241 uint16_t cds_atomic_fetch_xor16(void* addr, uint16_t val, int atomic_index, const char * position) {
242 _ATOMIC_RMW_( ^=, 16, addr, val, atomic_index, position);
244 uint32_t cds_atomic_fetch_xor32(void* addr, uint32_t val, int atomic_index, const char * position) {
245 _ATOMIC_RMW_( ^=, 32, addr, val, atomic_index, position);
247 uint64_t cds_atomic_fetch_xor64(void* addr, uint64_t val, int atomic_index, const char * position) {
248 _ATOMIC_RMW_( ^=, 64, addr, val, atomic_index, position);
251 // cds atomic compare and exchange
252 // In order to accomodate the LLVM PASS, the return values are not true or false.
254 #define _ATOMIC_CMPSWP_WEAK_ _ATOMIC_CMPSWP_
255 #define _ATOMIC_CMPSWP_(size, addr, expected, desired, atomic_index, position) \
257 uint ## size ## _t _desired = desired; \
258 uint ## size ## _t _expected = expected; \
259 uint ## size ## _t _old = model_rmwrcas_action_helper(addr, atomic_index, _expected, sizeof(_expected), position); \
260 if (_old == _expected) { \
261 model_rmw_action_helper(addr, (uint64_t) _desired, atomic_index, position); return _expected; } \
263 model_rmwc_action_helper(addr, atomic_index, position); _expected = _old; return _old; } \
266 // atomic_compare_exchange version 1: the CmpOperand (corresponds to expected)
267 // extracted from LLVM IR is an integer type.
269 uint8_t cds_atomic_compare_exchange8_v1(void* addr, uint8_t expected, uint8_t desired,
270 int atomic_index_succ, int atomic_index_fail, const char *position )
272 _ATOMIC_CMPSWP_(8, addr, expected, desired,
273 atomic_index_succ, position);
275 uint16_t cds_atomic_compare_exchange16_v1(void* addr, uint16_t expected, uint16_t desired,
276 int atomic_index_succ, int atomic_index_fail, const char *position)
278 _ATOMIC_CMPSWP_(16, addr, expected, desired,
279 atomic_index_succ, position);
281 uint32_t cds_atomic_compare_exchange32_v1(void* addr, uint32_t expected, uint32_t desired,
282 int atomic_index_succ, int atomic_index_fail, const char *position)
284 _ATOMIC_CMPSWP_(32, addr, expected, desired,
285 atomic_index_succ, position);
287 uint64_t cds_atomic_compare_exchange64_v1(void* addr, uint64_t expected, uint64_t desired,
288 int atomic_index_succ, int atomic_index_fail, const char *position)
290 _ATOMIC_CMPSWP_(64, addr, expected, desired,
291 atomic_index_succ, position);
294 // atomic_compare_exchange version 2
295 bool cds_atomic_compare_exchange8_v2(void* addr, uint8_t* expected, uint8_t desired,
296 int atomic_index_succ, int atomic_index_fail, const char *position )
298 uint8_t ret = cds_atomic_compare_exchange8_v1(addr, *expected,
299 desired, atomic_index_succ, atomic_index_fail, position);
300 if (ret == *expected) return true;
303 bool cds_atomic_compare_exchange16_v2(void* addr, uint16_t* expected, uint16_t desired,
304 int atomic_index_succ, int atomic_index_fail, const char *position)
306 uint16_t ret = cds_atomic_compare_exchange16_v1(addr, *expected,
307 desired, atomic_index_succ, atomic_index_fail, position);
308 if (ret == *expected) return true;
311 bool cds_atomic_compare_exchange32_v2(void* addr, uint32_t* expected, uint32_t desired,
312 int atomic_index_succ, int atomic_index_fail, const char *position)
314 uint32_t ret = cds_atomic_compare_exchange32_v1(addr, *expected,
315 desired, atomic_index_succ, atomic_index_fail, position);
316 if (ret == *expected) return true;
319 bool cds_atomic_compare_exchange64_v2(void* addr, uint64_t* expected, uint64_t desired,
320 int atomic_index_succ, int atomic_index_fail, const char *position)
322 uint64_t ret = cds_atomic_compare_exchange64_v1(addr, *expected,
323 desired, atomic_index_succ, atomic_index_fail, position);
324 if (ret == *expected) return true;
329 // cds atomic thread fence
331 void cds_atomic_thread_fence(int atomic_index, const char * position) {
332 model->switch_to_master(
333 new ModelAction(ATOMIC_FENCE, position, orders[atomic_index], FENCE_LOCATION)
338 #define _ATOMIC_CMPSWP_( __a__, __e__, __m__, __x__ ) \
339 ({ volatile __typeof__((__a__)->__f__)* __p__ = & ((__a__)->__f__); \
340 __typeof__(__e__) __q__ = (__e__); \
341 __typeof__(__m__) __v__ = (__m__); \
343 __typeof__((__a__)->__f__) __t__=(__typeof__((__a__)->__f__)) model_rmwr_action((void *)__p__, __x__); \
344 if (__t__ == * __q__ ) { \
345 model_rmw_action((void *)__p__, __x__, (uint64_t) __v__); __r__ = true; } \
346 else { model_rmwc_action((void *)__p__, __x__); *__q__ = __t__; __r__ = false;} \
349 #define _ATOMIC_FENCE_( __x__ ) \
350 ({ model_fence_action(__x__);})
355 #define _ATOMIC_MODIFY_( __a__, __o__, __m__, __x__ ) \
356 ({ volatile __typeof__((__a__)->__f__)* __p__ = & ((__a__)->__f__); \
357 __typeof__((__a__)->__f__) __old__=(__typeof__((__a__)->__f__)) model_rmwr_action((void *)__p__, __x__); \
358 __typeof__(__m__) __v__ = (__m__); \
359 __typeof__((__a__)->__f__) __copy__= __old__; \
360 __copy__ __o__ __v__; \
361 model_rmw_action((void *)__p__, __x__, (uint64_t) __copy__); \
362 __old__ = __old__; Silence clang (-Wunused-value) \
366 void cds_func_entry(const char * funcName) {
369 Thread * th = thread_current();
372 ModelHistory *history = model->get_history();
373 if ( !history->getFuncMap()->contains(funcName) ) {
374 func_id = history->get_func_counter();
375 history->incr_func_counter();
377 history->getFuncMap()->put(funcName, func_id);
379 func_id = history->getFuncMap()->get(funcName);
382 history->enter_function(func_id, th->get_id());
385 void cds_func_exit(const char * funcName) {
388 Thread * th = thread_current();
391 ModelHistory *history = model->get_history();
392 func_id = history->getFuncMap()->get(funcName);
394 history->exit_function(func_id, th->get_id());