nrf_fifo.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * Copyright (c) 2015 - 2018, 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. #ifndef NRF_FIFO_H__
  41. #define NRF_FIFO_H__
  42. #include <stdint.h>
  43. #include <stdbool.h>
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. /**
  48. * @file nrf_fifo.h
  49. *
  50. * @defgroup iot_socket_fifo FIFO
  51. * @ingroup iot_sdk_socket
  52. * @{
  53. * @brief A wait-free bounded FIFO of pointers for single producer/single consumer use.
  54. *
  55. * This FIFO is safe to use in single producer/single consumer patterns. In addition, the following
  56. * restrictions apply for init/deinit:
  57. *
  58. * a) nrf_fifo_enq() and nrf_fifo_deq() may only be called after nrf_fifo_init() is called.
  59. *
  60. * b) All calls to nrf_fifo_enq() and nrf_fifo_deq() must be finished and no new calls must be made before nrf_fifo_deinit() is called.
  61. *
  62. * These restrictions must be handled by the user of the module, for instance by using a mutex.
  63. */
  64. /**
  65. * @brief Wait function for blocking enqueue/dequeue.
  66. *
  67. * Should return NRF_SUCCESS as long as there are no errors while waiting.
  68. */
  69. typedef uint32_t (*fifo_wait_fn)(void);
  70. /**
  71. * @brief Flush function called on deinit.
  72. *
  73. * On deinit, this function will be called with each remaining element in the FIFO as argument. This
  74. * can be used to ensure that memory is deallocated properly.
  75. *
  76. * @param[in] p_data Pointer to data that is flushed from FIFO.
  77. */
  78. typedef void (*fifo_flush_fn)(void * p_data);
  79. /**
  80. * @brief FIFO data structure.
  81. */
  82. typedef struct {
  83. void ** pp_elements; /**< The array of elements in the FIFO. */
  84. uint32_t nmemb; /**< The number of elements in this FIFO. */
  85. fifo_wait_fn wait; /**< The wait function used if blocking. */
  86. fifo_flush_fn flush; /**< The flush function used on deinit. */
  87. volatile uint32_t read_pos; /**< Read pointer to next element to read. */
  88. volatile uint32_t write_pos; /**< Write pointer to next element to write. */
  89. } nrf_fifo_t;
  90. /**
  91. * @brief Function for initializing the FIFO.
  92. *
  93. * @param[out] p_fifo The FIFO to initialize.
  94. * @param[in] nmemb The maximum number of elements in the FIFO.
  95. * @param[in] wait_fn The wait function to use for blocking enqueue/dequeue. If NULL, the enq/deq
  96. * functions will never block.
  97. * @param[in] flush_fn The flush function to call on deinit. If NULL, the flush function will not
  98. * be called.
  99. *
  100. * @retval NRF_SUCCESS if fifo was initialized successfully.
  101. */
  102. uint32_t nrf_fifo_init(nrf_fifo_t * p_fifo, uint32_t nmemb, fifo_wait_fn wait_fn, fifo_flush_fn flush_fn);
  103. /**
  104. * @brief Function for deinitializing the FIFO.
  105. *
  106. * Frees all memory allocated by this FIFO. All elements are removed. If a flush function was
  107. * specified in nrf_fifo_init(), the function will be called for each remaining element in the
  108. * FIFO.
  109. *
  110. * @param[in, out] p_fifo The FIFO to deinitialize.
  111. */
  112. void nrf_fifo_deinit(nrf_fifo_t * p_fifo);
  113. /**
  114. * @brief Function for enqueuing an element on the FIFO.
  115. *
  116. * @param[in, out] p_fifo The FIFO to enqueue elements on.
  117. * @param[in] p_ctx The pointer to enqueue.
  118. * @param[in] wait If true, this function will block until the FIFO has available space. Any
  119. * errors returned by this function will be propagated to the caller.
  120. *
  121. * @retval NRF_SUCCESS if the element was queued.
  122. * @retval NRF_ERROR_NO_MEM if wait was set to false and no space was available.
  123. */
  124. uint32_t nrf_fifo_enq(nrf_fifo_t * p_fifo, void * p_ctx, bool wait);
  125. /**
  126. * @brief Function for dequeuing an element from the FIFO.
  127. *
  128. * @param[in, out] p_fifo The FIFO to dequeue elements from.
  129. * @param[out] pp_ctx Pointer to where the dequeued element should be stored.
  130. * @param[in] wait If true, this function will block until the FIFO has elements for dequeuing.
  131. * Any errors returned by this function will be propagated to the caller.
  132. *
  133. * @retval NRF_SUCCESS if the element was queued.
  134. * @retval NRF_ERROR_NO_MEM if wait was set to false and no space was available.
  135. */
  136. uint32_t nrf_fifo_deq(nrf_fifo_t * p_fifo, void ** pp_ctx, bool wait);
  137. /**
  138. * @brief Function for checking if the FIFO is empty.
  139. *
  140. * @param[in] p_fifo The FIFO to check.
  141. * @return true if empty, false if not.
  142. */
  143. bool nrf_fifo_empty(nrf_fifo_t * p_fifo);
  144. /**
  145. * @brief Function for checking if the FIFO is full.
  146. *
  147. * @param[in] p_fifo The FIFO to check.
  148. * @return true if full, false if not.
  149. */
  150. bool nrf_fifo_full(nrf_fifo_t * p_fifo);
  151. /**@} */
  152. #ifdef __cplusplus
  153. }
  154. #endif
  155. #endif // NRF_FIFO_H__