2014-06-10 20:15:42 +02:00
|
|
|
/*
|
2015-05-18 18:47:17 +02:00
|
|
|
* Copyright (C) 2014-2015 Freie Universität Berlin
|
2014-06-10 20:15:42 +02:00
|
|
|
*
|
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser General
|
|
|
|
* Public License v2.1. See the file LICENSE in the top level directory for more
|
|
|
|
* details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2015-05-18 18:47:17 +02:00
|
|
|
* @ingroup cpu_cortexm_common
|
2014-06-10 20:15:42 +02:00
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @brief Implementation of the kernels atomic interface
|
|
|
|
*
|
|
|
|
* @author Stefan Pfeiffer <stefan.pfeiffer@fu-berlin.de>
|
|
|
|
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
2015-04-28 23:08:47 +02:00
|
|
|
* @author Joakim Gebart <joakim.gebart@eistec.se>
|
2014-06-10 20:15:42 +02:00
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
2015-04-28 23:08:47 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
#include "atomic.h"
|
2014-06-10 20:15:42 +02:00
|
|
|
#include "irq.h"
|
2015-04-28 23:08:47 +02:00
|
|
|
#include "cpu.h"
|
2014-06-10 20:15:42 +02:00
|
|
|
|
2015-05-18 18:47:17 +02:00
|
|
|
#if ARCH_HAS_ATOMIC_COMPARE_AND_SWAP
|
2015-04-28 23:08:47 +02:00
|
|
|
int atomic_cas(atomic_int_t *var, int old, int now)
|
2014-06-10 20:15:42 +02:00
|
|
|
{
|
2015-04-28 23:08:47 +02:00
|
|
|
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);
|
2014-06-10 20:15:42 +02:00
|
|
|
}
|
2015-05-18 18:47:17 +02:00
|
|
|
#endif
|