Update.
[platform/upstream/glibc.git] / resolv / gai_suspend.c
1 /* Copyright (C) 2001 Free Software Foundation, Inc.
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
6    modify it under the terms of the GNU Library General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
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
13    Library General Public License for more details.
14
15    You should have received a copy of the GNU Library General Public
16    License along with the GNU C Library; see the file COPYING.LIB.  If not,
17    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18    Boston, MA 02111-1307, USA.  */
19
20 #include <errno.h>
21 #include <netdb.h>
22 #include <pthread.h>
23 #include <stdlib.h>
24 #include <sys/time.h>
25
26 #include "gai_misc.h"
27
28
29 int
30 gai_suspend (const struct gaicb *const list[], int ent,
31              const struct timespec *timeout)
32 {
33   struct waitlist waitlist[ent];
34   struct requestlist *requestlist[ent];
35   pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
36   int cnt;
37   int dummy;
38   int none = 1;
39   int result;
40
41   /* Request the mutex.  */
42   pthread_mutex_lock (&__gai_requests_mutex);
43
44   /* There is not yet a finished request.  Signal the request that
45      we are working for it.  */
46   for (cnt = 0; cnt < ent; ++cnt)
47     if (list[cnt] != NULL && list[cnt]->__return == EAI_INPROGRESS)
48       {
49         requestlist[cnt] = __gai_find_request (list[cnt]);
50
51         if (requestlist[cnt] != NULL)
52           {
53             waitlist[cnt].cond = &cond;
54             waitlist[cnt].next = requestlist[cnt]->waiting;
55             waitlist[cnt].counterp = &dummy;
56             waitlist[cnt].sigevp = NULL;
57             waitlist[cnt].caller_pid = 0;       /* Not needed.  */
58             requestlist[cnt]->waiting = &waitlist[cnt];
59             none = 0;
60           }
61       }
62
63   if (none)
64     {
65       if (cnt < ent)
66         /* There is an entry which is finished.  */
67         result = 0;
68       else
69         result = EAI_ALLDONE;
70     }
71   else
72     {
73       /* There is no request done but some are still being worked on.  */
74       int oldstate;
75
76       /* Since `pthread_cond_wait'/`pthread_cond_timedwait' are cancelation
77          points we must be careful.  We added entries to the waiting lists
78          which we must remove.  So defer cancelation for now.  */
79       pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &oldstate);
80
81       if (timeout == NULL)
82         result = pthread_cond_wait (&cond, &__gai_requests_mutex);
83       else
84         {
85           /* We have to convert the relative timeout value into an
86              absolute time value with pthread_cond_timedwait expects.  */
87           struct timeval now;
88           struct timespec abstime;
89
90           __gettimeofday (&now, NULL);
91           abstime.tv_nsec = timeout->tv_nsec + now.tv_usec * 1000;
92           abstime.tv_sec = timeout->tv_sec + now.tv_sec;
93           if (abstime.tv_nsec >= 1000000000)
94             {
95               abstime.tv_nsec -= 1000000000;
96               abstime.tv_sec += 1;
97             }
98
99           result = pthread_cond_timedwait (&cond, &__gai_requests_mutex,
100                                            &abstime);
101         }
102
103       /* Now remove the entry in the waiting list for all requests
104          which didn't terminate.  */
105       for (cnt = 0; cnt < ent; ++cnt)
106         if (list[cnt] != NULL && list[cnt]->__return == EAI_INPROGRESS
107             && requestlist[cnt] != NULL)
108           {
109             struct waitlist **listp = &requestlist[cnt]->waiting;
110
111             /* There is the chance that we cannot find our entry anymore.
112                This could happen if the request terminated and restarted
113                again.  */
114             while (*listp != NULL && *listp != &waitlist[cnt])
115               listp = &(*listp)->next;
116
117             if (*listp != NULL)
118               *listp = (*listp)->next;
119           }
120
121       /* Now it's time to restore the cancelation state.  */
122       pthread_setcancelstate (oldstate, NULL);
123
124       /* Release the conditional variable.  */
125       if (pthread_cond_destroy (&cond) != 0)
126         /* This must never happen.  */
127         abort ();
128
129       if (result != 0)
130         {
131           /* An error occurred.  Possibly it's EINTR.  We have to translate
132              the timeout error report of `pthread_cond_timedwait' to the
133              form expected from `gai_suspend'.  */
134           if (__builtin_expect (result, ETIMEDOUT) == ETIMEDOUT)
135             result = EAI_AGAIN;
136           else if (result == EINTR)
137             result = EAI_INTR;
138           else
139             result = EAI_SYSTEM;
140         }
141     }
142
143   /* Release the mutex.  */
144   pthread_mutex_unlock (&__gai_requests_mutex);
145
146   return result;
147 }