From 3b56c119a7197343df7abe03be598ad75e5cc800 Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Tue, 22 Mar 2011 12:17:00 -0500 Subject: [PATCH] FS-3173 --- src/include/switch_apr.h | 60 ++++++++++++++++++++++++++++++++++++++++ src/switch_apr.c | 53 ++++++++++++++++++++++++++++++++++- 2 files changed, 112 insertions(+), 1 deletion(-) diff --git a/src/include/switch_apr.h b/src/include/switch_apr.h index a196af76f5..62559faf63 100644 --- a/src/include/switch_apr.h +++ b/src/include/switch_apr.h @@ -24,6 +24,7 @@ * Contributor(s): * * Anthony Minessale II + * Eliot Gable * * switch_apr.h -- APR includes header * @@ -410,6 +411,65 @@ SWITCH_DECLARE(switch_status_t) switch_mutex_trylock(switch_mutex_t *lock); /** @} */ +/** + * @defgroup switch_atomic Multi-Threaded Adtomic Operations Routines + * @ingroup switch_apr + * @{ + */ + +/** Opaque type used for the atomic operations */ +#ifdef apr_atomic_t + typedef apr_atomic_t switch_atomic_t; +#else + typedef uint32_t switch_atomic_t; +#endif + +/** + * Some architectures require atomic operations internal structures to be + * initialized before use. + * @param pool The memory pool to use when initializing the structures. + */ +SWITCH_DECLARE(switch_status_t) switch_atomic_init(switch_memory_pool_t *pool); + +/** + * Uses an atomic operation to read the uint32 value at the location specified + * by mem. + * @param mem The location of memory which stores the value to read. + */ +SWITCH_DECLARE(uint32_t) switch_atomic_read(volatile switch_atomic_t *mem); + +/** + * Uses an atomic operation to set a uint32 value at a specified location of + * memory. + * @param mem The location of memory to set. + * @param val The uint32 value to set at the memory location. + */ +SWITCH_DECLARE(void) switch_atomic_set(volatile switch_atomic_t *mem, uint32_t val); + +/** + * Uses an atomic operation to add the uint32 value to the value at the + * specified location of memory. + * @param mem The location of the value to add to. + * @param val The uint32 value to add to the value at the memory location. + */ +SWITCH_DECLARE(void) switch_atomic_add(volatile switch_atomic_t *mem, uint32_t val); + +/** + * Uses an atomic operation to increment the value at the specified memroy + * location. + * @param mem The location of the value to increment. + */ +SWITCH_DECLARE(void) switch_atomic_inc(volatile switch_atomic_t *mem); + +/** + * Uses an atomic operation to decrement the value at the specified memroy + * location. + * @param mem The location of the value to decrement. + */ +SWITCH_DECLARE(int) switch_atomic_dec(volatile switch_atomic_t *mem); + +/** @} */ + /** * @defgroup switch_thread_rwlock Thread Read/Write lock Routines * @ingroup switch_apr diff --git a/src/switch_apr.c b/src/switch_apr.c index 3798d6e2dc..3ca3c63f77 100644 --- a/src/switch_apr.c +++ b/src/switch_apr.c @@ -24,7 +24,7 @@ * Contributor(s): * * Michael Jerris - * + * Eliot Gable * * switch_apr.c -- apr wrappers and extensions * @@ -38,6 +38,7 @@ /* apr headers*/ #include +#include #include #include #include @@ -1141,6 +1142,56 @@ SWITCH_DECLARE(switch_status_t) switch_thread_join(switch_status_t *retval, swit } +SWITCH_DECLARE(switch_status_t) switch_atomic_init(switch_memory_pool_t *pool) +{ + return apr_atomic_init((apr_pool_t *) pool); +} + +SWITCH_DECLARE(uint32_t) switch_atomic_read(volatile switch_atomic_t *mem) +{ +#ifdef apr_atomic_t + return apr_atomic_read((apr_atomic_t *)mem); +#else + return apr_atomic_read32((apr_uint32_t *)mem); +#endif +} + +SWITCH_DECLARE(void) switch_atomic_set(volatile switch_atomic_t *mem, uint32_t val) +{ +#ifdef apr_atomic_t + apr_atomic_set((apr_atomic_t *)mem, val); +#else + apr_atomic_set32((apr_uint32_t *)mem, val); +#endif +} + +SWITCH_DECLARE(void) switch_atomic_add(volatile switch_atomic_t *mem, uint32_t val) +{ +#ifdef apr_atomic_t + apr_atomic_add((apr_atomic_t *)mem, val); +#else + apr_atomic_add32((apr_uint32_t *)mem, val); +#endif +} + +SWITCH_DECLARE(void) switch_atomic_inc(volatile switch_atomic_t *mem) +{ +#ifdef apr_atomic_t + apr_atomic_inc((apr_atomic_t *)mem); +#else + apr_atomic_inc32((apr_uint32_t *)mem); +#endif +} + +SWITCH_DECLARE(int) switch_atomic_dec(volatile switch_atomic_t *mem) +{ +#ifdef apr_atomic_t + return apr_atomic_dec((apr_atomic_t *)mem); +#else + return apr_atomic_dec32((apr_uint32_t *)mem); +#endif +} + /* For Emacs: * Local Variables: