]> git.ipfire.org Git - thirdparty/glibc.git/blame - resolv/gai_notify.c
(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[thirdparty/glibc.git] / resolv / gai_notify.c
CommitLineData
a334319f 1/* Copyright (C) 2001 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
AJ
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
2ace5721
UD
19
20#include <netdb.h>
21#include <pthread.h>
22#include <stdlib.h>
23
a334319f 24#include "gai_misc.h"
2ace5721
UD
25
26
27static void *
28notify_func_wrapper (void *arg)
29{
a334319f
UD
30 struct sigevent *sigev = arg;
31 sigev->sigev_notify_function (sigev->sigev_value);
2ace5721
UD
32 return NULL;
33}
34
35
36int
37internal_function
38__gai_notify_only (struct sigevent *sigev, pid_t caller_pid)
39{
40 int result = 0;
41
42 /* Send the signal to notify about finished processing of the request. */
43 if (sigev->sigev_notify == SIGEV_THREAD)
44 {
45 /* We have to start a thread. */
46 pthread_t tid;
47 pthread_attr_t attr, *pattr;
48
49 pattr = (pthread_attr_t *) sigev->sigev_notify_attributes;
50 if (pattr == NULL)
51 {
52 pthread_attr_init (&attr);
53 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
54 pattr = &attr;
55 }
56
a334319f 57 if (pthread_create (&tid, pattr, notify_func_wrapper, sigev) < 0)
2ace5721
UD
58 result = -1;
59 }
60 else if (sigev->sigev_notify == SIGEV_SIGNAL)
61 /* We have to send a signal. */
62 if (__gai_sigqueue (sigev->sigev_signo, sigev->sigev_value, caller_pid)
63 < 0)
64 result = -1;
65
66 return result;
67}
68
69
70void
71internal_function
72__gai_notify (struct requestlist *req)
73{
74 struct waitlist *waitlist;
75
76 /* Now also notify possibly waiting threads. */
77 waitlist = req->waiting;
78 while (waitlist != NULL)
79 {
80 struct waitlist *next = waitlist->next;
81
a334319f
UD
82 /* Decrement the counter. This is used in both cases. */
83 --*waitlist->counterp;
84
2ace5721 85 if (waitlist->sigevp == NULL)
a334319f 86 pthread_cond_signal (waitlist->cond);
2ace5721
UD
87 else
88 /* This is part of a asynchronous `getaddrinfo_a' operation. If
89 this request is the last one, send the signal. */
a334319f 90 if (*waitlist->counterp == 0)
2ace5721
UD
91 {
92 __gai_notify_only (waitlist->sigevp, waitlist->caller_pid);
93 /* This is tricky. See getaddrinfo_a.c for the reason why
94 this works. */
95 free ((void *) waitlist->counterp);
96 }
97
98 waitlist = next;
99 }
100}