--- /dev/null
+/*\r
+ * FreeRTOS Kernel V10.2.1\r
+ * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.\r
+ *\r
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
+ * this software and associated documentation files (the "Software"), to deal in\r
+ * the Software without restriction, including without limitation the rights to\r
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
+ * the Software, and to permit persons to whom the Software is furnished to do so,\r
+ * subject to the following conditions:\r
+ *\r
+ * The above copyright notice and this permission notice shall be included in all\r
+ * copies or substantial portions of the Software.\r
+ *\r
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
+ * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
+ * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
+ * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
+ *\r
+ * http://www.FreeRTOS.org\r
+ * http://aws.amazon.com/freertos\r
+ *\r
+ * 1 tab == 4 spaces!\r
+ */\r
+\r
+/**\r
+ * @file atomic.h\r
+ * @brief FreeRTOS atomic operation support.\r
+ *\r
+ * This file implements atomic by disabling interrupts globally. \r
+ * Implementation with architecture specific atomic instructions \r
+ * are to be provided under each compiler directory. \r
+ */\r
+\r
+#ifndef ATOMIC_H\r
+#define ATOMIC_H\r
+\r
+#ifndef INC_FREERTOS_H\r
+ #error "include FreeRTOS.h must appear in source files before include atomic.h"\r
+#endif\r
+\r
+/* Standard includes. */\r
+#include <stdint.h>\r
+\r
+#ifdef __cplusplus\r
+extern "C" {\r
+#endif\r
+\r
+/* Port specific definitions -- entering/exiting critical section.\r
+ * Refer template -- ./lib/FreeRTOS/portable/Compiler/Arch/portmacro.h\r
+ *\r
+ * Every call to ATOMIC_EXIT_CRITICAL() must be closely paired with\r
+ * ATOMIC_ENTER_CRITICAL().\r
+ * */\r
+#if defined( portSET_INTERRUPT_MASK_FROM_ISR )\r
+\r
+ /* Nested interrupt scheme is supported in this port. */\r
+ #define ATOMIC_ENTER_CRITICAL() \\r
+ UBaseType_t uxCriticalSectionType = portSET_INTERRUPT_MASK_FROM_ISR()\r
+\r
+ #define ATOMIC_EXIT_CRITICAL() \\r
+ portCLEAR_INTERRUPT_MASK_FROM_ISR( uxCriticalSectionType )\r
+\r
+#else\r
+\r
+ /* Nested interrupt scheme is NOT supported in this port. */\r
+ #define ATOMIC_ENTER_CRITICAL() portENTER_CRITICAL()\r
+ #define ATOMIC_EXIT_CRITICAL() portEXIT_CRITICAL()\r
+\r
+#endif /* portSET_INTERRUPT_MASK_FROM_ISR() */\r
+\r
+/* Port specific definition -- "always inline". \r
+ * Inline is compiler specific, and may not always get inlined depending on your optimization level. \r
+ * Also, inline is considerred as performance optimization for atomic. \r
+ * Thus, if portFORCE_INLINE is not provided by portmacro.h, instead of resulting error,\r
+ * simply define it. \r
+ */\r
+#ifndef portFORCE_INLINE\r
+ #define portFORCE_INLINE \r
+#endif\r
+\r
+#define ATOMIC_COMPARE_AND_SWAP_SUCCESS 0x1U /**< Compare and swap succeeded, swapped. */\r
+#define ATOMIC_COMPARE_AND_SWAP_FAILURE 0x0U /**< Compare and swap failed, did not swap. */\r
+\r
+/*----------------------------- Swap && CAS ------------------------------*/\r
+\r
+/**\r
+ * Atomic compare-and-swap\r
+ *\r
+ * @brief Performs an atomic compare-and-swap operation on the specified values.\r
+ *\r
+ * @param[in, out] pDestination Pointer to memory location from where value is\r
+ * to be loaded and checked.\r
+ * @param[in] ulExchange If condition meets, write this value to memory.\r
+ * @param[in] ulComparand Swap condition.\r
+ *\r
+ * @return Unsigned integer of value 1 or 0. 1 for swapped, 0 for not swapped.\r
+ *\r
+ * @note This function only swaps *pDestination with ulExchange, if previous\r
+ * *pDestination value equals ulComparand.\r
+ */\r
+static portFORCE_INLINE uint32_t Atomic_CompareAndSwap_u32(\r
+ uint32_t volatile * pDestination,\r
+ uint32_t ulExchange,\r
+ uint32_t ulComparand )\r
+{\r
+\r
+ uint32_t ulReturnValue = ATOMIC_COMPARE_AND_SWAP_FAILURE;\r
+\r
+ ATOMIC_ENTER_CRITICAL();\r
+\r
+ if ( *pDestination == ulComparand )\r
+ {\r
+ *pDestination = ulExchange;\r
+ ulReturnValue = ATOMIC_COMPARE_AND_SWAP_SUCCESS;\r
+ }\r
+\r
+ ATOMIC_EXIT_CRITICAL();\r
+\r
+ return ulReturnValue;\r
+\r
+}\r
+\r
+/**\r
+ * Atomic swap (pointers)\r
+ *\r
+ * @brief Atomically sets the address pointed to by *ppDestination to the value\r
+ * of *pExchange.\r
+ *\r
+ * @param[in, out] ppDestination Pointer to memory location from where a pointer\r
+ * value is to be loaded and written back to.\r
+ * @param[in] pExchange Pointer value to be written to *ppDestination.\r
+ *\r
+ * @return The initial value of *ppDestination.\r
+ */\r
+static portFORCE_INLINE void * Atomic_SwapPointers_p32(\r
+ void * volatile * ppDestination,\r
+ void * pExchange )\r
+{\r
+ void * pReturnValue;\r
+\r
+ ATOMIC_ENTER_CRITICAL();\r
+\r
+ pReturnValue = *ppDestination;\r
+\r
+ *ppDestination = pExchange;\r
+\r
+ ATOMIC_EXIT_CRITICAL();\r
+\r
+ return pReturnValue;\r
+}\r
+\r
+/**\r
+ * Atomic compare-and-swap (pointers)\r
+ *\r
+ * @brief Performs an atomic compare-and-swap operation on the specified pointer\r
+ * values.\r
+ *\r
+ * @param[in, out] ppDestination Pointer to memory location from where a pointer\r
+ * value is to be loaded and checked.\r
+ * @param[in] pExchange If condition meets, write this value to memory.\r
+ * @param[in] pComparand Swap condition.\r
+ *\r
+ * @return Unsigned integer of value 1 or 0. 1 for swapped, 0 for not swapped.\r
+ *\r
+ * @note This function only swaps *ppDestination with pExchange, if previous\r
+ * *ppDestination value equals pComparand.\r
+ */\r
+static portFORCE_INLINE uint32_t Atomic_CompareAndSwapPointers_p32(\r
+ void * volatile * ppDestination,\r
+ void * pExchange, void * pComparand )\r
+{\r
+ uint32_t ulReturnValue = ATOMIC_COMPARE_AND_SWAP_FAILURE;\r
+\r
+ ATOMIC_ENTER_CRITICAL();\r
+\r
+ if ( *ppDestination == pComparand )\r
+ {\r
+ *ppDestination = pExchange;\r
+ ulReturnValue = ATOMIC_COMPARE_AND_SWAP_SUCCESS;\r
+ }\r
+\r
+ ATOMIC_EXIT_CRITICAL();\r
+\r
+ return ulReturnValue;\r
+}\r
+\r
+\r
+/*----------------------------- Arithmetic ------------------------------*/\r
+\r
+/**\r
+ * Atomic add\r
+ *\r
+ * @brief Atomically adds count to the value of the specified pointer points to.\r
+ *\r
+ * @param[in,out] pAddend Pointer to memory location from where value is to be\r
+ * loaded and written back to.\r
+ * @param[in] ulCount Value to be added to *pAddend.\r
+ *\r
+ * @return previous *pAddend value.\r
+ */\r
+static portFORCE_INLINE uint32_t Atomic_Add_u32(\r
+ uint32_t volatile * pAddend,\r
+ uint32_t ulCount )\r
+{\r
+ uint32_t ulCurrent;\r
+\r
+ ATOMIC_ENTER_CRITICAL();\r
+\r
+ ulCurrent = *pAddend;\r
+\r
+ *pAddend += ulCount;\r
+\r
+ ATOMIC_EXIT_CRITICAL();\r
+\r
+ return ulCurrent;\r
+}\r
+\r
+/**\r
+ * Atomic subtract\r
+ *\r
+ * @brief Atomically subtracts count from the value of the specified pointer\r
+ * pointers to.\r
+ *\r
+ * @param[in,out] pAddend Pointer to memory location from where value is to be\r
+ * loaded and written back to.\r
+ * @param[in] ulCount Value to be subtract from *pAddend.\r
+ *\r
+ * @return previous *pAddend value.\r
+ */\r
+static portFORCE_INLINE uint32_t Atomic_Subtract_u32(\r
+ uint32_t volatile * pAddend,\r
+ uint32_t ulCount )\r
+{\r
+ uint32_t ulCurrent;\r
+\r
+ ATOMIC_ENTER_CRITICAL();\r
+\r
+ ulCurrent = *pAddend;\r
+\r
+ *pAddend -= ulCount;\r
+\r
+ ATOMIC_EXIT_CRITICAL();\r
+\r
+ return ulCurrent;\r
+}\r
+\r
+/**\r
+ * Atomic increment\r
+ *\r
+ * @brief Atomically increments the value of the specified pointer points to.\r
+ *\r
+ * @param[in,out] pAddend Pointer to memory location from where value is to be\r
+ * loaded and written back to.\r
+ *\r
+ * @return *pAddend value before increment.\r
+ */\r
+static portFORCE_INLINE uint32_t Atomic_Increment_u32( uint32_t volatile * pAddend )\r
+{\r
+ uint32_t ulCurrent;\r
+\r
+ ATOMIC_ENTER_CRITICAL();\r
+\r
+ ulCurrent = *pAddend;\r
+\r
+ *pAddend += 1;\r
+\r
+ ATOMIC_EXIT_CRITICAL();\r
+\r
+ return ulCurrent;\r
+}\r
+\r
+/**\r
+ * Atomic decrement\r
+ *\r
+ * @brief Atomically decrements the value of the specified pointer points to\r
+ *\r
+ * @param[in,out] pAddend Pointer to memory location from where value is to be\r
+ * loaded and written back to.\r
+ *\r
+ * @return *pAddend value before decrement.\r
+ */\r
+static portFORCE_INLINE uint32_t Atomic_Decrement_u32( uint32_t volatile * pAddend )\r
+{\r
+ uint32_t ulCurrent;\r
+\r
+ ATOMIC_ENTER_CRITICAL();\r
+\r
+ ulCurrent = *pAddend;\r
+\r
+ *pAddend -= 1;\r
+\r
+ ATOMIC_EXIT_CRITICAL();\r
+\r
+ return ulCurrent;\r
+}\r
+\r
+/*----------------------------- Bitwise Logical ------------------------------*/\r
+\r
+/**\r
+ * Atomic OR\r
+ *\r
+ * @brief Performs an atomic OR operation on the specified values.\r
+ *\r
+ * @param [in, out] pDestination Pointer to memory location from where value is\r
+ * to be loaded and written back to.\r
+ * @param [in] ulValue Value to be ORed with *pDestination.\r
+ *\r
+ * @return The original value of *pDestination.\r
+ */\r
+static portFORCE_INLINE uint32_t Atomic_OR_u32(\r
+ uint32_t volatile * pDestination,\r
+ uint32_t ulValue )\r
+{\r
+ uint32_t ulCurrent;\r
+\r
+ ATOMIC_ENTER_CRITICAL();\r
+\r
+ ulCurrent = *pDestination;\r
+\r
+ *pDestination |= ulValue;\r
+\r
+ ATOMIC_EXIT_CRITICAL();\r
+\r
+ return ulCurrent;\r
+}\r
+\r
+/**\r
+ * Atomic AND\r
+ *\r
+ * @brief Performs an atomic AND operation on the specified values.\r
+ *\r
+ * @param [in, out] pDestination Pointer to memory location from where value is\r
+ * to be loaded and written back to.\r
+ * @param [in] ulValue Value to be ANDed with *pDestination.\r
+ *\r
+ * @return The original value of *pDestination.\r
+ */\r
+static portFORCE_INLINE uint32_t Atomic_AND_u32(\r
+ uint32_t volatile * pDestination,\r
+ uint32_t ulValue )\r
+{\r
+ uint32_t ulCurrent;\r
+\r
+ ATOMIC_ENTER_CRITICAL();\r
+\r
+ ulCurrent = *pDestination;\r
+\r
+ *pDestination &= ulValue;\r
+\r
+ ATOMIC_EXIT_CRITICAL();\r
+\r
+ return ulCurrent;\r
+}\r
+\r
+/**\r
+ * Atomic NAND\r
+ *\r
+ * @brief Performs an atomic NAND operation on the specified values.\r
+ *\r
+ * @param [in, out] pDestination Pointer to memory location from where value is\r
+ * to be loaded and written back to.\r
+ * @param [in] ulValue Value to be NANDed with *pDestination.\r
+ *\r
+ * @return The original value of *pDestination.\r
+ */\r
+static portFORCE_INLINE uint32_t Atomic_NAND_u32(\r
+ uint32_t volatile * pDestination,\r
+ uint32_t ulValue )\r
+{\r
+ uint32_t ulCurrent;\r
+\r
+ ATOMIC_ENTER_CRITICAL();\r
+\r
+ ulCurrent = *pDestination;\r
+\r
+ *pDestination = ~(ulCurrent & ulValue);\r
+\r
+ ATOMIC_EXIT_CRITICAL();\r
+\r
+ return ulCurrent;\r
+}\r
+\r
+/**\r
+ * Atomic XOR\r
+ *\r
+ * @brief Performs an atomic XOR operation on the specified values.\r
+ *\r
+ * @param [in, out] pDestination Pointer to memory location from where value is\r
+ * to be loaded and written back to.\r
+ * @param [in] ulValue Value to be XORed with *pDestination.\r
+ *\r
+ * @return The original value of *pDestination.\r
+ */\r
+static portFORCE_INLINE uint32_t Atomic_XOR_u32(\r
+ uint32_t volatile * pDestination,\r
+ uint32_t ulValue )\r
+{\r
+ uint32_t ulCurrent;\r
+\r
+ ATOMIC_ENTER_CRITICAL();\r
+\r
+ ulCurrent = *pDestination;\r
+\r
+ *pDestination ^= ulValue;\r
+\r
+ ATOMIC_EXIT_CRITICAL();\r
+\r
+ return ulCurrent;\r
+}\r
+\r
+#ifdef __cplusplus\r
+}\r
+#endif\r
+\r
+#endif /* ATOMIC_H */\r