mq_desclose.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /****************************************************************************
  2. * sched/mqueue/mq_desclose.c
  3. *
  4. * Copyright (C) 2007, 2009, 2013-2017 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <mqueue.h>
  40. #include <sched.h>
  41. #include <string.h>
  42. #include <assert.h>
  43. #include <queue.h>
  44. #include <nuttx/sched.h>
  45. #include <nuttx/mqueue.h>
  46. #include "mqueue/mqueue.h"
  47. /****************************************************************************
  48. * Private Functions
  49. ****************************************************************************/
  50. /****************************************************************************
  51. * Name: mq_desfree
  52. *
  53. * Description:
  54. * Deallocate a message queue descriptor but returning it to the free list
  55. *
  56. * Input Parameters:
  57. * mqdes - message queue descriptor to free
  58. *
  59. ****************************************************************************/
  60. #define mq_desfree(mqdes) sq_addlast((FAR sq_entry_t*)mqdes, &g_desfree)
  61. /****************************************************************************
  62. * Public Functions
  63. ****************************************************************************/
  64. /****************************************************************************
  65. * Name: nxmq_desclose_group
  66. *
  67. * Description:
  68. * This function performs the portion of the mq_close operation related
  69. * to freeing resource used by the message queue descriptor itself.
  70. *
  71. * Input Parameters:
  72. * mqdes - Message queue descriptor.
  73. * group - Group that has the open descriptor.
  74. *
  75. * Returned Value:
  76. * Zero (OK) is returned on success; a negated errno value is returned on
  77. * and failure.
  78. *
  79. * Assumptions:
  80. * - Called only from mq_close() with the scheduler locked.
  81. *
  82. ****************************************************************************/
  83. int nxmq_desclose_group(mqd_t mqdes, FAR struct task_group_s *group)
  84. {
  85. FAR struct mqueue_inode_s *msgq;
  86. #ifdef CONFIG_DEBUG_FEATURES
  87. mqd_t mq_ptr;
  88. #endif
  89. DEBUGASSERT(mqdes != NULL && group != NULL);
  90. #ifdef CONFIG_DEBUG_FEATURES
  91. /* Check that msgq is valid for closing. It must be owned by the current
  92. * group. NOTE the call to sq_rem() below would corrupt the descriptor
  93. * list if mqdes did not lie in the list.
  94. */
  95. mq_ptr = (mqd_t)sq_peek(&group->tg_msgdesq);
  96. while (mq_ptr)
  97. {
  98. if (mq_ptr == mqdes)
  99. {
  100. break;
  101. }
  102. mq_ptr = (mqd_t)sq_next(mq_ptr);
  103. }
  104. DEBUGASSERT(mq_ptr != NULL);
  105. if (mq_ptr == NULL)
  106. {
  107. /* 'mqdes' does not lie in the group's list of message descriptors. */
  108. return -EPERM;
  109. }
  110. #endif
  111. /* Remove the message descriptor from the current group's list of message
  112. * descriptors.
  113. */
  114. sq_rem((FAR sq_entry_t *)mqdes, &group->tg_msgdesq);
  115. /* Find the message queue associated with the message descriptor */
  116. msgq = mqdes->msgq;
  117. /* Check if the calling task has a notification attached to the message
  118. * queue via this mqdes.
  119. */
  120. if (msgq->ntmqdes == mqdes)
  121. {
  122. memset(&msgq->ntevent, 0, sizeof(struct sigevent));
  123. msgq->ntpid = INVALID_PROCESS_ID;
  124. msgq->ntmqdes = NULL;
  125. nxsig_cancel_notification(&msgq->ntwork);
  126. }
  127. /* Deallocate the message descriptor */
  128. mq_desfree(mqdes);
  129. return OK;
  130. }