aio.h 8.1 KB

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