Merge pull request #221 from g-oikonomou/contrib/mutex

Memory barriers, critical sections and mutexes
This commit is contained in:
George Oikonomou 2017-12-10 13:41:20 +00:00 committed by GitHub
commit 34f598002f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 500 additions and 0 deletions

View File

@ -0,0 +1,44 @@
/*
* Copyright (c) 2017, George Oikonomou - http://www.spd.gr
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*---------------------------------------------------------------------------*/
#ifndef MEMORY_BARRIER_CORTEX_H_
#define MEMORY_BARRIER_CORTEX_H_
/*---------------------------------------------------------------------------*/
#include "contiki.h"
#ifdef CMSIS_CONF_HEADER_PATH
#include CMSIS_CONF_HEADER_PATH
#endif
/*---------------------------------------------------------------------------*/
#define memory_barrier() __DMB()
/*---------------------------------------------------------------------------*/
#endif /* MEMORY_BARRIER_CORTEX_H_ */
/*---------------------------------------------------------------------------*/

View File

@ -0,0 +1,82 @@
/*
* Copyright (c) 2017, George Oikonomou - http://www.spd.gr
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*---------------------------------------------------------------------------*/
/**
* \addtogroup arm
*
* Arm Cortex-M implementation of mutexes using the LDREX, STREX and DMB
* instructions.
*
* @{
*/
/*---------------------------------------------------------------------------*/
#ifndef MUTEX_CORTEX_H_
#define MUTEX_CORTEX_H_
/*---------------------------------------------------------------------------*/
#include "contiki.h"
#ifdef CMSIS_CONF_HEADER_PATH
#include CMSIS_CONF_HEADER_PATH
#endif
#include <stdint.h>
#include <stdbool.h>
/*---------------------------------------------------------------------------*/
#define mutex_try_lock(m) mutex_cortex_try_lock(m)
#define mutex_unlock(m) mutex_cortex_unlock(m)
/*---------------------------------------------------------------------------*/
#define MUTEX_CONF_HAS_MUTEX_T 1
typedef uint8_t mutex_t;
/*---------------------------------------------------------------------------*/
static inline bool
mutex_cortex_try_lock(volatile mutex_t *mutex)
{
int status = 1;
if(__LDREXB(mutex) == 0) {
status = __STREXB(1, mutex);
}
__DMB();
return status == 0 ? true : false;
}
/*---------------------------------------------------------------------------*/
static inline void
mutex_cortex_unlock(volatile mutex_t *mutex)
{
__DMB();
*mutex = 0;
}
/*---------------------------------------------------------------------------*/
#endif /* MUTEX_CORTEX_H_ */
/*---------------------------------------------------------------------------*/
/** @} */

View File

@ -58,5 +58,12 @@
#define TSCH_CONF_HW_FRAME_FILTERING 0
#endif /* MAC_CONF_WITH_TSCH */
/*---------------------------------------------------------------------------*/
/* Path to CMSIS header */
#define CMSIS_CONF_HEADER_PATH "cc2538_cm3.h"
/* Path to headers with implementation of mutexes and memory barriers */
#define MUTEX_CONF_ARCH_HEADER_PATH "mutex-cortex.h"
#define MEMORY_BARRIER_CONF_ARCH_HEADER_PATH "memory-barrier-cortex.h"
/*---------------------------------------------------------------------------*/
#endif /* CC2538_DEF_H_ */
/*---------------------------------------------------------------------------*/

View File

@ -93,5 +93,12 @@
/*---------------------------------------------------------------------------*/
#define RTIMER_ARCH_SECOND 65536
/*---------------------------------------------------------------------------*/
/* Path to CMSIS header */
#define CMSIS_CONF_HEADER_PATH "cc13x0-cc26x0-cm3.h"
/* Path to headers with implementation of mutexes and memory barriers */
#define MUTEX_CONF_ARCH_HEADER_PATH "mutex-cortex.h"
#define MEMORY_BARRIER_CONF_ARCH_HEADER_PATH "memory-barrier-cortex.h"
/*---------------------------------------------------------------------------*/
#endif /* CC13XX_CC26XX_DEF_H_ */
/*---------------------------------------------------------------------------*/

View File

@ -106,6 +106,8 @@ void *w_memset(void *out, int value, size_t n);
#endif /* memcpy */
#endif /* __GNUC__ && __MSP430__ && MSP430_MEMCPY_WORKAROUND */
#define memory_barrier() asm volatile("" : : : "memory")
#define MSP430_REQUIRE_CPUON 0
#define MSP430_REQUIRE_LPM1 1
#define MSP430_REQUIRE_LPM2 2

92
os/sys/critical.h Normal file
View File

@ -0,0 +1,92 @@
/*
* Copyright (c) 2017, George Oikonomou - http://www.spd.gr
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*---------------------------------------------------------------------------*/
/**
* \addtogroup sys
* @{
*
* \defgroup critical Critical sections
* @{
*
* Platform-independent functions for critical section entry and exit
*/
/*---------------------------------------------------------------------------*/
#ifndef CRITICAL_H_
#define CRITICAL_H_
/*---------------------------------------------------------------------------*/
#include "contiki.h"
#include "sys/memory-barrier.h"
#include "sys/int-master.h"
#include <stdint.h>
/*---------------------------------------------------------------------------*/
/**
* \brief Enter a critical section
* \return The status of the master interrupt before entering the critical
*
* This function will return the status of the master interrupt as it was
* before entering the critical section.
*
* The semantics of the return value are entirely platform-specific. The
* calling code should not try to determine whether the master interrupt was
* previously enabled/disabled by interpreting the return value of this
* function. The return value should only be used as an argument to
* critical_exit().
*/
static inline int_master_status_t
critical_enter()
{
int_master_status_t status = int_master_read_and_disable();
memory_barrier();
return status;
}
/*---------------------------------------------------------------------------*/
/**
* \brief Exit a critical section and restore the master interrupt
* \param status The new status of the master interrupt
*
* The semantics of \e status are platform-dependent. Normally, the argument
* provided to this function will be a value previously retrieved through a
* call to critical_enter().
*/
static inline void
critical_exit(int_master_status_t status)
{
memory_barrier();
int_master_status_set(status);
}
/*---------------------------------------------------------------------------*/
#endif /* CRITICAL_H_ */
/*---------------------------------------------------------------------------*/
/**
* @}
* @}
*/

67
os/sys/memory-barrier.h Normal file
View File

@ -0,0 +1,67 @@
/*
* Copyright (c) 2017, George Oikonomou - http://www.spd.gr
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*---------------------------------------------------------------------------*/
/**
* \addtogroup sys
* @{
*
* \defgroup memory-barrier CPU/Compiler memory barriers
* @{
*
* API for CPU/Compiler memory barriers
*/
/*---------------------------------------------------------------------------*/
#ifndef MEMORY_BARRIER_H_
#define MEMORY_BARRIER_H_
/*---------------------------------------------------------------------------*/
#include "contiki.h"
/*---------------------------------------------------------------------------*/
#ifdef MEMORY_BARRIER_CONF_ARCH_HEADER_PATH
#include MEMORY_BARRIER_CONF_ARCH_HEADER_PATH
#endif /* MEMORY_BARRIER_CONF_ARCH_HEADER_PATH */
/*---------------------------------------------------------------------------*/
#ifndef memory_barrier
/**
* \brief Insert a memory barrier
*
* It is the platform/CPU developer's responsibility to expand this macro to
* a function that creates a memory barrier. Calling this macro will otherwise
* not generate any code.
*/
#define memory_barrier()
#endif
/*---------------------------------------------------------------------------*/
#endif /* MEMORY_BARRIER_H_ */
/*---------------------------------------------------------------------------*/
/**
* @}
* @}
*/

70
os/sys/mutex.c Normal file
View File

@ -0,0 +1,70 @@
/*
* Copyright (c) 2017, George Oikonomou - http://www.spd.gr
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*---------------------------------------------------------------------------*/
/**
* \addtogroup mutex
* @{
*/
/*---------------------------------------------------------------------------*/
#include "contiki.h"
#include "sys/mutex.h"
#include "sys/critical.h"
#include <stdint.h>
#include <stdbool.h>
/*---------------------------------------------------------------------------*/
bool
mutex_generic_try_lock(volatile mutex_t *mutex)
{
bool success = false;
int_master_status_t status = critical_enter();
if(*mutex == MUTEX_STATUS_UNLOCKED) {
*mutex = MUTEX_STATUS_LOCKED;
success = true;
}
critical_exit(status);
return success;
}
/*---------------------------------------------------------------------------*/
void
mutex_generic_unlock(volatile mutex_t *mutex)
{
int_master_status_t status = critical_enter();
*mutex = MUTEX_STATUS_UNLOCKED;
critical_exit(status);
}
/*---------------------------------------------------------------------------*/
/** @} */

129
os/sys/mutex.h Normal file
View File

@ -0,0 +1,129 @@
/*
* Copyright (c) 2017, George Oikonomou - http://www.spd.gr
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*---------------------------------------------------------------------------*/
/**
* \addtogroup sys
* @{
*
* \defgroup mutex Mutexes
* @{
*
* This library provides an API and generic implementation of mutexes.
*
* Calling code should manipulate mutexes through the mutex_try_lock() and
* mutex_unlock() macros. By default, those macros will expand to the generic
* mutex manipulation implementations provided here. While these will work,
* they do reply on disabling the master interrupt in order to perform the
* lock/unlock operation.
*
* It is possible to override those generic implementation with CPU-specific
* implementations that exploit synchronisation instructions. To do so,
* create a CPU-specific header file. In this file, define mutex_try_lock()
* and mutex_unlock() to expand to the respective CPU function names. These
* can (but do not have to) be inlined. Then define MUTEX_CONF_ARCH_HEADER_PATH
* as this header's filename.
*/
/*---------------------------------------------------------------------------*/
#ifndef MUTEX_H_
#define MUTEX_H_
/*---------------------------------------------------------------------------*/
#include "contiki.h"
#include <stdint.h>
#include <stdbool.h>
/*---------------------------------------------------------------------------*/
#define MUTEX_STATUS_LOCKED 1 /** The mutex is locked */
#define MUTEX_STATUS_UNLOCKED 0 /** The mutex is not locked */
/*---------------------------------------------------------------------------*/
#ifdef MUTEX_CONF_ARCH_HEADER_PATH
#include MUTEX_CONF_ARCH_HEADER_PATH
#endif /* MUTEX_CONF_ARCH_HEADER_PATH */
/*---------------------------------------------------------------------------*/
#if !MUTEX_CONF_HAS_MUTEX_T
/**
* \brief Mutex data type
*
* It is possible for the platform to override this with its own typedef. In
* this scenario, make sure to also define MUTEX_CONF_HAS_MUTEX_T as 1.
*/
typedef uint_fast8_t mutex_t;
#endif
/*---------------------------------------------------------------------------*/
#ifndef mutex_try_lock
/**
* \brief Try to lock a mutex
* \param m A pointer to the mutex to be locked
* \retval true Locking succeeded
* \retval false Locking failed (the mutex is already locked)
*
* This macro will expand to mutex_generic_try_lock() or to a CPU-provided
* implementation. Platform-independent code should use this macro instead
* of mutex_generic_try_lock().
*/
#define mutex_try_lock(m) mutex_generic_try_lock(m)
#endif
#ifndef mutex_unlock
/**
* \brief Unlock a previously acquired mutex
* \param m A pointer to the mutex to be unlocked
*
* This macro will expand to mutex_generic_unlock() or to a CPU-provided
* implementation. Platform-independent code should use this macro instead
* of mutex_generic_unlock().
*/
#define mutex_unlock(m) mutex_generic_unlock(m)
#endif
/*---------------------------------------------------------------------------*/
/**
* \brief Try to lock a mutex
* \param mutex A pointer to the mutex to be locked
* \retval true Locking succeeded
* \retval false Locking failed (the mutex is already locked)
*
* Do not call this function directly. Use the mutex_try_lock() macro instead.
*/
bool mutex_generic_try_lock(volatile mutex_t *mutex);
/**
* \brief Unlock a previously acquired mutex
* \param mutex A pointer to the mutex to be unlocked
*
* Do not call this function directly. Use the mutex_unlock() macro instead.
*/
void mutex_generic_unlock(volatile mutex_t *mutex);
/*---------------------------------------------------------------------------*/
#endif /* MUTEX_H_ */
/*---------------------------------------------------------------------------*/
/**
* @}
* @}
*/