diff --git a/cpu/cortex-m3_common/atomic_arch.c b/cpu/cortex-m3_common/atomic_arch.c index 9b51df0e58..1ee7f4fc50 100644 --- a/cpu/cortex-m3_common/atomic_arch.c +++ b/cpu/cortex-m3_common/atomic_arch.c @@ -10,23 +10,37 @@ * @ingroup cpu_cortexm3_common * @{ * - * @file atomic_arch.c + * @file * @brief Implementation of the kernels atomic interface * * @author Stefan Pfeiffer * @author Hauke Petersen + * @author Joakim Gebart * * @} */ -#include "arch/atomic_arch.h" +#include +#include "atomic.h" #include "irq.h" +#include "cpu.h" -unsigned int atomic_arch_set_return(unsigned int *to_set, unsigned int value) +int atomic_cas(atomic_int_t *var, int old, int now) { - disableIRQ(); - unsigned int old = *to_set; - *to_set = value; - enableIRQ(); - return old; + int tmp; + int status; + + /* Load exclusive */ + tmp = __LDREXW((volatile uint32_t *)(&ATOMIC_VALUE(*var))); + + if (tmp != old) { + /* Clear memory exclusivity */ + __CLREX(); + return 0; + } + + /* Try to write the new value */ + status = __STREXW(now, (volatile uint32_t *)(&ATOMIC_VALUE(*var))); + + return (status == 0); } diff --git a/cpu/cortex-m3_common/include/cpu.h b/cpu/cortex-m3_common/include/cpu.h index 6ace208af4..b72a1d768c 100644 --- a/cpu/cortex-m3_common/include/cpu.h +++ b/cpu/cortex-m3_common/include/cpu.h @@ -23,13 +23,13 @@ * @author Joakim Gebart */ -#ifndef __CORTEXM_COMMON_H -#define __CORTEXM_COMMON_H +#ifndef CORTEXM_COMMON_H_ +#define CORTEXM_COMMON_H_ /** * @brief Cortex-M3 has architecture specific atomic operations in atomic_arch.c. */ -#define ARCH_HAS_ATOMIC_INT 1 +#define ARCH_HAS_ATOMIC_COMPARE_AND_SWAP 1 #include "cpu-conf.h" @@ -55,5 +55,5 @@ void cpu_init(void); } #endif -#endif /* __CORTEXM_COMMON_H */ +#endif /* CORTEXM_COMMON_H_ */ /** @} */