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/>. */
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
;
37 unsigned int cntr
= 1;
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
;
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
];
65 struct __timespec64 ts
;
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;
81 /* There is an entry which is finished. */
88 /* There is no request done but some are still being worked on. */
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
98 GAI_MISC_WAIT (result
, cntr
, timeout
== NULL
? NULL
: &ts
, 1);
100 struct timespec ts32
= valid_timespec64_to_timespec (ts
);
101 result
= pthread_cond_timedwait (&cond
, &__gai_requests_mutex
,
102 timeout
== NULL
? NULL
: &ts32
);
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
116 while (*listp
!= NULL
&& *listp
!= &waitlist
[cnt
])
117 listp
= &(*listp
)->next
;
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. */
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
))
140 else if (result
== EINTR
)
147 /* Release the mutex. */
148 __pthread_mutex_unlock (&__gai_requests_mutex
);
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
);
159 # endif /* PTHREAD_IN_LIBC */
161 #else /* __TIMESIZE != 64 */
163 libc_hidden_ver (___gai_suspend_time64
, __gai_suspend_time64
)
164 versioned_symbol (libc
, ___gai_suspend_time64
, __gai_suspend_time64
,
166 # else /* !PTHREAD_IN_LIBC */
168 hidden_ver (___gai_suspend_time64
, __gai_suspend_time64
)
170 #endif /* !PTHREAD_IN_LIBC */
173 ___gai_suspend (const struct gaicb
*const list
[], int ent
,
174 const struct timespec
*timeout
)
176 struct __timespec64 ts64
;
179 ts64
= valid_timespec_to_timespec64 (*timeout
);
181 return __gai_suspend_time64 (list
, ent
, timeout
!= NULL
? &ts64
: NULL
);
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
);
189 weak_alias (___gai_suspend
, gai_suspend
)
190 # endif /* !PTHREAD_IN_LIBC */
191 #endif /* __TIMESIZE != 64 */