gai_notify.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* Copyright (C) 2001-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2001.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <netdb.h>
  16. #include <pthread.h>
  17. #include <stdlib.h>
  18. #include <gai_misc.h>
  19. struct notify_func
  20. {
  21. void (*func) (sigval_t);
  22. sigval_t value;
  23. };
  24. static void *
  25. notify_func_wrapper (void *arg)
  26. {
  27. gai_start_notify_thread ();
  28. struct notify_func *const n = arg;
  29. void (*func) (sigval_t) = n->func;
  30. sigval_t value = n->value;
  31. free (n);
  32. (*func) (value);
  33. return NULL;
  34. }
  35. int
  36. __gai_notify_only (struct sigevent *sigev, pid_t caller_pid)
  37. {
  38. int result = 0;
  39. /* Send the signal to notify about finished processing of the request. */
  40. if (sigev->sigev_notify == SIGEV_THREAD)
  41. {
  42. /* We have to start a thread. */
  43. pthread_t tid;
  44. pthread_attr_t attr, *pattr;
  45. pattr = (pthread_attr_t *) sigev->sigev_notify_attributes;
  46. if (pattr == NULL)
  47. {
  48. pthread_attr_init (&attr);
  49. pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
  50. pattr = &attr;
  51. }
  52. /* SIGEV may be freed as soon as we return, so we cannot let the
  53. notification thread use that pointer. Even though a sigval_t is
  54. only one word and the same size as a void *, we cannot just pass
  55. the value through pthread_create as the argument and have the new
  56. thread run the user's function directly, because on some machines
  57. the calling convention for a union like sigval_t is different from
  58. that for a pointer type like void *. */
  59. struct notify_func *nf = malloc (sizeof *nf);
  60. if (nf == NULL)
  61. result = -1;
  62. else
  63. {
  64. nf->func = sigev->sigev_notify_function;
  65. nf->value = sigev->sigev_value;
  66. if (pthread_create (&tid, pattr, notify_func_wrapper, nf) < 0)
  67. {
  68. free (nf);
  69. result = -1;
  70. }
  71. }
  72. }
  73. else if (sigev->sigev_notify == SIGEV_SIGNAL)
  74. /* We have to send a signal. */
  75. if (__gai_sigqueue (sigev->sigev_signo, sigev->sigev_value, caller_pid)
  76. < 0)
  77. result = -1;
  78. return result;
  79. }
  80. void
  81. __gai_notify (struct requestlist *req)
  82. {
  83. struct waitlist *waitlist;
  84. /* Now also notify possibly waiting threads. */
  85. waitlist = req->waiting;
  86. while (waitlist != NULL)
  87. {
  88. struct waitlist *next = waitlist->next;
  89. if (waitlist->sigevp == NULL)
  90. {
  91. #ifdef DONT_NEED_GAI_MISC_COND
  92. GAI_MISC_NOTIFY (waitlist);
  93. #else
  94. /* Decrement the counter. */
  95. --*waitlist->counterp;
  96. pthread_cond_signal (waitlist->cond);
  97. #endif
  98. }
  99. else
  100. /* This is part of an asynchronous `getaddrinfo_a' operation. If
  101. this request is the last one, send the signal. */
  102. if (--*waitlist->counterp == 0)
  103. {
  104. __gai_notify_only (waitlist->sigevp, waitlist->caller_pid);
  105. /* This is tricky. See getaddrinfo_a.c for the reason why
  106. this works. */
  107. free ((void *) waitlist->counterp);
  108. }
  109. waitlist = next;
  110. }
  111. }