Skip to contentSkip to conversation

atomic_cmpxchg: Handle spurious failure of __atomic_compare_exchange_n in Power8 and ARMv8

PATCH In review Junchang Wang 2 participants 2 messages Oct 26, 2018 – Oct 27, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 atomic_cmpxchg: Handle spurious failure of __atomic_compare_exchange_n in Power8 and ARMv8

If the 4th parameter (field *weak*) of __atomic_compare_exchange_n is set, it may refuse to perform the CAS operation and return a failure, even if the value of field *old* is equal to the content of the specified memory location. This happens in architectures where LL/SC is used to emulate CAS primitive (e.g., PPC and ARM).

This patch returns the *old* value if __atomic_compare_exchange_n has been successfully performed; otherwise, *old*+1 is returned, which forces the caller to retry the CAS loop.

---
 CodeSamples/api-pthreads/api-gcc.h | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
CodeSamples/api-pthreads/api-gcc.h +2 -3
index 1dd26ca..3afe340 100644
@@ -168,9 +168,8 @@ struct __xchg_dummy {
 ({ \
 	typeof(*ptr) _____actual = (o); \
 	\
-	(void)__atomic_compare_exchange_n(ptr, (void *)&_____actual, (n), 1, \
-					  __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); \
-	_____actual; \
+	__atomic_compare_exchange_n(ptr, (void *)&_____actual, (n), 1, \
+			__ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) ? (o) : (o)+1; \
 })
 
 static __inline__ int atomic_cmpxchg(atomic_t *v, int old, int new)
Signature · 1 line
2.7.4
#1 lore raw
Quoted 6 lines · Junchang Wang

If the 4th parameter (field *weak*) of __atomic_compare_exchange_n is set, it may refuse to perform the CAS operation and return a failure, even if the value of field *old* is equal to the content of the specified memory location. This happens in architectures where LL/SC is used to emulate CAS primitive (e.g., PPC and ARM).

Quoted 6 lines

This patch returns the *old* value if __atomic_compare_exchange_n has been successfully performed; otherwise, *old*+1 is returned, which forces the caller to retry the CAS loop.

Very good, queued and pushed with adjustments to the commit log above, thank you!!!

Please let me know if further adjustments are required.

Thanx, Paul

Quoted 23 lines
---
 CodeSamples/api-pthreads/api-gcc.h | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
CodeSamples/api-pthreads/api-gcc.h +2 -3
index 1dd26ca..3afe340 100644
@@ -168,9 +168,8 @@ struct __xchg_dummy {
 ({ \
 	typeof(*ptr) _____actual = (o); \
 	\
-	(void)__atomic_compare_exchange_n(ptr, (void *)&_____actual, (n), 1, \
-					  __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); \
-	_____actual; \
+	__atomic_compare_exchange_n(ptr, (void *)&_____actual, (n), 1, \
+			__ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) ? (o) : (o)+1; \
 })
 
 static __inline__ int atomic_cmpxchg(atomic_t *v, int old, int new)
Signature · 1 line
2.7.4