aio_cancel.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* Cancel requests associated with given file descriptor.
  2. Copyright (C) 1997-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. /* We use an UGLY hack to prevent gcc from finding us cheating. The
  17. implementation of aio_cancel and aio_cancel64 are identical and so
  18. we want to avoid code duplication by using aliases. But gcc sees
  19. the different parameter lists and prints a warning. We define here
  20. a function so that aio_cancel64 has no prototype. */
  21. #ifndef aio_cancel
  22. #define aio_cancel64 XXX
  23. #include <aio.h>
  24. /* And undo the hack. */
  25. #undef aio_cancel64
  26. #endif
  27. #include <assert.h>
  28. #include <errno.h>
  29. #include <fcntl.h>
  30. #include <aio_misc.h>
  31. int
  32. aio_cancel (int fildes, struct aiocb *aiocbp)
  33. {
  34. struct requestlist *req = NULL;
  35. int result = AIO_ALLDONE;
  36. /* If fildes is invalid, error. */
  37. if (fcntl (fildes, F_GETFL) < 0)
  38. {
  39. __set_errno (EBADF);
  40. return -1;
  41. }
  42. /* Request the mutex. */
  43. pthread_mutex_lock (&__aio_requests_mutex);
  44. /* We are asked to cancel a specific AIO request. */
  45. if (aiocbp != NULL)
  46. {
  47. /* If the AIO request is not for this descriptor it has no value
  48. to look for the request block. */
  49. if (aiocbp->aio_fildes != fildes)
  50. {
  51. pthread_mutex_unlock (&__aio_requests_mutex);
  52. __set_errno (EINVAL);
  53. return -1;
  54. }
  55. else if (aiocbp->__error_code == EINPROGRESS)
  56. {
  57. struct requestlist *last = NULL;
  58. req = __aio_find_req_fd (fildes);
  59. if (req == NULL)
  60. {
  61. not_found:
  62. pthread_mutex_unlock (&__aio_requests_mutex);
  63. __set_errno (EINVAL);
  64. return -1;
  65. }
  66. while (req->aiocbp != (aiocb_union *) aiocbp)
  67. {
  68. last = req;
  69. req = req->next_prio;
  70. if (req == NULL)
  71. goto not_found;
  72. }
  73. /* Don't remove the entry if a thread is already working on it. */
  74. if (req->running == allocated)
  75. {
  76. result = AIO_NOTCANCELED;
  77. req = NULL;
  78. }
  79. else
  80. {
  81. /* We can remove the entry. */
  82. __aio_remove_request (last, req, 0);
  83. result = AIO_CANCELED;
  84. req->next_prio = NULL;
  85. }
  86. }
  87. }
  88. else
  89. {
  90. /* Find the beginning of the list of all requests for this
  91. desriptor. */
  92. req = __aio_find_req_fd (fildes);
  93. /* If any request is worked on by a thread it must be the first.
  94. So either we can delete all requests or all but the first. */
  95. if (req != NULL)
  96. {
  97. if (req->running == allocated)
  98. {
  99. struct requestlist *old = req;
  100. req = req->next_prio;
  101. old->next_prio = NULL;
  102. result = AIO_NOTCANCELED;
  103. if (req != NULL)
  104. __aio_remove_request (old, req, 1);
  105. }
  106. else
  107. {
  108. result = AIO_CANCELED;
  109. /* We can remove the entry. */
  110. __aio_remove_request (NULL, req, 1);
  111. }
  112. }
  113. }
  114. /* Mark requests as canceled and send signal. */
  115. while (req != NULL)
  116. {
  117. struct requestlist *old = req;
  118. assert (req->running == yes || req->running == queued);
  119. req->aiocbp->aiocb.__error_code = ECANCELED;
  120. req->aiocbp->aiocb.__return_value = -1;
  121. __aio_notify (req);
  122. req = req->next_prio;
  123. __aio_free_request (old);
  124. }
  125. /* Release the mutex. */
  126. pthread_mutex_unlock (&__aio_requests_mutex);
  127. return result;
  128. }
  129. #ifndef aio_cancel
  130. weak_alias (aio_cancel, aio_cancel64)
  131. #endif