mq_close.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /****************************************************************************
  2. * fs/mqueue/mq_close.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 <mqueue.h>
  26. #include <assert.h>
  27. #include <debug.h>
  28. #include <nuttx/kmalloc.h>
  29. #include <nuttx/sched.h>
  30. #include <nuttx/mqueue.h>
  31. #include "inode/inode.h"
  32. #include "mqueue/mqueue.h"
  33. /****************************************************************************
  34. * Public Functions
  35. ****************************************************************************/
  36. /****************************************************************************
  37. * Name: nxmq_close_group
  38. *
  39. * Description:
  40. * This function is used to indicate that all threads in the group are
  41. * finished with the specified message queue mqdes. The nxmq_close_group()
  42. * deallocates any system resources allocated by the system for use by
  43. * this task for its message queue.
  44. *
  45. * Input Parameters:
  46. * mqdes - Message queue descriptor.
  47. * group - Group that has the open descriptor.
  48. *
  49. * Returned Value:
  50. * Zero (OK) if the message queue is closed successfully. Otherwise, a
  51. * negated errno value is returned.
  52. *
  53. ****************************************************************************/
  54. int nxmq_close_group(mqd_t mqdes, FAR struct task_group_s *group)
  55. {
  56. FAR struct mqueue_inode_s *msgq;
  57. FAR struct inode *inode;
  58. int ret = OK;
  59. DEBUGASSERT(mqdes != NULL && group != NULL);
  60. /* Verify the inputs */
  61. if (mqdes)
  62. {
  63. sched_lock();
  64. /* Find the message queue associated with the message descriptor */
  65. msgq = mqdes->msgq;
  66. DEBUGASSERT(msgq && msgq->inode);
  67. /* Close/free the message descriptor */
  68. ret = nxmq_desclose_group(mqdes, group);
  69. if (ret >= 0)
  70. {
  71. /* Get the inode from the message queue structure */
  72. inode = msgq->inode;
  73. DEBUGASSERT(inode->u.i_mqueue == msgq);
  74. /* Decrement the reference count on the inode, possibly free it */
  75. mq_inode_release(inode);
  76. }
  77. sched_unlock();
  78. }
  79. return ret;
  80. }
  81. /****************************************************************************
  82. * Name: nxmq_close
  83. *
  84. * Description:
  85. * This is an internal OS interface. It is functionally equivalent to
  86. * mq_close() except that:
  87. *
  88. * - It is not a cancellation point, and
  89. * - It does not modify the errno value.
  90. *
  91. * See comments with mq_close() for a more complete description of the
  92. * behavior of this function
  93. *
  94. * Input Parameters:
  95. * mqdes - Message queue descriptor.
  96. *
  97. * Returned Value:
  98. * This is an internal OS interface and should not be used by applications.
  99. * It follows the NuttX internal error return policy: Zero (OK) is
  100. * returned on success. A negated errno value is returned on failure.
  101. *
  102. ****************************************************************************/
  103. int nxmq_close(mqd_t mqdes)
  104. {
  105. FAR struct tcb_s *rtcb = (FAR struct tcb_s *)nxsched_self();
  106. int ret;
  107. /* Lock the scheduler to prevent any asynchronous task delete operation
  108. * (unlikely).
  109. */
  110. sched_lock();
  111. DEBUGASSERT(mqdes != NULL && rtcb != NULL && rtcb->group != NULL);
  112. /* Then perform the close operation */
  113. ret = nxmq_close_group(mqdes, rtcb->group);
  114. sched_unlock();
  115. return ret;
  116. }
  117. /****************************************************************************
  118. * Name: mq_close
  119. *
  120. * Description:
  121. * This function is used to indicate that the calling task is finished
  122. * with the specified message queue mqdes. The mq_close() deallocates
  123. * any system resources allocated by the system for use by this task for
  124. * its message queue.
  125. *
  126. * If the calling task has attached a notification to the message queue
  127. * via this mqdes, this attachment will be removed and the message queue
  128. * is available for another process to attach a notification.
  129. *
  130. * Input Parameters:
  131. * mqdes - Message queue descriptor.
  132. *
  133. * Returned Value:
  134. * 0 (OK) if the message queue is closed successfully,
  135. * otherwise, -1 (ERROR).
  136. *
  137. * Assumptions:
  138. * - The behavior of a task that is blocked on either a [nx]mq_send() or
  139. * [nx]mq_receive() is undefined when mq_close() is called.
  140. * - The results of using this message queue descriptor after a successful
  141. * return from mq_close() is undefined.
  142. *
  143. ****************************************************************************/
  144. int mq_close(mqd_t mqdes)
  145. {
  146. int ret;
  147. ret = nxmq_close(mqdes);
  148. if (ret < 0)
  149. {
  150. set_errno(-ret);
  151. ret = ERROR;
  152. }
  153. return ret;
  154. }
  155. /****************************************************************************
  156. * Name: mq_inode_release
  157. *
  158. * Description:
  159. * Release a reference count on a message queue inode.
  160. *
  161. * Input Parameters:
  162. * inode - The message queue inode
  163. *
  164. * Returned Value:
  165. * None
  166. *
  167. ****************************************************************************/
  168. void mq_inode_release(FAR struct inode *inode)
  169. {
  170. int ret;
  171. /* Decrement the reference count on the inode */
  172. do
  173. {
  174. ret = inode_semtake();
  175. /* The only error that is expected is due to thread cancellation.
  176. * At this point, we must continue to free the mqueue anyway.
  177. */
  178. DEBUGASSERT(ret == OK || ret == -ECANCELED);
  179. }
  180. while (ret < 0);
  181. if (inode->i_crefs > 0)
  182. {
  183. inode->i_crefs--;
  184. }
  185. /* If the message queue was previously unlinked and the reference count
  186. * has decremented to zero, then release the message queue and delete
  187. * the inode now.
  188. */
  189. if (inode->i_crefs <= 0 && (inode->i_flags & FSNODEFLAG_DELETED) != 0)
  190. {
  191. FAR struct mqueue_inode_s *msgq = inode->u.i_mqueue;
  192. DEBUGASSERT(msgq);
  193. /* Free the message queue (and any messages left in it) */
  194. nxmq_free_msgq(msgq);
  195. inode->u.i_mqueue = NULL;
  196. /* Release and free the inode container. If it has been properly
  197. * unlinked, then the peer pointer should be NULL.
  198. */
  199. inode_semgive();
  200. DEBUGASSERT(inode->i_peer == NULL);
  201. inode_free(inode);
  202. return;
  203. }
  204. inode_semgive();
  205. }