aio_initialize.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /****************************************************************************
  2. * fs/aio/aio_initialize.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <assert.h>
  25. #include <errno.h>
  26. #include <queue.h>
  27. #include <nuttx/sched.h>
  28. #include <nuttx/semaphore.h>
  29. #include "aio/aio.h"
  30. #ifdef CONFIG_FS_AIO
  31. /****************************************************************************
  32. * Private Data
  33. ****************************************************************************/
  34. /* This is an array of pre-allocated AIO containers */
  35. static struct aio_container_s g_aioc_alloc[CONFIG_FS_NAIOC];
  36. /* This is a list of free AIO containers */
  37. static dq_queue_t g_aioc_free;
  38. /* This counting semaphore tracks the number of free AIO containers */
  39. static sem_t g_aioc_freesem;
  40. /* This binary semaphore supports exclusive access to the list of pending
  41. * asynchronous I/O. g_aio_holder and a_aio_count support the reentrant
  42. * lock.
  43. */
  44. static sem_t g_aio_exclsem;
  45. static pid_t g_aio_holder;
  46. static uint16_t g_aio_count;
  47. /****************************************************************************
  48. * Public Data
  49. ****************************************************************************/
  50. /* This is a list of pending asynchronous I/O. The user must hold the
  51. * lock on this list in order to access the list.
  52. */
  53. dq_queue_t g_aio_pending;
  54. /****************************************************************************
  55. * Public Functions
  56. ****************************************************************************/
  57. /****************************************************************************
  58. * Name: aio_initialize
  59. *
  60. * Description:
  61. * Perform one-time initialization of the asynchronous I/O sub-system
  62. *
  63. * Input Parameters:
  64. * None
  65. *
  66. * Returned Value:
  67. * None
  68. *
  69. ****************************************************************************/
  70. void aio_initialize(void)
  71. {
  72. int i;
  73. /* Initialize counting semaphores */
  74. nxsem_init(&g_aioc_freesem, 0, CONFIG_FS_NAIOC);
  75. nxsem_set_protocol(&g_aioc_freesem, SEM_PRIO_NONE);
  76. nxsem_init(&g_aio_exclsem, 0, 1);
  77. g_aio_holder = INVALID_PROCESS_ID;
  78. /* Initialize the container queues */
  79. dq_init(&g_aioc_free);
  80. dq_init(&g_aio_pending);
  81. /* Add all of the pre-allocated AIO containers to the free list */
  82. for (i = 0; i < CONFIG_FS_NAIOC; i++)
  83. {
  84. /* Add the container to the free list */
  85. dq_addlast(&g_aioc_alloc[i].aioc_link, &g_aioc_free);
  86. }
  87. }
  88. /****************************************************************************
  89. * Name: aio_lock/aio_unlock
  90. *
  91. * Description:
  92. * Take/give the lock on the pending asynchronous I/O list
  93. *
  94. * Input Parameters:
  95. * None
  96. *
  97. * Returned Value:
  98. * aio_lock() return -ECANCELED if the calling thread is canceled.
  99. *
  100. ****************************************************************************/
  101. int aio_lock(void)
  102. {
  103. pid_t me = getpid();
  104. int ret = OK;
  105. /* Does this thread already hold the semaphore? */
  106. if (g_aio_holder == me)
  107. {
  108. /* Yes, just increment the counts held */
  109. DEBUGASSERT(g_aio_count > 0 && g_aio_count < UINT16_MAX);
  110. g_aio_count++;
  111. }
  112. else
  113. {
  114. ret = nxsem_wait_uninterruptible(&g_aio_exclsem);
  115. if (ret >= 0)
  116. {
  117. /* And mark it as ours */
  118. g_aio_holder = me;
  119. g_aio_count = 1;
  120. }
  121. }
  122. return ret;
  123. }
  124. void aio_unlock(void)
  125. {
  126. DEBUGASSERT(g_aio_holder == getpid() && g_aio_count > 0);
  127. /* Would decrementing the count release the lock? */
  128. if (g_aio_count <= 1)
  129. {
  130. /* Yes.. that we will no longer be the holder */
  131. g_aio_holder = INVALID_PROCESS_ID;
  132. g_aio_count = 0;
  133. nxsem_post(&g_aio_exclsem);
  134. }
  135. else
  136. {
  137. /* Otherwise, just decrement the count. We still hold the lock. */
  138. g_aio_count--;
  139. }
  140. }
  141. /****************************************************************************
  142. * Name: aioc_alloc
  143. *
  144. * Description:
  145. * Allocate a new AIO container by taking the next, pre-allocated
  146. * container from the free list. This function will wait until
  147. * aioc_free() is called in the event that there is no free container
  148. * available in the free list.
  149. *
  150. * Input Parameters:
  151. * None
  152. *
  153. * Returned Value:
  154. * A reference to the allocated AIO container. This allocation never
  155. * fails because the logic will wait in the event that there is no free
  156. * container.
  157. *
  158. ****************************************************************************/
  159. FAR struct aio_container_s *aioc_alloc(void)
  160. {
  161. FAR struct aio_container_s *aioc = NULL;
  162. int ret;
  163. /* Take a count from semaphore, thus guaranteeing that we have an AIO
  164. * container set aside for us.
  165. */
  166. ret = nxsem_wait_uninterruptible(&g_aioc_freesem);
  167. if (ret < 0)
  168. {
  169. return NULL;
  170. }
  171. /* Get our AIO container */
  172. ret = aio_lock();
  173. if (ret >= 0)
  174. {
  175. aioc = (FAR struct aio_container_s *)dq_remfirst(&g_aioc_free);
  176. aio_unlock();
  177. DEBUGASSERT(aioc);
  178. }
  179. return aioc;
  180. }
  181. /****************************************************************************
  182. * Name: aioc_free
  183. *
  184. * Description:
  185. * Free an AIO container by returning it to the free list and, perhaps,
  186. * awakening any threads waiting for that resource
  187. *
  188. * Input Parameters:
  189. * aioc - The AIO container to be free
  190. *
  191. * Returned Value:
  192. * None
  193. *
  194. ****************************************************************************/
  195. void aioc_free(FAR struct aio_container_s *aioc)
  196. {
  197. int ret;
  198. DEBUGASSERT(aioc);
  199. /* Return the container to the free list */
  200. do
  201. {
  202. ret = aio_lock();
  203. /* The only possible error should be if we were awakened only by
  204. * thread cancellation.
  205. */
  206. DEBUGASSERT(ret == OK || ret == -ECANCELED);
  207. }
  208. while (ret < 0);
  209. dq_addlast(&aioc->aioc_link, &g_aioc_free);
  210. aio_unlock();
  211. /* The post the counting semaphore, announcing the availability of the
  212. * free AIO container.
  213. */
  214. nxsem_post(&g_aioc_freesem);
  215. }
  216. #endif /* CONFIG_FS_AIO */