aioc_contain.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /****************************************************************************
  2. * fs/aio/aioc_contain.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 <sched.h>
  25. #include <errno.h>
  26. #include <nuttx/sched.h>
  27. #include <nuttx/fs/fs.h>
  28. #include <nuttx/net/net.h>
  29. #include "aio/aio.h"
  30. #ifdef CONFIG_FS_AIO
  31. /****************************************************************************
  32. * Public Functions
  33. ****************************************************************************/
  34. /****************************************************************************
  35. * Name: aio_contain
  36. *
  37. * Description:
  38. * Create and initialize a container for the provided AIO control block
  39. *
  40. * Input Parameters:
  41. * aiocbp - The AIO control block pointer
  42. *
  43. * Returned Value:
  44. * A reference to the new AIO control block container. This function
  45. * will not fail but will wait if necessary for the resources to perform
  46. * this operation. NULL will be returned on certain errors with the
  47. * errno value already set appropriately.
  48. *
  49. ****************************************************************************/
  50. FAR struct aio_container_s *aio_contain(FAR struct aiocb *aiocbp)
  51. {
  52. FAR struct aio_container_s *aioc;
  53. FAR struct file *filep;
  54. #ifdef CONFIG_PRIORITY_INHERITANCE
  55. struct sched_param param;
  56. #endif
  57. int ret;
  58. /* Get the file structure corresponding to the file descriptor. */
  59. ret = fs_getfilep(aiocbp->aio_fildes, &filep);
  60. if (ret < 0)
  61. {
  62. goto errout;
  63. }
  64. DEBUGASSERT(filep != NULL);
  65. /* Allocate the AIO control block container, waiting for one to become
  66. * available if necessary. This should not fail except for in the case
  67. * where the calling thread is canceled.
  68. */
  69. aioc = aioc_alloc();
  70. if (aioc != NULL)
  71. {
  72. /* Initialize the container */
  73. memset(aioc, 0, sizeof(struct aio_container_s));
  74. aioc->aioc_aiocbp = aiocbp;
  75. aioc->aioc_filep = filep;
  76. aioc->aioc_pid = getpid();
  77. #ifdef CONFIG_PRIORITY_INHERITANCE
  78. DEBUGVERIFY(nxsched_get_param (aioc->aioc_pid, &param));
  79. aioc->aioc_prio = param.sched_priority;
  80. #endif
  81. /* Add the container to the pending transfer list. */
  82. ret = aio_lock();
  83. if (ret < 0)
  84. {
  85. aioc_free(aioc);
  86. goto errout;
  87. }
  88. dq_addlast(&aioc->aioc_link, &g_aio_pending);
  89. aio_unlock();
  90. }
  91. return aioc;
  92. errout:
  93. set_errno(-ret);
  94. return NULL;
  95. }
  96. /****************************************************************************
  97. * Name: aioc_decant
  98. *
  99. * Description:
  100. * Remove the AIO control block from the container and free all resources
  101. * used by the container.
  102. *
  103. * Input Parameters:
  104. * aioc - Pointer to the AIO control block container
  105. *
  106. * Returned Value:
  107. * A pointer to the no-longer contained AIO control block.
  108. *
  109. ****************************************************************************/
  110. FAR struct aiocb *aioc_decant(FAR struct aio_container_s *aioc)
  111. {
  112. FAR struct aiocb *aiocbp = NULL;
  113. int ret;
  114. DEBUGASSERT(aioc);
  115. /* Remove the container to the pending transfer list. */
  116. ret = aio_lock();
  117. if (ret >= 0)
  118. {
  119. dq_rem(&aioc->aioc_link, &g_aio_pending);
  120. /* De-cant the AIO control block and return the container to the
  121. * free list.
  122. */
  123. aiocbp = aioc->aioc_aiocbp;
  124. aioc_free(aioc);
  125. aio_unlock();
  126. }
  127. return aiocbp;
  128. }
  129. #endif /* CONFIG_FS_AIO */