x86-64: Add Avoid_Short_Distance_REP_MOVSB
[glibc.git] / resolv / gai_suspend.c
blob965bb113a4d5422e8f495049d751d1dcd8213d51
1 /* Copyright (C) 2001-2021 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2001.
5 The GNU C Library is free software; you can redistribute it and/or
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.
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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <netdb.h>
21 #include <pthread.h>
22 #include <stdlib.h>
23 #include <sys/time.h>
25 #include <gai_misc.h>
27 int
28 ___gai_suspend_time64 (const struct gaicb *const list[], int ent,
29 const struct __timespec64 *timeout)
31 struct waitlist waitlist[ent];
32 struct requestlist *requestlist[ent];
33 #ifndef DONT_NEED_GAI_MISC_COND
34 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
35 #endif
36 int cnt;
37 unsigned int cntr = 1;
38 int none = 1;
39 int result;
41 /* Request the mutex. */
42 __pthread_mutex_lock (&__gai_requests_mutex);
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)
49 requestlist[cnt] = __gai_find_request (list[cnt]);
51 if (requestlist[cnt] != NULL)
53 #ifndef DONT_NEED_GAI_MISC_COND
54 waitlist[cnt].cond = &cond;
55 #endif
56 waitlist[cnt].next = requestlist[cnt]->waiting;
57 waitlist[cnt].counterp = &cntr;
58 waitlist[cnt].sigevp = NULL;
59 waitlist[cnt].caller_pid = 0; /* Not needed. */
60 requestlist[cnt]->waiting = &waitlist[cnt];
61 none = 0;
65 struct __timespec64 ts;
66 if (timeout != NULL)
68 __clock_gettime64 (CLOCK_MONOTONIC, &ts);
69 ts.tv_sec += timeout->tv_sec;
70 ts.tv_nsec += timeout->tv_nsec;
71 if (ts.tv_nsec >= 1000000000)
73 ts.tv_nsec -= 1000000000;
74 ts.tv_sec++;
78 if (none)
80 if (cnt < ent)
81 /* There is an entry which is finished. */
82 result = 0;
83 else
84 result = EAI_ALLDONE;
86 else
88 /* There is no request done but some are still being worked on. */
89 int oldstate;
91 /* Since `pthread_cond_wait'/`pthread_cond_timedwait' are cancelation
92 points we must be careful. We added entries to the waiting lists
93 which we must remove. So defer cancelation for now. */
94 __pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &oldstate);
96 #ifdef DONT_NEED_GAI_MISC_COND
97 result = 0;
98 GAI_MISC_WAIT (result, cntr, timeout == NULL ? NULL : &ts, 1);
99 #else
100 struct timespec ts32 = valid_timespec64_to_timespec (ts);
101 result = pthread_cond_timedwait (&cond, &__gai_requests_mutex,
102 timeout == NULL ? NULL : &ts32);
103 #endif
105 /* Now remove the entry in the waiting list for all requests
106 which didn't terminate. */
107 for (cnt = 0; cnt < ent; ++cnt)
108 if (list[cnt] != NULL && list[cnt]->__return == EAI_INPROGRESS
109 && requestlist[cnt] != NULL)
111 struct waitlist **listp = &requestlist[cnt]->waiting;
113 /* There is the chance that we cannot find our entry anymore.
114 This could happen if the request terminated and restarted
115 again. */
116 while (*listp != NULL && *listp != &waitlist[cnt])
117 listp = &(*listp)->next;
119 if (*listp != NULL)
120 *listp = (*listp)->next;
123 /* Now it's time to restore the cancelation state. */
124 __pthread_setcancelstate (oldstate, NULL);
126 #ifndef DONT_NEED_GAI_MISC_COND
127 /* Release the conditional variable. */
128 if (pthread_cond_destroy (&cond) != 0)
129 /* This must never happen. */
130 abort ();
131 #endif
133 if (result != 0)
135 /* An error occurred. Possibly it's EINTR. We have to translate
136 the timeout error report of `pthread_cond_timedwait' to the
137 form expected from `gai_suspend'. */
138 if (__glibc_likely (result == ETIMEDOUT))
139 result = EAI_AGAIN;
140 else if (result == EINTR)
141 result = EAI_INTR;
142 else
143 result = EAI_SYSTEM;
147 /* Release the mutex. */
148 __pthread_mutex_unlock (&__gai_requests_mutex);
150 return result;
153 #if __TIMESIZE == 64
154 # if PTHREAD_IN_LIBC
155 versioned_symbol (libc, ___gai_suspend_time64, gai_suspend, GLIBC_2_34);
156 # if OTHER_SHLIB_COMPAT (libanl, GLIBC_2_2_3, GLIBC_2_34)
157 compat_symbol (libanl, ___gai_suspend_time64, gai_suspend, GLIBC_2_2_3);
158 # endif
159 # endif /* PTHREAD_IN_LIBC */
161 #else /* __TIMESIZE != 64 */
162 # if PTHREAD_IN_LIBC
163 libc_hidden_ver (___gai_suspend_time64, __gai_suspend_time64)
164 versioned_symbol (libc, ___gai_suspend_time64, __gai_suspend_time64,
165 GLIBC_2_34);
166 # else /* !PTHREAD_IN_LIBC */
167 # if IS_IN (libanl)
168 hidden_ver (___gai_suspend_time64, __gai_suspend_time64)
169 # endif
170 #endif /* !PTHREAD_IN_LIBC */
173 ___gai_suspend (const struct gaicb *const list[], int ent,
174 const struct timespec *timeout)
176 struct __timespec64 ts64;
178 if (timeout != NULL)
179 ts64 = valid_timespec_to_timespec64 (*timeout);
181 return __gai_suspend_time64 (list, ent, timeout != NULL ? &ts64 : NULL);
183 #if PTHREAD_IN_LIBC
184 versioned_symbol (libc, ___gai_suspend, gai_suspend, GLIBC_2_34);
185 # if OTHER_SHLIB_COMPAT (libanl, GLIBC_2_2_3, GLIBC_2_34)
186 compat_symbol (libanl, ___gai_suspend, gai_suspend, GLIBC_2_2_3);
187 # endif
188 # else
189 weak_alias (___gai_suspend, gai_suspend)
190 # endif /* !PTHREAD_IN_LIBC */
191 #endif /* __TIMESIZE != 64 */