From c0cce761f8e04183ddd506bdb814d66bab5f4a78 Mon Sep 17 00:00:00 2001 From: Yann Ylavic Date: Thu, 9 Feb 2023 13:36:18 +0000 Subject: apr_atomic: Generic apr_atomic_read64() needs a mutex on 32bit systems (tearing). A 64bit load on a 32 bit CPU/system uses two instructions (tearing), so ensure atomicity with regard to other atomic functions by using the (same) lock. test_atomics_threaded_setread64() fails because of this on 32bit systems. PR 66457. * atomic/unix/mutex64.c(apr_atomic_read64): Use locking when APR_SIZEOF_VOIDP < 8 git-svn-id: https://svn.apache.org/repos/asf/apr/apr/trunk@1907541 13f79535-47bb-0310-9956-ffa450edef68 --- atomic/unix/mutex64.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/atomic/unix/mutex64.c b/atomic/unix/mutex64.c index 9fc44af61..9f1f1df6d 100644 --- a/atomic/unix/mutex64.c +++ b/atomic/unix/mutex64.c @@ -96,7 +96,26 @@ apr_status_t apr__atomic_generic64_init(apr_pool_t *p) APR_DECLARE(apr_uint64_t) apr_atomic_read64(volatile apr_uint64_t *mem) { + /* On 32bit CPUs this loads with two instructions (tearing), + * so a lock is needed to ensure atomicity. + * + * APR_SIZEOF_VOIDP is probably not the right check for 32 vs 64 bits CPUs + * but it spares an (hardly-)exhaustive list of supported CPUs (and using + * assembly). If APR_SIZEOF_VOIDP==4 means that the compiler generates + * 32bit instructions (-m32 or whatever) then it's the right check though. + */ +#if APR_SIZEOF_VOIDP >= 8 return *mem; +#else + apr_uint64_t cur_value; + DECLARE_MUTEX_LOCKED(mutex, mem); + + cur_value = *mem; + + MUTEX_UNLOCK(mutex); + + return cur_value; +#endif } APR_DECLARE(void) apr_atomic_set64(volatile apr_uint64_t *mem, apr_uint64_t val) -- cgit v1.2.1