]>
Commit | Line | Data |
---|---|---|
04277e02 | 1 | /* Copyright (C) 2001-2019 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 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://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 | |
2ace5721 | 24 | |
f1762c0c UD |
25 | struct notify_func |
26 | { | |
27 | void (*func) (sigval_t); | |
28 | sigval_t value; | |
29 | }; | |
2ace5721 UD |
30 | |
31 | static void * | |
32 | notify_func_wrapper (void *arg) | |
33 | { | |
f1762c0c UD |
34 | gai_start_notify_thread (); |
35 | struct notify_func *const n = arg; | |
36 | void (*func) (sigval_t) = n->func; | |
37 | sigval_t value = n->value; | |
38 | free (n); | |
39 | (*func) (value); | |
2ace5721 UD |
40 | return NULL; |
41 | } | |
42 | ||
43 | ||
44 | int | |
2ace5721 UD |
45 | __gai_notify_only (struct sigevent *sigev, pid_t caller_pid) |
46 | { | |
47 | int result = 0; | |
48 | ||
49 | /* Send the signal to notify about finished processing of the request. */ | |
50 | if (sigev->sigev_notify == SIGEV_THREAD) | |
51 | { | |
52 | /* We have to start a thread. */ | |
53 | pthread_t tid; | |
54 | pthread_attr_t attr, *pattr; | |
55 | ||
56 | pattr = (pthread_attr_t *) sigev->sigev_notify_attributes; | |
57 | if (pattr == NULL) | |
58 | { | |
59 | pthread_attr_init (&attr); | |
60 | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); | |
61 | pattr = &attr; | |
62 | } | |
63 | ||
f1762c0c UD |
64 | /* SIGEV may be freed as soon as we return, so we cannot let the |
65 | notification thread use that pointer. Even though a sigval_t is | |
66 | only one word and the same size as a void *, we cannot just pass | |
67 | the value through pthread_create as the argument and have the new | |
68 | thread run the user's function directly, because on some machines | |
69 | the calling convention for a union like sigval_t is different from | |
70 | that for a pointer type like void *. */ | |
71 | struct notify_func *nf = malloc (sizeof *nf); | |
72 | if (nf == NULL) | |
2ace5721 | 73 | result = -1; |
f1762c0c UD |
74 | else |
75 | { | |
76 | nf->func = sigev->sigev_notify_function; | |
77 | nf->value = sigev->sigev_value; | |
78 | if (pthread_create (&tid, pattr, notify_func_wrapper, nf) < 0) | |
79 | { | |
80 | free (nf); | |
81 | result = -1; | |
82 | } | |
83 | } | |
2ace5721 UD |
84 | } |
85 | else if (sigev->sigev_notify == SIGEV_SIGNAL) | |
86 | /* We have to send a signal. */ | |
87 | if (__gai_sigqueue (sigev->sigev_signo, sigev->sigev_value, caller_pid) | |
88 | < 0) | |
89 | result = -1; | |
90 | ||
91 | return result; | |
92 | } | |
93 | ||
94 | ||
95 | void | |
2ace5721 UD |
96 | __gai_notify (struct requestlist *req) |
97 | { | |
98 | struct waitlist *waitlist; | |
99 | ||
100 | /* Now also notify possibly waiting threads. */ | |
101 | waitlist = req->waiting; | |
102 | while (waitlist != NULL) | |
103 | { | |
104 | struct waitlist *next = waitlist->next; | |
105 | ||
2ace5721 | 106 | if (waitlist->sigevp == NULL) |
f1762c0c UD |
107 | { |
108 | #ifdef DONT_NEED_GAI_MISC_COND | |
109 | GAI_MISC_NOTIFY (waitlist); | |
110 | #else | |
111 | /* Decrement the counter. */ | |
112 | --*waitlist->counterp; | |
113 | ||
114 | pthread_cond_signal (waitlist->cond); | |
115 | #endif | |
116 | } | |
2ace5721 | 117 | else |
c0c3f78a | 118 | /* This is part of an asynchronous `getaddrinfo_a' operation. If |
2ace5721 | 119 | this request is the last one, send the signal. */ |
f1762c0c | 120 | if (--*waitlist->counterp == 0) |
2ace5721 UD |
121 | { |
122 | __gai_notify_only (waitlist->sigevp, waitlist->caller_pid); | |
123 | /* This is tricky. See getaddrinfo_a.c for the reason why | |
124 | this works. */ | |
125 | free ((void *) waitlist->counterp); | |
126 | } | |
127 | ||
128 | waitlist = next; | |
129 | } | |
130 | } |