iob_alloc_qentry.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /****************************************************************************
  2. * mm/iob/iob_alloc_qentry.c
  3. *
  4. * Copyright (C) 2014, 2016-2017 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <semaphore.h>
  40. #include <assert.h>
  41. #include <errno.h>
  42. #include <nuttx/irq.h>
  43. #include <nuttx/arch.h>
  44. #include <nuttx/mm/iob.h>
  45. #include "iob.h"
  46. #if CONFIG_IOB_NCHAINS > 0
  47. /****************************************************************************
  48. * Private Functions
  49. ****************************************************************************/
  50. /****************************************************************************
  51. * Name: iob_alloc_qcommitted
  52. *
  53. * Description:
  54. * Allocate an I/O buffer by taking the buffer at the head of the committed
  55. * list.
  56. *
  57. ****************************************************************************/
  58. static FAR struct iob_qentry_s *iob_alloc_qcommitted(void)
  59. {
  60. FAR struct iob_qentry_s *iobq = NULL;
  61. irqstate_t flags;
  62. /* We don't know what context we are called from so we use extreme measures
  63. * to protect the committed list: We disable interrupts very briefly.
  64. */
  65. flags = enter_critical_section();
  66. /* Take the I/O buffer from the head of the committed list */
  67. iobq = g_iob_qcommitted;
  68. if (iobq != NULL)
  69. {
  70. /* Remove the I/O buffer from the committed list */
  71. g_iob_qcommitted = iobq->qe_flink;
  72. /* Put the I/O buffer in a known state */
  73. iobq->qe_head = NULL; /* Nothing is contained */
  74. }
  75. leave_critical_section(flags);
  76. return iobq;
  77. }
  78. /****************************************************************************
  79. * Name: iob_allocwait_qentry
  80. *
  81. * Description:
  82. * Allocate an I/O buffer chain container by taking the buffer at the head
  83. * of the free list. This function is intended only for internal use by
  84. * the IOB module.
  85. *
  86. ****************************************************************************/
  87. static FAR struct iob_qentry_s *iob_allocwait_qentry(void)
  88. {
  89. FAR struct iob_qentry_s *qentry;
  90. irqstate_t flags;
  91. int ret = OK;
  92. /* The following must be atomic; interrupt must be disabled so that there
  93. * is no conflict with interrupt level I/O buffer chain container
  94. * allocations. This is not as bad as it sounds because interrupts will be
  95. * re-enabled while we are waiting for I/O buffers to become free.
  96. */
  97. flags = enter_critical_section();
  98. /* Try to get an I/O buffer chain container. If successful, the semaphore
  99. * count will bedecremented atomically.
  100. */
  101. qentry = iob_tryalloc_qentry();
  102. while (ret == OK && qentry == NULL)
  103. {
  104. /* If not successful, then the semaphore count was less than or equal
  105. * to zero (meaning that there are no free buffers). We need to wait
  106. * for an I/O buffer chain container to be released when the
  107. * semaphore count will be incremented.
  108. */
  109. ret = nxsem_wait(&g_qentry_sem);
  110. if (ret < 0)
  111. {
  112. /* EINTR is not an error! EINTR simply means that we were
  113. * awakened by a signal and we should try again.
  114. *
  115. * REVISIT: Many end-user interfaces are required to return
  116. * with an error if EINTR is set. Most uses of this function
  117. * is in internal, non-user logic. But are there cases where
  118. * the error should be returned.
  119. */
  120. if (ret == -EINTR)
  121. {
  122. /* Force a success indication so that we will continue looping. */
  123. ret = OK;
  124. }
  125. }
  126. else
  127. {
  128. /* When we wake up from wait successfully, an I/O buffer chain container was
  129. * freed and we hold a count for one IOB. Unless somehting
  130. * failed, we should have an IOB waiting for us in the
  131. * committed list.
  132. */
  133. qentry = iob_alloc_qcommitted();
  134. DEBUGASSERT(qentry != NULL);
  135. if (qentry == NULL)
  136. {
  137. /* This should not fail, but we allow for that possibility to
  138. * handle any potential, non-obvious race condition. Perhaps
  139. * the free IOB ended up in the g_iob_free list?
  140. *
  141. * We need release our count so that it is available to
  142. * iob_tryalloc(), perhaps allowing another thread to take our
  143. * count. In that event, iob_tryalloc() will fail above and
  144. * we will have to wait again.
  145. */
  146. nxsem_post(&g_qentry_sem);
  147. qentry = iob_tryalloc_qentry();
  148. }
  149. }
  150. }
  151. leave_critical_section(flags);
  152. return qentry;
  153. }
  154. /****************************************************************************
  155. * Public Functions
  156. ****************************************************************************/
  157. /****************************************************************************
  158. * Name: iob_alloc_qentry
  159. *
  160. * Description:
  161. * Allocate an I/O buffer chain container by taking the buffer at the head
  162. * of the free list. This function is intended only for internal use by
  163. * the IOB module.
  164. *
  165. ****************************************************************************/
  166. FAR struct iob_qentry_s *iob_alloc_qentry(void)
  167. {
  168. /* Were we called from the interrupt level? */
  169. if (up_interrupt_context() || sched_idletask())
  170. {
  171. /* Yes, then try to allocate an I/O buffer without waiting */
  172. return iob_tryalloc_qentry();
  173. }
  174. else
  175. {
  176. /* Then allocate an I/O buffer, waiting as necessary */
  177. return iob_allocwait_qentry();
  178. }
  179. }
  180. /****************************************************************************
  181. * Name: iob_tryalloc_qentry
  182. *
  183. * Description:
  184. * Try to allocate an I/O buffer chain container by taking the buffer at
  185. * the head of the free list without waiting for the container to become
  186. * free. This function is intended only for internal use by the IOB module.
  187. *
  188. ****************************************************************************/
  189. FAR struct iob_qentry_s *iob_tryalloc_qentry(void)
  190. {
  191. FAR struct iob_qentry_s *iobq;
  192. irqstate_t flags;
  193. /* We don't know what context we are called from so we use extreme measures
  194. * to protect the free list: We disable interrupts very briefly.
  195. */
  196. flags = enter_critical_section();
  197. iobq = g_iob_freeqlist;
  198. if (iobq)
  199. {
  200. /* Remove the I/O buffer chain container from the free list and
  201. * decrement the counting semaphore that tracks the number of free
  202. * containers.
  203. */
  204. g_iob_freeqlist = iobq->qe_flink;
  205. /* Take a semaphore count. Note that we cannot do this in
  206. * in the orthodox way by calling nxsem_wait() or nxsem_trywait()
  207. * because this function may be called from an interrupt
  208. * handler. Fortunately we know at at least one free buffer
  209. * so a simple decrement is all that is needed.
  210. */
  211. g_qentry_sem.semcount--;
  212. DEBUGASSERT(g_qentry_sem.semcount >= 0);
  213. /* Put the I/O buffer in a known state */
  214. iobq->qe_head = NULL; /* Nothing is contained */
  215. }
  216. leave_critical_section(flags);
  217. return iobq;
  218. }
  219. #endif /* CONFIG_IOB_NCHAINS > 0 */