atomic_cmpxchg: Handle spurious failure of __atomic_compare_exchange_n in Power8 and ARMv8
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
@@ -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
Quoted 6 lines · Junchang Wang
If the 4th parameter (field *weak*) of
__atomic_compare_exchange_nis 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_nhas been successfully performed; otherwise, *old*+1 is returned, which forces the caller to retry the CAS loop.Signed-off-by Junchang Wang <[email protected]>Signed-off-by kira Yokosawa <[email protected]>
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@@ -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