spinlock.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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(__SPINLOCK_H__)
  31. #define __SPINLOCK_H__
  32. #include <stdint.h>
  33. #include <stdbool.h>
  34. //! @addtogroup spinlock
  35. //! @{
  36. ////////////////////////////////////////////////////////////////////////////////
  37. // Definitions
  38. ////////////////////////////////////////////////////////////////////////////////
  39. //! @brief Typedef for a spinlock.
  40. typedef struct _spinlock {
  41. uint32_t owner; //!< Lock count.
  42. // uint32_t owner; //!< Core number of the owning CPU.
  43. uint32_t _cacheLineFiller[7]; //! Padding to make the lock consume a full cache line (32 bytes).
  44. } spinlock_t __attribute__ ((aligned (32)));
  45. //! @brief Special timeout values for the spinlock.
  46. enum _spinlock_timeouts
  47. {
  48. //! Timeout value meaning do not block at all if the spinlock isn't free.
  49. kSpinlockNoWait = 0,
  50. //! Timeout value to use to wait indefinitely for the spinlock to become available.
  51. kSpinlockWaitForever = 0xffffffff
  52. };
  53. ////////////////////////////////////////////////////////////////////////////////
  54. // Code
  55. ////////////////////////////////////////////////////////////////////////////////
  56. #if defined(__cplusplus)
  57. extern "C" {
  58. #endif
  59. //! @name Spinlock API
  60. //@{
  61. //! @brief Initialize a new spinlock.
  62. //!
  63. //! The spinlock is initialized in the unlocked state. Call spinlock_lock() if you need it
  64. //! to be locked.
  65. //!
  66. //! @param lock Pointer to the new spinlock.
  67. void spinlock_init(spinlock_t * lock);
  68. //! @brief Lock the given spinlock.
  69. //!
  70. //! Use this function to lock a spinlock. If the spinlock is unlocked, it will be locked and
  71. //! this function will return immediately. However, if the spinlock is already locked then
  72. //! this function will block for up to @a timeout microseconds waiting for another thread to
  73. //! unlock it.
  74. //!
  75. //! If #kSpinlockNoWait is passed for the timeout, then the function will not block at all.
  76. //! In this case, either the spinlock is locked immediately or it is left untouched.
  77. //!
  78. //! To wait infinitely for the spinlock to become free, pass #kSpinlockWaitForever. If the
  79. //! spinlock is already locked and no other thread unlocks it, then this function will never
  80. //! return (i.e., deadlock).
  81. //!
  82. //! @param lock Pointer to the spinlock to lock.
  83. //! @param timeout Maximum number of microseconds to block waiting for the spinlock to become
  84. //! available if it was locked upon entry to this function.
  85. //!
  86. //! @retval 0 The spinlock was locked successfully.
  87. //! @retval 1 A timeout occurred while waiting for the spinlock to be unlocked.
  88. int spinlock_lock(spinlock_t * lock, uint32_t timeout);
  89. //! @brief Unlock the given spinlock.
  90. //!
  91. //! @param lock Pointer to the spinlock to unlock.
  92. void spinlock_unlock(spinlock_t * lock);
  93. //! @brief Check whether a spinlock is currently locked.
  94. //!
  95. //! @param lock Pointer to the spinlock to test.
  96. bool spinlock_is_locked(spinlock_t * lock);
  97. //@}
  98. #if defined(__cplusplus)
  99. }
  100. #endif
  101. //! @}
  102. #endif // __SPINLOCK_H__
  103. ////////////////////////////////////////////////////////////////////////////////
  104. // EOF
  105. ////////////////////////////////////////////////////////////////////////////////