sem_close.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /****************************************************************************
  2. * fs/semaphore/sem_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 <errno.h>
  26. #include <nuttx/kmalloc.h>
  27. #include <nuttx/semaphore.h>
  28. #include <nuttx/fs/fs.h>
  29. #include "inode/inode.h"
  30. #ifdef CONFIG_FS_NAMED_SEMAPHORES
  31. /****************************************************************************
  32. * Public Functions
  33. ****************************************************************************/
  34. /****************************************************************************
  35. * Name: sem_close
  36. *
  37. * Description:
  38. * This function is called to indicate that the calling task is finished
  39. * with the specified named semaphore, 'sem'. The sem_close() deallocates
  40. * any system resources allocated by the system for this named semaphore.
  41. *
  42. * If the semaphore has not been removed with a call to sem_unlink(), then
  43. * sem_close() has no effect on the named semaphore. However, when the
  44. * named semaphore has been fully unlinked, the semaphore will vanish when
  45. * the last task closes it.
  46. *
  47. * Input Parameters:
  48. * sem - semaphore descriptor
  49. *
  50. * Returned Value:
  51. * 0 (OK), or -1 (ERROR) if unsuccessful.
  52. *
  53. * Assumptions:
  54. * - Care must be taken to avoid risking the deletion of a semaphore that
  55. * another calling task has already locked.
  56. * - sem_close must not be called for an un-named semaphore
  57. *
  58. ****************************************************************************/
  59. int sem_close(FAR sem_t *sem)
  60. {
  61. FAR struct nsem_inode_s *nsem;
  62. struct inode *inode;
  63. int ret;
  64. DEBUGASSERT(sem);
  65. /* Upcast to get back to out internal representation */
  66. nsem = (FAR struct nsem_inode_s *)sem;
  67. DEBUGASSERT(nsem->ns_inode);
  68. inode = nsem->ns_inode;
  69. /* Decrement the reference count on the inode */
  70. do
  71. {
  72. ret = inode_semtake();
  73. /* The only error that is expected is due to thread cancellation.
  74. * At this point, we must continue to free the semaphore anyway.
  75. */
  76. DEBUGASSERT(ret == OK || ret == -ECANCELED);
  77. }
  78. while (ret < 0);
  79. if (inode->i_crefs > 0)
  80. {
  81. inode->i_crefs--;
  82. }
  83. /* If the semaphore was previously unlinked and the reference count has
  84. * decremented to zero, then release the semaphore and delete the inode
  85. * now.
  86. */
  87. if (inode->i_crefs <= 0 && (inode->i_flags & FSNODEFLAG_DELETED) != 0)
  88. {
  89. /* Destroy the semaphore and free the container */
  90. nxsem_destroy(&nsem->ns_sem);
  91. group_free(NULL, nsem);
  92. /* Release and free the inode container. If it has been properly
  93. * unlinked, then the peer pointer should be NULL.
  94. */
  95. inode_semgive();
  96. DEBUGASSERT(inode->i_peer == NULL);
  97. inode_free(inode);
  98. return OK;
  99. }
  100. inode_semgive();
  101. return OK;
  102. }
  103. #endif /* CONFIG_FS_NAMED_SEMAPHORES */