aio.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /****************************************************************************
  2. * fs/aio/aio.h
  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. #ifndef __FS_AIO_AIO_H
  21. #define __FS_AIO_AIO_H
  22. /****************************************************************************
  23. * Included Files
  24. ****************************************************************************/
  25. #include <nuttx/config.h>
  26. #include <sys/types.h>
  27. #include <string.h>
  28. #include <aio.h>
  29. #include <queue.h>
  30. #include <nuttx/wqueue.h>
  31. #include <nuttx/net/net.h>
  32. #ifdef CONFIG_FS_AIO
  33. /****************************************************************************
  34. * Pre-processor Definitions
  35. ****************************************************************************/
  36. /* Configuration ************************************************************/
  37. /* Number of pre-allocated AIO Control block containers */
  38. #ifndef CONFIG_FS_NAIOC
  39. # define CONFIG_FS_NAIOC 8
  40. #endif
  41. /****************************************************************************
  42. * Public Types
  43. ****************************************************************************/
  44. /* This structure contains one AIO control block and appends information
  45. * needed by the logic running on the worker thread. These structures are
  46. * pre-allocated, the number pre-allocated controlled by CONFIG_FS_NAIOC.
  47. */
  48. struct file;
  49. struct aio_container_s
  50. {
  51. dq_entry_t aioc_link; /* Supports a doubly linked list */
  52. FAR struct aiocb *aioc_aiocbp; /* The contained AIO control block */
  53. FAR struct file *aioc_filep; /* File structure to use with the I/O */
  54. struct work_s aioc_work; /* Used to defer I/O to the work thread */
  55. pid_t aioc_pid; /* ID of the waiting task */
  56. #ifdef CONFIG_PRIORITY_INHERITANCE
  57. uint8_t aioc_prio; /* Priority of the waiting task */
  58. #endif
  59. };
  60. /****************************************************************************
  61. * Public Data
  62. ****************************************************************************/
  63. #undef EXTERN
  64. #if defined(__cplusplus)
  65. #define EXTERN extern "C"
  66. extern "C"
  67. {
  68. #else
  69. #define EXTERN extern
  70. #endif
  71. /* This is a list of pending asynchronous I/O. The user must hold the
  72. * lock on this list in order to access the list.
  73. */
  74. EXTERN dq_queue_t g_aio_pending;
  75. /****************************************************************************
  76. * Public Function Prototypes
  77. ****************************************************************************/
  78. /****************************************************************************
  79. * Name: aio_initialize
  80. *
  81. * Description:
  82. * Perform one-time initialization of the asynchronous I/O sub-system
  83. *
  84. * Input Parameters:
  85. * None
  86. *
  87. * Returned Value:
  88. * None
  89. *
  90. ****************************************************************************/
  91. void aio_initialize(void);
  92. /****************************************************************************
  93. * Name: aio_lock/aio_unlock
  94. *
  95. * Description:
  96. * Take/give the lock on the pending asynchronous I/O list. These locks
  97. * are implemented with re-entrant semaphores -- deadlocks will not occur
  98. * if the lock is taken multiple times on the same thread.
  99. *
  100. * Input Parameters:
  101. * None
  102. *
  103. * Returned Value:
  104. * aio_lock() return -ECANCELED if the calling thread is canceled.
  105. *
  106. ****************************************************************************/
  107. int aio_lock(void);
  108. void aio_unlock(void);
  109. /****************************************************************************
  110. * Name: aioc_alloc
  111. *
  112. * Description:
  113. * Allocate a new AIO container by taking the next, pre-allocated
  114. * container from the free list. This function will wait until
  115. * aioc_free() is called in the event that there is no free container
  116. * available in the free list.
  117. *
  118. * Input Parameters:
  119. * None
  120. *
  121. * Returned Value:
  122. * A reference to the allocated AIO container. This allocation never
  123. * fails because the logic will wait in the event that there is no free
  124. * container.
  125. *
  126. ****************************************************************************/
  127. FAR struct aio_container_s *aioc_alloc(void);
  128. /****************************************************************************
  129. * Name: aioc_free
  130. *
  131. * Description:
  132. * Free an AIO container by returning it to the free list and, perhaps,
  133. * awakening any threads waiting for that resource
  134. *
  135. * Input Parameters:
  136. * aioc - The AIO container to be free
  137. *
  138. * Returned Value:
  139. * None
  140. *
  141. ****************************************************************************/
  142. void aioc_free(FAR struct aio_container_s *aioc);
  143. /****************************************************************************
  144. * Name: aio_contain
  145. *
  146. * Description:
  147. * Create and initialize a container for the provided AIO control block
  148. *
  149. * Input Parameters:
  150. * aiocbp - The AIO control block pointer
  151. *
  152. * Returned Value:
  153. * A reference to the new AIO control block container. This function
  154. * will not fail but will wait if necessary for the resources to perform
  155. * this operation. NULL will be returned on certain errors with the
  156. * errno value already set appropriately.
  157. *
  158. ****************************************************************************/
  159. FAR struct aio_container_s *aio_contain(FAR struct aiocb *aiocbp);
  160. /****************************************************************************
  161. * Name: aioc_decant
  162. *
  163. * Description:
  164. * Remove the AIO control block from the container and free all resources
  165. * used by the container.
  166. *
  167. * Input Parameters:
  168. * aioc - Pointer to the AIO control block container
  169. *
  170. * Returned Value:
  171. * A pointer to the no-longer contained AIO control block.
  172. *
  173. ****************************************************************************/
  174. FAR struct aiocb *aioc_decant(FAR struct aio_container_s *aioc);
  175. /****************************************************************************
  176. * Name: aio_queue
  177. *
  178. * Description:
  179. * Schedule the asynchronous I/O on the low priority work queue
  180. *
  181. * Input Parameters:
  182. * arg - Worker argument. In this case, a pointer to an instance of
  183. * struct aiocb cast to void *.
  184. *
  185. * Returned Value:
  186. * Zero (OK) on success. Otherwise, -1 is returned and the errno is set
  187. * appropriately.
  188. *
  189. ****************************************************************************/
  190. int aio_queue(FAR struct aio_container_s *aioc, worker_t worker);
  191. /****************************************************************************
  192. * Name: aio_signal
  193. *
  194. * Description:
  195. * Signal the client that an I/O has completed.
  196. *
  197. * Input Parameters:
  198. * pid - ID of the task to signal
  199. * aiocbp - Pointer to the asynchronous I/O state structure that includes
  200. * information about how to signal the client
  201. *
  202. * Returned Value:
  203. * Zero (OK) if the client was successfully signalled. Otherwise, a
  204. * negated errno value is returned.
  205. *
  206. ****************************************************************************/
  207. int aio_signal(pid_t pid, FAR struct aiocb *aiocbp);
  208. #undef EXTERN
  209. #if defined(__cplusplus)
  210. }
  211. #endif
  212. #endif /* CONFIG_FS_AIO */
  213. #endif /* __FS_AIO_AIO_H */