atomics.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2012, Freescale Semiconductor, Inc.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #if !defined(__ATOMICS_H__)
  31. #define __ATOMICS_H__
  32. #include <stdint.h>
  33. #include <stdbool.h>
  34. //! @addtogroup atomics
  35. //! @{
  36. ////////////////////////////////////////////////////////////////////////////////
  37. // Code
  38. ////////////////////////////////////////////////////////////////////////////////
  39. #if defined(__cplusplus)
  40. extern "C" {
  41. #endif
  42. //! @name Atomic operations
  43. //@{
  44. //! @brief Atomic compare and swap operation.
  45. //!
  46. //! Tests the word pointed to by @a value for equality with @a oldValue. If they are
  47. //! equal, the word pointed to by @a value is set to @a newValue. If *value is not
  48. //! equal to @a oldValue, then no change is made. The return value indicates whether
  49. //! the swap was performed. Of course, this entire operation is guaranteed to be
  50. //! atomic even on multiprocessor platforms.
  51. //!
  52. //! A memory barrier is performed prior to the compare and swap operation.
  53. //!
  54. //! @param value Pointer to the word to compare and swap.
  55. //! @param oldValue Value to compare against.
  56. //! @param newValue Value to value to swap in if *value is equal to oldValue.
  57. //! @retval false No change was made to *value.
  58. //! @retval true The swap was performed, and *value is now equal to newValue.
  59. bool atomic_compare_and_swap(volatile uint32_t * value, uint32_t oldValue, uint32_t newValue);
  60. //! @brief Atomic add operation.
  61. //!
  62. //! A memory barrier is performed prior to the add operation.
  63. //!
  64. //! @param value Pointer to the word to add to.
  65. //! @param delta Signed value to atomically add to *value.
  66. //! @return The original value is returned.
  67. int32_t atomic_add(volatile int32_t * value, int32_t delta);
  68. //! @brief Atomically increment a value.
  69. //!
  70. //! @param value Pointer to the word to increment.
  71. //! @return The original value is returned.
  72. int32_t atomic_increment(volatile int32_t * value);
  73. //! @brief Atomically decrement a value.
  74. //!
  75. //! @param value Pointer to the word to decrement.
  76. //! @return The original value is returned.
  77. int32_t atomic_decrement(volatile int32_t * value);
  78. //@}
  79. #if defined(__cplusplus)
  80. }
  81. #endif
  82. //! @}
  83. #endif // __ATOMICS_H__
  84. ////////////////////////////////////////////////////////////////////////////////
  85. // EOF
  86. ////////////////////////////////////////////////////////////////////////////////