1 | /* Copyright (C) 2001-2017 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 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. |
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 | Lesser General Public License for more details. |
14 | |
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 | <http://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <errno.h> |
20 | #include <netdb.h> |
21 | #include <pthread.h> |
22 | #include <stdlib.h> |
23 | #include <sys/time.h> |
24 | |
25 | #include <gai_misc.h> |
26 | |
27 | |
28 | int |
29 | gai_suspend (const struct gaicb *const list[], int ent, |
30 | const struct timespec *timeout) |
31 | { |
32 | struct waitlist waitlist[ent]; |
33 | struct requestlist *requestlist[ent]; |
34 | #ifndef DONT_NEED_GAI_MISC_COND |
35 | pthread_cond_t cond = PTHREAD_COND_INITIALIZER; |
36 | #endif |
37 | int cnt; |
38 | unsigned int cntr = 1; |
39 | int none = 1; |
40 | int result; |
41 | |
42 | /* Request the mutex. */ |
43 | pthread_mutex_lock (&__gai_requests_mutex); |
44 | |
45 | /* There is not yet a finished request. Signal the request that |
46 | we are working for it. */ |
47 | for (cnt = 0; cnt < ent; ++cnt) |
48 | if (list[cnt] != NULL && list[cnt]->__return == EAI_INPROGRESS) |
49 | { |
50 | requestlist[cnt] = __gai_find_request (list[cnt]); |
51 | |
52 | if (requestlist[cnt] != NULL) |
53 | { |
54 | #ifndef DONT_NEED_GAI_MISC_COND |
55 | waitlist[cnt].cond = &cond; |
56 | #endif |
57 | waitlist[cnt].next = requestlist[cnt]->waiting; |
58 | waitlist[cnt].counterp = &cntr; |
59 | waitlist[cnt].sigevp = NULL; |
60 | waitlist[cnt].caller_pid = 0; /* Not needed. */ |
61 | requestlist[cnt]->waiting = &waitlist[cnt]; |
62 | none = 0; |
63 | } |
64 | } |
65 | |
66 | if (none) |
67 | { |
68 | if (cnt < ent) |
69 | /* There is an entry which is finished. */ |
70 | result = 0; |
71 | else |
72 | result = EAI_ALLDONE; |
73 | } |
74 | else |
75 | { |
76 | /* There is no request done but some are still being worked on. */ |
77 | int oldstate; |
78 | |
79 | /* Since `pthread_cond_wait'/`pthread_cond_timedwait' are cancelation |
80 | points we must be careful. We added entries to the waiting lists |
81 | which we must remove. So defer cancelation for now. */ |
82 | pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &oldstate); |
83 | |
84 | #ifdef DONT_NEED_GAI_MISC_COND |
85 | result = 0; |
86 | GAI_MISC_WAIT (result, cntr, timeout, 1); |
87 | #else |
88 | if (timeout == NULL) |
89 | result = pthread_cond_wait (&cond, &__gai_requests_mutex); |
90 | else |
91 | { |
92 | /* We have to convert the relative timeout value into an |
93 | absolute time value with pthread_cond_timedwait expects. */ |
94 | struct timeval now; |
95 | struct timespec abstime; |
96 | |
97 | __gettimeofday (&now, NULL); |
98 | abstime.tv_nsec = timeout->tv_nsec + now.tv_usec * 1000; |
99 | abstime.tv_sec = timeout->tv_sec + now.tv_sec; |
100 | if (abstime.tv_nsec >= 1000000000) |
101 | { |
102 | abstime.tv_nsec -= 1000000000; |
103 | abstime.tv_sec += 1; |
104 | } |
105 | |
106 | result = pthread_cond_timedwait (&cond, &__gai_requests_mutex, |
107 | &abstime); |
108 | } |
109 | #endif |
110 | |
111 | /* Now remove the entry in the waiting list for all requests |
112 | which didn't terminate. */ |
113 | for (cnt = 0; cnt < ent; ++cnt) |
114 | if (list[cnt] != NULL && list[cnt]->__return == EAI_INPROGRESS |
115 | && requestlist[cnt] != NULL) |
116 | { |
117 | struct waitlist **listp = &requestlist[cnt]->waiting; |
118 | |
119 | /* There is the chance that we cannot find our entry anymore. |
120 | This could happen if the request terminated and restarted |
121 | again. */ |
122 | while (*listp != NULL && *listp != &waitlist[cnt]) |
123 | listp = &(*listp)->next; |
124 | |
125 | if (*listp != NULL) |
126 | *listp = (*listp)->next; |
127 | } |
128 | |
129 | /* Now it's time to restore the cancelation state. */ |
130 | pthread_setcancelstate (oldstate, NULL); |
131 | |
132 | #ifndef DONT_NEED_GAI_MISC_COND |
133 | /* Release the conditional variable. */ |
134 | if (pthread_cond_destroy (&cond) != 0) |
135 | /* This must never happen. */ |
136 | abort (); |
137 | #endif |
138 | |
139 | if (result != 0) |
140 | { |
141 | /* An error occurred. Possibly it's EINTR. We have to translate |
142 | the timeout error report of `pthread_cond_timedwait' to the |
143 | form expected from `gai_suspend'. */ |
144 | if (__builtin_expect (result, ETIMEDOUT) == ETIMEDOUT) |
145 | result = EAI_AGAIN; |
146 | else if (result == EINTR) |
147 | result = EAI_INTR; |
148 | else |
149 | result = EAI_SYSTEM; |
150 | } |
151 | } |
152 | |
153 | /* Release the mutex. */ |
154 | pthread_mutex_unlock (&__gai_requests_mutex); |
155 | |
156 | return result; |
157 | } |
158 | |