-#define _ATOMIC_CMPSWP_(size, addr, expected, desired, atomic_index) \
-({ \
- uint##size##_t _desired = desired; \
- uint##size##_t _expected = expected; \
- uint##size##_t _old = model_rmwr_action_helper(addr, atomic_index); \
- if (_old == _expected ) { \
- model_rmw_action_helper(addr, atomic_index, (uint64_t) _desired ); return _expected; } \
- else { \
- model_rmwc_action_helper(addr, atomic_index); _expected = _old; return _old; } \
-})
-
-// expected is supposed to be a pointer to an address, but the CmpOperand
-// extracted from LLVM IR is an integer type.
-
-uint8_t cds_atomic_compare_exchange8(void* addr, uint8_t expected,
- uint8_t desired, int atomic_index_succ, int atomic_index_fail ) {
- _ATOMIC_CMPSWP_(8, addr, expected, desired, atomic_index_succ );
-}
-uint16_t cds_atomic_compare_exchange16(void* addr, uint16_t expected,
- uint16_t desired, int atomic_index_succ, int atomic_index_fail ) {
- _ATOMIC_CMPSWP_(16, addr, expected, desired, atomic_index_succ );
-}
-uint32_t cds_atomic_compare_exchange32(void* addr, uint32_t expected,
- uint32_t desired, int atomic_index_succ, int atomic_index_fail ) {
- _ATOMIC_CMPSWP_(32, addr, expected, desired, atomic_index_succ );
-}
-uint64_t cds_atomic_compare_exchange64(void* addr, uint64_t expected,
- uint64_t desired, int atomic_index_succ, int atomic_index_fail ) {
- _ATOMIC_CMPSWP_(64, addr, expected, desired, atomic_index_succ );
-}
+#define _ATOMIC_CMPSWP_(size, addr, expected, desired, atomic_index, position) \
+ ({ \
+ uint ## size ## _t _desired = desired; \
+ uint ## size ## _t _expected = expected; \
+ uint ## size ## _t _old = model_rmwrcas_action_helper(addr, atomic_index, _expected, sizeof(_expected), position); \
+ if (_old == _expected) { \
+ model_rmw_action_helper(addr, (uint64_t) _desired, atomic_index, position); \
+ *((uint ## size ## _t *)addr) = desired; \
+ thread_id_t tid = thread_current()->get_id(); \
+ for(int i=0;i < size / 8;i++) { \
+ recordWrite(tid, (void *)(((char *)addr)+i)); \
+ } \
+ return _expected; } \
+ else { \
+ model_rmwc_action_helper(addr, atomic_index, position); _expected = _old; return _old; } \
+ })
+
+// atomic_compare_exchange version 1: the CmpOperand (corresponds to expected)
+// extracted from LLVM IR is an integer type.
+#define CDSATOMICCASV1(size) \
+ 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) { \
+ _ATOMIC_CMPSWP_(size, addr, expected, desired, atomic_index_succ, position); \
+ }
+
+CDSATOMICCASV1(8)
+CDSATOMICCASV1(16)
+CDSATOMICCASV1(32)
+CDSATOMICCASV1(64)
+
+// atomic_compare_exchange version 2
+#define CDSATOMICCASV2(size) \
+ 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) { \
+ uint ## size ## _t ret = cds_atomic_compare_exchange ## size ## _v1(addr, *expected, desired, atomic_index_succ, atomic_index_fail, position); \
+ if (ret == *expected) {return true;} else {return false;} \
+ }
+
+CDSATOMICCASV2(8)
+CDSATOMICCASV2(16)
+CDSATOMICCASV2(32)
+CDSATOMICCASV2(64)