Return-Path: Delivered-To: apmail-apr-cvs-archive@www.apache.org Received: (qmail 48030 invoked from network); 8 Dec 2003 22:58:04 -0000 Received: from daedalus.apache.org (HELO mail.apache.org) (208.185.179.12) by minotaur-2.apache.org with SMTP; 8 Dec 2003 22:58:04 -0000 Received: (qmail 36692 invoked by uid 500); 8 Dec 2003 22:57:50 -0000 Delivered-To: apmail-apr-cvs-archive@apr.apache.org Received: (qmail 36602 invoked by uid 500); 8 Dec 2003 22:57:50 -0000 Mailing-List: contact cvs-help@apr.apache.org; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Reply-To: dev@apr.apache.org Delivered-To: mailing list cvs@apr.apache.org Received: (qmail 36589 invoked from network); 8 Dec 2003 22:57:50 -0000 Date: 8 Dec 2003 22:58:03 -0000 Message-ID: <20031208225803.48003.qmail@minotaur.apache.org> From: trawick@apache.org To: apr-cvs@apache.org Subject: cvs commit: apr/atomic/unix apr_atomic.c X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N trawick 2003/12/08 14:58:02 Modified: atomic/unix apr_atomic.c Log: changes to the apr atomic implementations for unix-ish boxes a) need to check for __ppc__ on Darwin to pick up the PPC non-portable atomics b) axe the historic checks for defined(apr_atomic_foo) when deciding whether to generate the default version of apr_atomic_foo; checking for APR_OVERRIDE_ATOMIC_FOO is sufficient c) if the platform has defined apr_atomic_cas32() but not all the other apr_atomic_foo32(), use apr_atomic_cas32() in a generic version that will be much preferable to the no-cas32 version (which must use locking) Revision Changes Path 1.35 +69 -33 apr/atomic/unix/apr_atomic.c Index: apr_atomic.c =================================================================== RCS file: /home/cvs/apr/atomic/unix/apr_atomic.c,v retrieving revision 1.34 retrieving revision 1.35 diff -u -r1.34 -r1.35 --- apr_atomic.c 8 Dec 2003 19:49:16 -0000 1.34 +++ apr_atomic.c 8 Dec 2003 22:58:02 -0000 1.35 @@ -175,7 +175,7 @@ #endif /* (__linux__ || __EMX__ || __FreeBSD__) && __i386__ */ -#if defined(__PPC__) && defined(__GNUC__) && !APR_FORCE_ATOMIC_GENERIC +#if (defined(__PPC__) || defined(__ppc__)) && defined(__GNUC__) && !APR_FORCE_ATOMIC_GENERIC APR_DECLARE(apr_uint32_t) apr_atomic_cas32(volatile apr_uint32_t *mem, apr_uint32_t swap, apr_uint32_t cmp) @@ -200,7 +200,7 @@ #endif /* __PPC__ && __GNUC__ */ -#if !defined(apr_atomic_init) && !defined(APR_OVERRIDE_ATOMIC_INIT) +#if !defined(APR_OVERRIDE_ATOMIC_INIT) #if APR_HAS_THREADS #define NUM_ATOMIC_HASH 7 @@ -226,12 +226,24 @@ #endif /* APR_HAS_THREADS */ return APR_SUCCESS; } -#endif /*!defined(apr_atomic_init) && !defined(APR_OVERRIDE_ATOMIC_INIT) */ +#endif /* !defined(APR_OVERRIDE_ATOMIC_INIT) */ /* abort() if 'x' does not evaluate to APR_SUCCESS. */ #define CHECK(x) do { if ((x) != APR_SUCCESS) abort(); } while (0) -#if !defined(apr_atomic_add32) && !defined(APR_OVERRIDE_ATOMIC_ADD32) +#if !defined(APR_OVERRIDE_ATOMIC_ADD32) +#if defined(APR_OVERRIDE_ATOMIC_CAS32) +apr_uint32_t apr_atomic_add32(volatile apr_uint32_t *mem, apr_uint32_t val) +{ + apr_uint32_t old_value, new_value; + + do { + old_value = *mem; + new_value = old_value + val; + } while (apr_atomic_cas32(mem, new_value, old_value) != old_value); + return old_value; +} +#else apr_uint32_t apr_atomic_add32(volatile apr_uint32_t *mem, apr_uint32_t val) { apr_uint32_t old_value; @@ -249,9 +261,21 @@ #endif /* APR_HAS_THREADS */ return old_value; } -#endif /*!defined(apr_atomic_sub32) && !defined(APR_OVERRIDE_ATOMIC_SUB32) */ +#endif /* defined(APR_OVERRIDE_ATOMIC_CAS32) */ +#endif /* !defined(APR_OVERRIDE_ATOMIC_SUB32) */ -#if !defined(apr_atomic_sub32) && !defined(APR_OVERRIDE_ATOMIC_SUB32) +#if !defined(APR_OVERRIDE_ATOMIC_SUB32) +#if defined(APR_OVERRIDE_ATOMIC_CAS32) +void apr_atomic_sub32(volatile apr_uint32_t *mem, apr_uint32_t val) +{ + apr_uint32_t old_value, new_value; + + do { + old_value = *mem; + new_value = old_value - val; + } while (apr_atomic_cas32(mem, new_value, old_value) != old_value); +} +#else void apr_atomic_sub32(volatile apr_uint32_t *mem, apr_uint32_t val) { #if APR_HAS_THREADS @@ -264,9 +288,10 @@ *mem -= val; #endif /* APR_HAS_THREADS */ } -#endif /*!defined(apr_atomic_sub32) && !defined(APR_OVERRIDE_ATOMIC_SUB32) */ +#endif /* defined(APR_OVERRIDE_ATOMIC_CAS32) */ +#endif /* !defined(APR_OVERRIDE_ATOMIC_SUB32) */ -#if !defined(apr_atomic_set32) && !defined(APR_OVERRIDE_ATOMIC_SET32) +#if !defined(APR_OVERRIDE_ATOMIC_SET32) void apr_atomic_set32(volatile apr_uint32_t *mem, apr_uint32_t val) { #if APR_HAS_THREADS @@ -279,29 +304,28 @@ *mem = val; #endif /* APR_HAS_THREADS */ } -#endif /*!defined(apr_atomic_set32) && !defined(APR_OVERRIDE_ATOMIC_SET32) */ +#endif /* !defined(APR_OVERRIDE_ATOMIC_SET32) */ -#if !defined(apr_atomic_inc32) && !defined(APR_OVERRIDE_ATOMIC_INC32) +#if !defined(APR_OVERRIDE_ATOMIC_INC32) apr_uint32_t apr_atomic_inc32(volatile apr_uint32_t *mem) { - apr_uint32_t old_value; - -#if APR_HAS_THREADS - apr_thread_mutex_t *lock = hash_mutex[ATOMIC_HASH(mem)]; - - CHECK(apr_thread_mutex_lock(lock)); - old_value = *mem; - (*mem)++; - CHECK(apr_thread_mutex_unlock(lock)); -#else - old_value = *mem; - (*mem)++; -#endif /* APR_HAS_THREADS */ - return old_value; + return apr_atomic_add32(mem, 1); } -#endif /*!defined(apr_atomic_inc32) && !defined(APR_OVERRIDE_ATOMIC_INC32) */ +#endif /* !defined(APR_OVERRIDE_ATOMIC_INC32) */ -#if !defined(apr_atomic_dec32) && !defined(APR_OVERRIDE_ATOMIC_DEC32) +#if !defined(APR_OVERRIDE_ATOMIC_DEC32) +#if defined(APR_OVERRIDE_ATOMIC_CAS32) +int apr_atomic_dec32(volatile apr_uint32_t *mem) +{ + apr_uint32_t old_value, new_value; + + do { + old_value = *mem; + new_value = old_value - 1; + } while (apr_atomic_cas32(mem, new_value, old_value) != old_value); + return old_value != 1; +} +#else int apr_atomic_dec32(volatile apr_uint32_t *mem) { #if APR_HAS_THREADS @@ -318,9 +342,10 @@ return *mem; #endif /* APR_HAS_THREADS */ } -#endif /*!defined(apr_atomic_dec32) && !defined(APR_OVERRIDE_ATOMIC_DEC32) */ +#endif /* defined(APR_OVERRIDE_ATOMIC_CAS32) */ +#endif /* !defined(APR_OVERRIDE_ATOMIC_DEC32) */ -#if !defined(apr_atomic_cas32) && !defined(APR_OVERRIDE_ATOMIC_CAS32) +#if !defined(APR_OVERRIDE_ATOMIC_CAS32) apr_uint32_t apr_atomic_cas32(volatile apr_uint32_t *mem, apr_uint32_t with, apr_uint32_t cmp) { @@ -342,9 +367,19 @@ #endif /* APR_HAS_THREADS */ return prev; } -#endif /*!defined(apr_atomic_cas32) && !defined(APR_OVERRIDE_ATOMIC_CAS32) */ +#endif /* !defined(APR_OVERRIDE_ATOMIC_CAS32) */ -#if !defined(apr_atomic_xchg32) && !defined(APR_OVERRIDE_ATOMIC_XCHG32) +#if !defined(APR_OVERRIDE_ATOMIC_XCHG32) +#if defined(APR_OVERRIDE_ATOMIC_CAS32) +apr_uint32_t apr_atomic_xchg32(volatile apr_uint32_t *mem, apr_uint32_t val) +{ + apr_uint32_t prev; + do { + prev = *mem; + } while (apr_atomic_cas32(mem, val, prev) != prev); + return prev; +} +#else apr_uint32_t apr_atomic_xchg32(volatile apr_uint32_t *mem, apr_uint32_t val) { apr_uint32_t prev; @@ -361,9 +396,10 @@ #endif /* APR_HAS_THREADS */ return prev; } -#endif /*!defined(apr_atomic_xchg32) && !defined(APR_OVERRIDE_ATOMIC_XCHG32) */ +#endif /* defined(APR_OVERRIDE_ATOMIC_CAS32) */ +#endif /* !defined(APR_OVERRIDE_ATOMIC_XCHG32) */ -#if !defined(apr_atomic_casptr) && !defined(APR_OVERRIDE_ATOMIC_CASPTR) +#if !defined(APR_OVERRIDE_ATOMIC_CASPTR) void *apr_atomic_casptr(volatile void **mem, void *with, const void *cmp) { void *prev; @@ -384,7 +420,7 @@ #endif /* APR_HAS_THREADS */ return prev; } -#endif /*!defined(apr_atomic_casptr) && !defined(APR_OVERRIDE_ATOMIC_CASPTR) */ +#endif /* !defined(APR_OVERRIDE_ATOMIC_CASPTR) */ #if !defined(APR_OVERRIDE_ATOMIC_READ32) APR_DECLARE(apr_uint32_t) apr_atomic_read32(volatile apr_uint32_t *mem)