1 /* Copyright (C) 2001-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
27 /* The available function names differ outside of libc. (In libc, we
28 need to use hidden aliases to avoid the PLT.) */
29 #define __pthread_attr_init pthread_attr_init
30 #define __pthread_attr_setdetachstate pthread_attr_setdetachstate
31 #define __pthread_cond_signal pthread_cond_signal
32 #define __pthread_cond_timedwait pthread_cond_timedwait
33 #define __pthread_create pthread_create
34 #define __pthread_exit pthread_exit
37 #ifndef gai_create_helper_thread
38 # define gai_create_helper_thread __gai_create_helper_thread
41 __gai_create_helper_thread (pthread_t
*threadp
, void *(*tf
) (void *),
46 /* Make sure the thread is created detached. */
47 __pthread_attr_init (&attr
);
48 __pthread_attr_setdetachstate (&attr
, PTHREAD_CREATE_DETACHED
);
50 int ret
= __pthread_create (threadp
, &attr
, tf
, arg
);
52 (void) __pthread_attr_destroy (&attr
);
58 /* Pool of request list entries. */
59 static struct requestlist
**pool
;
61 /* Number of total and allocated pool entries. */
62 static size_t pool_max_size
;
63 static size_t pool_size
;
65 /* We implement a two dimensional array but allocate each row separately.
66 The macro below determines how many entries should be used per row.
67 It should better be a power of two. */
68 #define ENTRIES_PER_ROW 32
70 /* How many rows we allocate at once. */
73 /* List of available entries. */
74 static struct requestlist
*freelist
;
76 /* Structure list of all currently processed requests. */
77 static struct requestlist
*requests
;
78 static struct requestlist
*requests_tail
;
80 /* Number of threads currently running. */
83 /* Number of threads waiting for work to arrive. */
84 static int idle_thread_count
;
87 /* These are the values used for optimization. We will probably
88 create a function to set these values. */
89 static struct gaiinit optim
=
91 20, /* int gai_threads; Maximal number of threads. */
92 64, /* int gai_num; Number of expected simultaneous requests. */
102 /* Since the list is global we need a mutex protecting it. */
103 pthread_mutex_t __gai_requests_mutex
= PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
;
105 /* When you add a request to the list and there are idle threads present,
106 you signal this condition variable. When a thread finishes work, it waits
107 on this condition variable for a time before it actually exits. */
108 pthread_cond_t __gai_new_request_notification
= PTHREAD_COND_INITIALIZER
;
111 /* Functions to handle request list pool. */
112 static struct requestlist
*
115 struct requestlist
*result
;
117 if (freelist
== NULL
)
119 struct requestlist
*new_row
;
122 if (pool_size
+ 1 >= pool_max_size
)
124 size_t new_max_size
= pool_max_size
+ ROWS_STEP
;
125 struct requestlist
**new_tab
;
127 new_tab
= (struct requestlist
**)
128 realloc (pool
, new_max_size
* sizeof (struct requestlist
*));
133 pool_max_size
= new_max_size
;
137 /* Allocate the new row. */
138 cnt
= pool_size
== 0 ? optim
.gai_num
: ENTRIES_PER_ROW
;
139 new_row
= (struct requestlist
*) calloc (cnt
,
140 sizeof (struct requestlist
));
144 pool
[pool_size
++] = new_row
;
146 /* Put all the new entries in the freelist. */
149 new_row
->next
= freelist
;
150 freelist
= new_row
++;
156 freelist
= freelist
->next
;
163 __gai_find_request (const struct gaicb
*gaicbp
)
165 struct requestlist
*runp
;
169 if (runp
->gaicbp
== gaicbp
)
179 __gai_remove_request (struct gaicb
*gaicbp
)
181 struct requestlist
*runp
;
182 struct requestlist
*lastp
;
187 if (runp
->gaicbp
== gaicbp
)
198 if (runp
->running
!= 0)
199 /* Currently handled. */
202 /* Dequeue the request. */
204 requests
= runp
->next
;
206 lastp
->next
= runp
->next
;
207 if (runp
== requests_tail
)
208 requests_tail
= lastp
;
214 /* The thread handler. */
215 static void *handle_requests (void *arg
);
218 /* The main function of the async I/O handling. It enqueues requests
219 and if necessary starts and handles threads. */
221 __gai_enqueue_request (struct gaicb
*gaicbp
)
223 struct requestlist
*newp
;
224 struct requestlist
*lastp
;
227 __pthread_mutex_lock (&__gai_requests_mutex
);
229 /* Get a new element for the waiting list. */
233 __pthread_mutex_unlock (&__gai_requests_mutex
);
234 __set_errno (EAGAIN
);
238 newp
->gaicbp
= gaicbp
;
239 newp
->waiting
= NULL
;
242 lastp
= requests_tail
;
243 if (requests_tail
== NULL
)
244 requests
= requests_tail
= newp
;
247 requests_tail
->next
= newp
;
248 requests_tail
= newp
;
251 gaicbp
->__return
= EAI_INPROGRESS
;
253 /* See if we need to and are able to create a thread. */
254 if (nthreads
< optim
.gai_threads
&& idle_thread_count
== 0)
260 /* Now try to start a thread. */
261 if (gai_create_helper_thread (&thid
, handle_requests
, newp
) == 0)
262 /* We managed to enqueue the request. All errors which can
263 happen now can be recognized by calls to `gai_error'. */
269 /* We cannot create a thread in the moment and there is
270 also no thread running. This is a problem. `errno' is
271 set to EAGAIN if this is only a temporary problem. */
272 assert (requests
== newp
|| lastp
->next
== newp
);
277 requests_tail
= lastp
;
279 newp
->next
= freelist
;
285 /* We are not handling the request after all. */
290 /* Enqueue the request in the request queue. */
293 /* If there is a thread waiting for work, then let it know that we
294 have just given it something to do. */
295 if (idle_thread_count
> 0)
296 __pthread_cond_signal (&__gai_new_request_notification
);
299 /* Release the mutex. */
300 __pthread_mutex_unlock (&__gai_requests_mutex
);
307 __attribute__ ((noreturn
))
308 handle_requests (void *arg
)
310 struct requestlist
*runp
= (struct requestlist
*) arg
;
314 /* If runp is NULL, then we were created to service the work queue
315 in general, not to handle any particular request. In that case we
316 skip the "do work" stuff on the first pass, and go directly to the
317 "get work off the work queue" part of this loop, which is near the
320 __pthread_mutex_lock (&__gai_requests_mutex
);
323 /* Make the request. */
324 struct gaicb
*req
= runp
->gaicbp
;
325 struct requestlist
*srchp
;
326 struct requestlist
*lastp
;
328 req
->__return
= getaddrinfo (req
->ar_name
, req
->ar_service
,
329 req
->ar_request
, &req
->ar_result
);
332 __pthread_mutex_lock (&__gai_requests_mutex
);
334 /* Send the signal to notify about finished processing of the
338 /* Now dequeue the current request. */
341 while (srchp
!= runp
)
346 assert (runp
->running
== 1);
348 if (requests_tail
== runp
)
349 requests_tail
= lastp
;
351 requests
= requests
->next
;
353 lastp
->next
= runp
->next
;
355 /* Free the old element. */
356 runp
->next
= freelist
;
361 while (runp
!= NULL
&& runp
->running
!= 0)
364 /* If the runlist is empty, then we sleep for a while, waiting for
365 something to arrive in it. */
366 if (runp
== NULL
&& optim
.gai_idle_time
>= 0)
369 struct timespec wakeup_time
;
372 __clock_gettime (CLOCK_REALTIME
, &now
);
373 wakeup_time
.tv_sec
= now
.tv_sec
+ optim
.gai_idle_time
;
374 wakeup_time
.tv_nsec
= now
.tv_nsec
;
375 if (wakeup_time
.tv_nsec
>= 1000000000)
377 wakeup_time
.tv_nsec
-= 1000000000;
378 ++wakeup_time
.tv_sec
;
380 __pthread_cond_timedwait (&__gai_new_request_notification
,
381 &__gai_requests_mutex
, &wakeup_time
);
384 while (runp
!= NULL
&& runp
->running
!= 0)
392 /* Mark the request as being worked on. */
393 assert (runp
->running
== 0);
396 /* If we have a request to process, and there's still another in
397 the run list, then we need to either wake up or create a new
398 thread to service the request that is still in the run list. */
399 if (requests
!= NULL
)
401 /* There are at least two items in the work queue to work on.
402 If there are other idle threads, then we should wake them
403 up for these other work elements; otherwise, we should try
404 to create a new thread. */
405 if (idle_thread_count
> 0)
406 __pthread_cond_signal (&__gai_new_request_notification
);
407 else if (nthreads
< optim
.gai_threads
)
412 /* Make sure the thread is created detached. */
413 __pthread_attr_init (&attr
);
414 __pthread_attr_setdetachstate (&attr
,
415 PTHREAD_CREATE_DETACHED
);
417 /* Now try to start a thread. If we fail, no big deal,
418 because we know that there is at least one thread (us)
419 that is working on lookup operations. */
420 if (__pthread_create (&thid
, &attr
, handle_requests
, NULL
)
427 /* Release the mutex. */
428 __pthread_mutex_unlock (&__gai_requests_mutex
);
430 while (runp
!= NULL
);
432 __pthread_exit (NULL
);
436 /* Free allocated resources. */
438 __attribute__ ((__destructor__
)) static
445 for (row
= 0; row
< pool_max_size
; ++row
)