nrf_mtx.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * Copyright (c) 2015 - 2019, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /** @file
  41. * @defgroup nrf_mtx nRF Mutex
  42. * @{
  43. * @ingroup app_common
  44. * @brief Mutex used for protecting resources.
  45. *
  46. * This module provides a mutex that can be used to ensure only one context may enter a critical
  47. * section holding the lock.
  48. */
  49. #ifndef NRF_MTX_H__
  50. #define NRF_MTX_H__
  51. #include <stdint.h>
  52. #include <stdbool.h>
  53. #include "nrf.h"
  54. #include "nrf_atomic.h"
  55. #include "nrf_assert.h"
  56. #define NRF_MTX_LOCKED 1
  57. #define NRF_MTX_UNLOCKED 0
  58. /**
  59. * @brief Mutex data type.
  60. *
  61. * All fields in this struct are internal, and should never be modified outside of the nrf_mtx_*
  62. * functions.
  63. */
  64. typedef nrf_atomic_u32_t nrf_mtx_t;
  65. /**
  66. * @brief Initialize mutex.
  67. *
  68. * This function _must_ be called before nrf_mtx_trylock() and nrf_mtx_unlock() functions.
  69. *
  70. * @param[in, out] p_mtx The mutex to be initialized.
  71. */
  72. __STATIC_INLINE void nrf_mtx_init(nrf_mtx_t * p_mtx);
  73. /**
  74. * @brief Destroy mutex.
  75. *
  76. * This function can be used in abort scenarios or when the mutex is no longer to be used.
  77. *
  78. * @param[in] p_mtx The mutex to be destroy.
  79. */
  80. __STATIC_INLINE void nrf_mtx_destroy(nrf_mtx_t * p_mtx);
  81. /**
  82. * @brief Try to lock a mutex.
  83. *
  84. * If the mutex is already held by another context, this function will return immediately.
  85. *
  86. * @param[in, out] p_mtx The mutex to be locked.
  87. * @return true if lock was acquired, false if not
  88. */
  89. __STATIC_INLINE bool nrf_mtx_trylock(nrf_mtx_t * p_mtx);
  90. /**
  91. * @brief Unlock a mutex.
  92. *
  93. * This function _must_ only be called when holding the lock. Unlocking a mutex which you do not
  94. * hold will give undefined behavior.
  95. *
  96. * @note Unlock must happen from the same context as the one used to lock the mutex.
  97. *
  98. * @param[in, out] p_mtx The mutex to be unlocked.
  99. */
  100. __STATIC_INLINE void nrf_mtx_unlock(nrf_mtx_t * p_mtx);
  101. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  102. __STATIC_INLINE void nrf_mtx_init(nrf_mtx_t * p_mtx)
  103. {
  104. ASSERT(p_mtx != NULL);
  105. *p_mtx = NRF_MTX_UNLOCKED;
  106. __DMB();
  107. }
  108. __STATIC_INLINE void nrf_mtx_destroy(nrf_mtx_t * p_mtx)
  109. {
  110. ASSERT(p_mtx != NULL);
  111. // Add memory barrier to ensure that any memory operations protected by the mutex complete
  112. // before the mutex is destroyed.
  113. __DMB();
  114. *p_mtx = NRF_MTX_UNLOCKED;
  115. }
  116. __STATIC_INLINE bool nrf_mtx_trylock(nrf_mtx_t * p_mtx)
  117. {
  118. ASSERT(p_mtx != NULL);
  119. uint32_t old_val = nrf_atomic_u32_fetch_store(p_mtx, NRF_MTX_LOCKED);
  120. // Add memory barrier to ensure that the mutex is locked before any memory operations protected
  121. // by the mutex are started.
  122. __DMB();
  123. return (old_val == NRF_MTX_UNLOCKED);
  124. }
  125. __STATIC_INLINE void nrf_mtx_unlock(nrf_mtx_t * p_mtx)
  126. {
  127. ASSERT(p_mtx != NULL);
  128. ASSERT(*p_mtx == NRF_MTX_LOCKED);
  129. // Add memory barrier to ensure that any memory operations protected by the mutex complete
  130. // before the mutex is unlocked.
  131. __DMB();
  132. *p_mtx = NRF_MTX_UNLOCKED;
  133. }
  134. #endif //SUPPRESS_INLINE_IMPLEMENTATION
  135. #endif // NRF_MTX_H__
  136. /** @} */