]>
Commit | Line | Data |
---|---|---|
2b778ceb | 1 | /* Copyright (C) 2001-2021 Free Software Foundation, Inc. |
2ace5721 UD |
2 | This file is part of the GNU C Library. |
3 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2001. | |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
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. | |
2ace5721 UD |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
2ace5721 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 16 | License along with the GNU C Library; if not, see |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
2ace5721 UD |
18 | |
19 | #include <netdb.h> | |
20 | #include <pthread.h> | |
21 | #include <stdlib.h> | |
f1762c0c | 22 | #include <gai_misc.h> |
2ace5721 | 23 | |
dbb949f5 FW |
24 | #if !PTHREAD_IN_LIBC |
25 | /* The available function names differ outside of libc. (In libc, we | |
26 | need to use hidden aliases to avoid the PLT.) */ | |
27 | #define __pthread_attr_init pthread_attr_init | |
28 | #define __pthread_attr_setdetachstate pthread_attr_setdetachstate | |
29 | #define __pthread_cond_signal pthread_cond_signal | |
30 | #define __pthread_cond_timedwait pthread_cond_timedwait | |
31 | #define __pthread_create pthread_create | |
32 | #endif | |
2ace5721 | 33 | |
f1762c0c UD |
34 | struct notify_func |
35 | { | |
36 | void (*func) (sigval_t); | |
37 | sigval_t value; | |
38 | }; | |
2ace5721 UD |
39 | |
40 | static void * | |
41 | notify_func_wrapper (void *arg) | |
42 | { | |
f1762c0c UD |
43 | gai_start_notify_thread (); |
44 | struct notify_func *const n = arg; | |
45 | void (*func) (sigval_t) = n->func; | |
46 | sigval_t value = n->value; | |
47 | free (n); | |
48 | (*func) (value); | |
2ace5721 UD |
49 | return NULL; |
50 | } | |
51 | ||
52 | ||
53 | int | |
2ace5721 UD |
54 | __gai_notify_only (struct sigevent *sigev, pid_t caller_pid) |
55 | { | |
56 | int result = 0; | |
57 | ||
58 | /* Send the signal to notify about finished processing of the request. */ | |
59 | if (sigev->sigev_notify == SIGEV_THREAD) | |
60 | { | |
61 | /* We have to start a thread. */ | |
62 | pthread_t tid; | |
63 | pthread_attr_t attr, *pattr; | |
64 | ||
65 | pattr = (pthread_attr_t *) sigev->sigev_notify_attributes; | |
66 | if (pattr == NULL) | |
67 | { | |
dbb949f5 FW |
68 | __pthread_attr_init (&attr); |
69 | __pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); | |
2ace5721 UD |
70 | pattr = &attr; |
71 | } | |
72 | ||
f1762c0c UD |
73 | /* SIGEV may be freed as soon as we return, so we cannot let the |
74 | notification thread use that pointer. Even though a sigval_t is | |
75 | only one word and the same size as a void *, we cannot just pass | |
76 | the value through pthread_create as the argument and have the new | |
77 | thread run the user's function directly, because on some machines | |
78 | the calling convention for a union like sigval_t is different from | |
79 | that for a pointer type like void *. */ | |
80 | struct notify_func *nf = malloc (sizeof *nf); | |
81 | if (nf == NULL) | |
2ace5721 | 82 | result = -1; |
f1762c0c UD |
83 | else |
84 | { | |
85 | nf->func = sigev->sigev_notify_function; | |
86 | nf->value = sigev->sigev_value; | |
dbb949f5 | 87 | if (__pthread_create (&tid, pattr, notify_func_wrapper, nf) < 0) |
f1762c0c UD |
88 | { |
89 | free (nf); | |
90 | result = -1; | |
91 | } | |
92 | } | |
2ace5721 UD |
93 | } |
94 | else if (sigev->sigev_notify == SIGEV_SIGNAL) | |
95 | /* We have to send a signal. */ | |
96 | if (__gai_sigqueue (sigev->sigev_signo, sigev->sigev_value, caller_pid) | |
97 | < 0) | |
98 | result = -1; | |
99 | ||
100 | return result; | |
101 | } | |
102 | ||
103 | ||
104 | void | |
2ace5721 UD |
105 | __gai_notify (struct requestlist *req) |
106 | { | |
107 | struct waitlist *waitlist; | |
108 | ||
109 | /* Now also notify possibly waiting threads. */ | |
110 | waitlist = req->waiting; | |
111 | while (waitlist != NULL) | |
112 | { | |
113 | struct waitlist *next = waitlist->next; | |
114 | ||
2ace5721 | 115 | if (waitlist->sigevp == NULL) |
f1762c0c UD |
116 | { |
117 | #ifdef DONT_NEED_GAI_MISC_COND | |
118 | GAI_MISC_NOTIFY (waitlist); | |
119 | #else | |
120 | /* Decrement the counter. */ | |
121 | --*waitlist->counterp; | |
122 | ||
123 | pthread_cond_signal (waitlist->cond); | |
124 | #endif | |
125 | } | |
2ace5721 | 126 | else |
c0c3f78a | 127 | /* This is part of an asynchronous `getaddrinfo_a' operation. If |
2ace5721 | 128 | this request is the last one, send the signal. */ |
f1762c0c | 129 | if (--*waitlist->counterp == 0) |
2ace5721 UD |
130 | { |
131 | __gai_notify_only (waitlist->sigevp, waitlist->caller_pid); | |
132 | /* This is tricky. See getaddrinfo_a.c for the reason why | |
133 | this works. */ | |
134 | free ((void *) waitlist->counterp); | |
135 | } | |
136 | ||
137 | waitlist = next; | |
138 | } | |
139 | } |