2024-01-01 18:12:26 +00:00
|
|
|
/* Copyright (C) 2001-2024 Free Software Foundation, Inc.
|
2023-10-30 18:57:01 +00:00
|
|
|
Copyright The GNU Toolchain Authors.
|
2001-03-03 18:21:04 +00:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
2001-07-06 04:58:11 +00:00
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
2001-03-03 18:21:04 +00:00
|
|
|
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2001-07-06 04:58:11 +00:00
|
|
|
Lesser General Public License for more details.
|
2001-03-03 18:21:04 +00:00
|
|
|
|
2001-07-06 04:58:11 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2012-02-09 23:18:22 +00:00
|
|
|
License along with the GNU C Library; if not, see
|
Prefer https to http for gnu.org and fsf.org URLs
Also, change sources.redhat.com to sourceware.org.
This patch was automatically generated by running the following shell
script, which uses GNU sed, and which avoids modifying files imported
from upstream:
sed -ri '
s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g
s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g
' \
$(find $(git ls-files) -prune -type f \
! -name '*.po' \
! -name 'ChangeLog*' \
! -path COPYING ! -path COPYING.LIB \
! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \
! -path manual/texinfo.tex ! -path scripts/config.guess \
! -path scripts/config.sub ! -path scripts/install-sh \
! -path scripts/mkinstalldirs ! -path scripts/move-if-change \
! -path INSTALL ! -path locale/programs/charmap-kw.h \
! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \
! '(' -name configure \
-execdir test -f configure.ac -o -f configure.in ';' ')' \
! '(' -name preconfigure \
-execdir test -f preconfigure.ac ';' ')' \
-print)
and then by running 'make dist-prepare' to regenerate files built
from the altered files, and then executing the following to cleanup:
chmod a+x sysdeps/unix/sysv/linux/riscv/configure
# Omit irrelevant whitespace and comment-only changes,
# perhaps from a slightly-different Autoconf version.
git checkout -f \
sysdeps/csky/configure \
sysdeps/hppa/configure \
sysdeps/riscv/configure \
sysdeps/unix/sysv/linux/csky/configure
# Omit changes that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines
git checkout -f \
sysdeps/powerpc/powerpc64/ppc-mcount.S \
sysdeps/unix/sysv/linux/s390/s390-64/syscall.S
# Omit change that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline
git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 05:40:42 +00:00
|
|
|
<https://www.gnu.org/licenses/>. */
|
2001-03-03 18:21:04 +00:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
2006-08-03 08:17:20 +00:00
|
|
|
#include <gai_misc.h>
|
2001-03-03 18:21:04 +00:00
|
|
|
|
2021-07-02 09:45:00 +00:00
|
|
|
#if !PTHREAD_IN_LIBC
|
|
|
|
/* The available function names differ outside of libc. (In libc, we
|
|
|
|
need to use hidden aliases to avoid the PLT.) */
|
|
|
|
#define __pthread_attr_init pthread_attr_init
|
|
|
|
#define __pthread_attr_setdetachstate pthread_attr_setdetachstate
|
|
|
|
#define __pthread_cond_signal pthread_cond_signal
|
|
|
|
#define __pthread_cond_timedwait pthread_cond_timedwait
|
|
|
|
#define __pthread_create pthread_create
|
|
|
|
#define __pthread_exit pthread_exit
|
|
|
|
#endif
|
2001-03-03 18:21:04 +00:00
|
|
|
|
2006-08-03 08:17:20 +00:00
|
|
|
#ifndef gai_create_helper_thread
|
|
|
|
# define gai_create_helper_thread __gai_create_helper_thread
|
|
|
|
|
|
|
|
extern inline int
|
|
|
|
__gai_create_helper_thread (pthread_t *threadp, void *(*tf) (void *),
|
|
|
|
void *arg)
|
|
|
|
{
|
|
|
|
pthread_attr_t attr;
|
|
|
|
|
|
|
|
/* Make sure the thread is created detached. */
|
2021-07-02 09:45:00 +00:00
|
|
|
__pthread_attr_init (&attr);
|
|
|
|
__pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
|
2006-08-03 08:17:20 +00:00
|
|
|
|
2021-07-02 09:45:00 +00:00
|
|
|
int ret = __pthread_create (threadp, &attr, tf, arg);
|
2006-08-03 08:17:20 +00:00
|
|
|
|
2021-07-02 09:45:00 +00:00
|
|
|
(void) __pthread_attr_destroy (&attr);
|
2006-08-03 08:17:20 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2001-03-03 18:21:04 +00:00
|
|
|
/* Pool of request list entries. */
|
|
|
|
static struct requestlist **pool;
|
|
|
|
|
|
|
|
/* Number of total and allocated pool entries. */
|
|
|
|
static size_t pool_max_size;
|
|
|
|
static size_t pool_size;
|
|
|
|
|
|
|
|
/* We implement a two dimensional array but allocate each row separately.
|
|
|
|
The macro below determines how many entries should be used per row.
|
|
|
|
It should better be a power of two. */
|
|
|
|
#define ENTRIES_PER_ROW 32
|
|
|
|
|
|
|
|
/* How many rows we allocate at once. */
|
|
|
|
#define ROWS_STEP 8
|
|
|
|
|
|
|
|
/* List of available entries. */
|
|
|
|
static struct requestlist *freelist;
|
|
|
|
|
|
|
|
/* Structure list of all currently processed requests. */
|
|
|
|
static struct requestlist *requests;
|
|
|
|
static struct requestlist *requests_tail;
|
|
|
|
|
|
|
|
/* Number of threads currently running. */
|
|
|
|
static int nthreads;
|
|
|
|
|
|
|
|
/* Number of threads waiting for work to arrive. */
|
|
|
|
static int idle_thread_count;
|
|
|
|
|
|
|
|
|
|
|
|
/* These are the values used for optimization. We will probably
|
2023-05-20 13:37:47 +00:00
|
|
|
create a function to set these values. */
|
2001-03-03 18:21:04 +00:00
|
|
|
static struct gaiinit optim =
|
|
|
|
{
|
|
|
|
20, /* int gai_threads; Maximal number of threads. */
|
2023-05-20 13:37:47 +00:00
|
|
|
64, /* int gai_num; Number of expected simultaneous requests. */
|
2001-03-03 18:21:04 +00:00
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Since the list is global we need a mutex protecting it. */
|
|
|
|
pthread_mutex_t __gai_requests_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
|
|
|
|
|
|
|
|
/* When you add a request to the list and there are idle threads present,
|
|
|
|
you signal this condition variable. When a thread finishes work, it waits
|
|
|
|
on this condition variable for a time before it actually exits. */
|
|
|
|
pthread_cond_t __gai_new_request_notification = PTHREAD_COND_INITIALIZER;
|
|
|
|
|
|
|
|
|
|
|
|
/* Functions to handle request list pool. */
|
|
|
|
static struct requestlist *
|
|
|
|
get_elem (void)
|
|
|
|
{
|
|
|
|
struct requestlist *result;
|
|
|
|
|
|
|
|
if (freelist == NULL)
|
|
|
|
{
|
|
|
|
struct requestlist *new_row;
|
|
|
|
int cnt;
|
|
|
|
|
|
|
|
if (pool_size + 1 >= pool_max_size)
|
|
|
|
{
|
|
|
|
size_t new_max_size = pool_max_size + ROWS_STEP;
|
|
|
|
struct requestlist **new_tab;
|
|
|
|
|
|
|
|
new_tab = (struct requestlist **)
|
|
|
|
realloc (pool, new_max_size * sizeof (struct requestlist *));
|
|
|
|
|
|
|
|
if (new_tab == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
pool_max_size = new_max_size;
|
|
|
|
pool = new_tab;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate the new row. */
|
|
|
|
cnt = pool_size == 0 ? optim.gai_num : ENTRIES_PER_ROW;
|
|
|
|
new_row = (struct requestlist *) calloc (cnt,
|
|
|
|
sizeof (struct requestlist));
|
|
|
|
if (new_row == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
pool[pool_size++] = new_row;
|
|
|
|
|
|
|
|
/* Put all the new entries in the freelist. */
|
|
|
|
do
|
|
|
|
{
|
|
|
|
new_row->next = freelist;
|
|
|
|
freelist = new_row++;
|
|
|
|
}
|
|
|
|
while (--cnt > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
result = freelist;
|
|
|
|
freelist = freelist->next;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct requestlist *
|
|
|
|
__gai_find_request (const struct gaicb *gaicbp)
|
|
|
|
{
|
|
|
|
struct requestlist *runp;
|
|
|
|
|
|
|
|
runp = requests;
|
|
|
|
while (runp != NULL)
|
|
|
|
if (runp->gaicbp == gaicbp)
|
|
|
|
return runp;
|
|
|
|
else
|
|
|
|
runp = runp->next;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
__gai_remove_request (struct gaicb *gaicbp)
|
|
|
|
{
|
|
|
|
struct requestlist *runp;
|
|
|
|
struct requestlist *lastp;
|
|
|
|
|
|
|
|
runp = requests;
|
|
|
|
lastp = NULL;
|
|
|
|
while (runp != NULL)
|
|
|
|
if (runp->gaicbp == gaicbp)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lastp = runp;
|
|
|
|
runp = runp->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (runp == NULL)
|
|
|
|
/* Not known. */
|
|
|
|
return -1;
|
|
|
|
if (runp->running != 0)
|
|
|
|
/* Currently handled. */
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* Dequeue the request. */
|
|
|
|
if (lastp == NULL)
|
|
|
|
requests = runp->next;
|
|
|
|
else
|
|
|
|
lastp->next = runp->next;
|
|
|
|
if (runp == requests_tail)
|
|
|
|
requests_tail = lastp;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* The thread handler. */
|
|
|
|
static void *handle_requests (void *arg);
|
|
|
|
|
|
|
|
|
|
|
|
/* The main function of the async I/O handling. It enqueues requests
|
|
|
|
and if necessary starts and handles threads. */
|
|
|
|
struct requestlist *
|
|
|
|
__gai_enqueue_request (struct gaicb *gaicbp)
|
|
|
|
{
|
|
|
|
struct requestlist *newp;
|
|
|
|
struct requestlist *lastp;
|
|
|
|
|
|
|
|
/* Get the mutex. */
|
2021-07-02 09:45:00 +00:00
|
|
|
__pthread_mutex_lock (&__gai_requests_mutex);
|
2001-03-03 18:21:04 +00:00
|
|
|
|
|
|
|
/* Get a new element for the waiting list. */
|
|
|
|
newp = get_elem ();
|
|
|
|
if (newp == NULL)
|
|
|
|
{
|
2021-07-02 09:45:00 +00:00
|
|
|
__pthread_mutex_unlock (&__gai_requests_mutex);
|
2001-03-03 18:21:04 +00:00
|
|
|
__set_errno (EAGAIN);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
newp->running = 0;
|
|
|
|
newp->gaicbp = gaicbp;
|
|
|
|
newp->waiting = NULL;
|
|
|
|
newp->next = NULL;
|
|
|
|
|
|
|
|
lastp = requests_tail;
|
|
|
|
if (requests_tail == NULL)
|
|
|
|
requests = requests_tail = newp;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
requests_tail->next = newp;
|
|
|
|
requests_tail = newp;
|
|
|
|
}
|
|
|
|
|
|
|
|
gaicbp->__return = EAI_INPROGRESS;
|
|
|
|
|
|
|
|
/* See if we need to and are able to create a thread. */
|
|
|
|
if (nthreads < optim.gai_threads && idle_thread_count == 0)
|
|
|
|
{
|
|
|
|
pthread_t thid;
|
|
|
|
|
|
|
|
newp->running = 1;
|
|
|
|
|
|
|
|
/* Now try to start a thread. */
|
2006-08-03 08:17:20 +00:00
|
|
|
if (gai_create_helper_thread (&thid, handle_requests, newp) == 0)
|
2001-03-03 18:21:04 +00:00
|
|
|
/* We managed to enqueue the request. All errors which can
|
|
|
|
happen now can be recognized by calls to `gai_error'. */
|
|
|
|
++nthreads;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (nthreads == 0)
|
|
|
|
{
|
|
|
|
/* We cannot create a thread in the moment and there is
|
|
|
|
also no thread running. This is a problem. `errno' is
|
|
|
|
set to EAGAIN if this is only a temporary problem. */
|
2018-11-05 11:47:30 +00:00
|
|
|
assert (requests == newp || lastp->next == newp);
|
|
|
|
if (lastp != NULL)
|
|
|
|
lastp->next = NULL;
|
|
|
|
else
|
|
|
|
requests = NULL;
|
2001-03-03 18:21:04 +00:00
|
|
|
requests_tail = lastp;
|
|
|
|
|
|
|
|
newp->next = freelist;
|
|
|
|
freelist = newp;
|
|
|
|
|
|
|
|
newp = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
/* We are not handling the request after all. */
|
|
|
|
newp->running = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Enqueue the request in the request queue. */
|
|
|
|
if (newp != NULL)
|
|
|
|
{
|
|
|
|
/* If there is a thread waiting for work, then let it know that we
|
|
|
|
have just given it something to do. */
|
|
|
|
if (idle_thread_count > 0)
|
2021-07-02 09:45:00 +00:00
|
|
|
__pthread_cond_signal (&__gai_new_request_notification);
|
2001-03-03 18:21:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Release the mutex. */
|
2021-07-02 09:45:00 +00:00
|
|
|
__pthread_mutex_unlock (&__gai_requests_mutex);
|
2001-03-03 18:21:04 +00:00
|
|
|
|
|
|
|
return newp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void *
|
2001-04-21 18:41:05 +00:00
|
|
|
__attribute__ ((noreturn))
|
2001-03-03 18:21:04 +00:00
|
|
|
handle_requests (void *arg)
|
|
|
|
{
|
|
|
|
struct requestlist *runp = (struct requestlist *) arg;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* If runp is NULL, then we were created to service the work queue
|
|
|
|
in general, not to handle any particular request. In that case we
|
|
|
|
skip the "do work" stuff on the first pass, and go directly to the
|
|
|
|
"get work off the work queue" part of this loop, which is near the
|
|
|
|
end. */
|
|
|
|
if (runp == NULL)
|
2021-07-02 09:45:00 +00:00
|
|
|
__pthread_mutex_lock (&__gai_requests_mutex);
|
2001-03-03 18:21:04 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Make the request. */
|
|
|
|
struct gaicb *req = runp->gaicbp;
|
|
|
|
struct requestlist *srchp;
|
|
|
|
struct requestlist *lastp;
|
|
|
|
|
|
|
|
req->__return = getaddrinfo (req->ar_name, req->ar_service,
|
|
|
|
req->ar_request, &req->ar_result);
|
|
|
|
|
|
|
|
/* Get the mutex. */
|
2021-07-02 09:45:00 +00:00
|
|
|
__pthread_mutex_lock (&__gai_requests_mutex);
|
2001-03-03 18:21:04 +00:00
|
|
|
|
|
|
|
/* Send the signal to notify about finished processing of the
|
|
|
|
request. */
|
|
|
|
__gai_notify (runp);
|
|
|
|
|
|
|
|
/* Now dequeue the current request. */
|
|
|
|
lastp = NULL;
|
|
|
|
srchp = requests;
|
|
|
|
while (srchp != runp)
|
|
|
|
{
|
|
|
|
lastp = srchp;
|
|
|
|
srchp = srchp->next;
|
|
|
|
}
|
|
|
|
assert (runp->running == 1);
|
|
|
|
|
|
|
|
if (requests_tail == runp)
|
|
|
|
requests_tail = lastp;
|
|
|
|
if (lastp == NULL)
|
|
|
|
requests = requests->next;
|
|
|
|
else
|
|
|
|
lastp->next = runp->next;
|
|
|
|
|
|
|
|
/* Free the old element. */
|
|
|
|
runp->next = freelist;
|
|
|
|
freelist = runp;
|
|
|
|
}
|
|
|
|
|
|
|
|
runp = requests;
|
|
|
|
while (runp != NULL && runp->running != 0)
|
|
|
|
runp = runp->next;
|
|
|
|
|
|
|
|
/* If the runlist is empty, then we sleep for a while, waiting for
|
|
|
|
something to arrive in it. */
|
|
|
|
if (runp == NULL && optim.gai_idle_time >= 0)
|
|
|
|
{
|
Change most internal uses of __gettimeofday to __clock_gettime.
Since gettimeofday will shortly be implemented in terms of
clock_gettime on all platforms, internal code should use clock_gettime
directly; in addition to removing a layer of indirection, this will
allow us to remove the PLT-bypass gunk for gettimeofday. (We can't
quite do that yet, but it'll be coming later in this patch series.)
In many cases, the changed code does fewer conversions.
The changed code always assumes __clock_gettime (CLOCK_REALTIME)
cannot fail. Most of the call sites were assuming gettimeofday could
not fail, but a few places were checking for errors. POSIX says
clock_gettime can only fail if the clock constant is invalid or
unsupported, and CLOCK_REALTIME is the one and only clock constant
that's required to be supported. For consistency I grepped the entire
source tree for any other places that checked for errors from
__clock_gettime (CLOCK_REALTIME), found one, and changed it too.
(For the record, POSIX also says gettimeofday can never fail.)
(It would be nice if we could declare that GNU systems will always
support CLOCK_MONOTONIC as well as CLOCK_REALTIME; there are several
places where we are using CLOCK_REALTIME where _MONOTONIC would be
more appropriate, and/or trying to use _MONOTONIC and then falling
back to _REALTIME. But the Hurd doesn't support CLOCK_MONOTONIC yet,
and it looks like adding it would involve substantial changes to
gnumach's internals and API. Oh well.)
A few Hurd-specific files were changed to use __host_get_time instead
of __clock_gettime, as this seemed tidier. We also assume this cannot
fail. Skimming the code in gnumach leads me to believe the only way
it could fail is if __mach_host_self also failed, and our
Hurd-specific code consistently assumes that can't happen, so I'm
going with that.
With the exception of support/support_test_main.c, test cases are not
modified, mainly because I didn't want to have to figure out which
test cases were testing gettimeofday specifically.
The definition of GETTIME in sysdeps/generic/memusage.h had a typo and
was not reading tv_sec at all. I fixed this. It appears nobody has been
generating malloc traces on a machine that doesn't have a superseding
definition.
There are a whole bunch of places where the code could be simplified
by factoring out timespec subtraction and/or comparison logic, but I
want to keep this patch as mechanical as possible.
Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc64le-linux-gnu,
powerpc64-linux-gnu, powerpc-linux-gnu, and aarch64-linux-gnu.
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Reviewed-by: Lukasz Majewski <lukma@denx.de>
2019-08-17 00:38:22 +00:00
|
|
|
struct timespec now;
|
2001-03-03 18:21:04 +00:00
|
|
|
struct timespec wakeup_time;
|
|
|
|
|
|
|
|
++idle_thread_count;
|
Change most internal uses of __gettimeofday to __clock_gettime.
Since gettimeofday will shortly be implemented in terms of
clock_gettime on all platforms, internal code should use clock_gettime
directly; in addition to removing a layer of indirection, this will
allow us to remove the PLT-bypass gunk for gettimeofday. (We can't
quite do that yet, but it'll be coming later in this patch series.)
In many cases, the changed code does fewer conversions.
The changed code always assumes __clock_gettime (CLOCK_REALTIME)
cannot fail. Most of the call sites were assuming gettimeofday could
not fail, but a few places were checking for errors. POSIX says
clock_gettime can only fail if the clock constant is invalid or
unsupported, and CLOCK_REALTIME is the one and only clock constant
that's required to be supported. For consistency I grepped the entire
source tree for any other places that checked for errors from
__clock_gettime (CLOCK_REALTIME), found one, and changed it too.
(For the record, POSIX also says gettimeofday can never fail.)
(It would be nice if we could declare that GNU systems will always
support CLOCK_MONOTONIC as well as CLOCK_REALTIME; there are several
places where we are using CLOCK_REALTIME where _MONOTONIC would be
more appropriate, and/or trying to use _MONOTONIC and then falling
back to _REALTIME. But the Hurd doesn't support CLOCK_MONOTONIC yet,
and it looks like adding it would involve substantial changes to
gnumach's internals and API. Oh well.)
A few Hurd-specific files were changed to use __host_get_time instead
of __clock_gettime, as this seemed tidier. We also assume this cannot
fail. Skimming the code in gnumach leads me to believe the only way
it could fail is if __mach_host_self also failed, and our
Hurd-specific code consistently assumes that can't happen, so I'm
going with that.
With the exception of support/support_test_main.c, test cases are not
modified, mainly because I didn't want to have to figure out which
test cases were testing gettimeofday specifically.
The definition of GETTIME in sysdeps/generic/memusage.h had a typo and
was not reading tv_sec at all. I fixed this. It appears nobody has been
generating malloc traces on a machine that doesn't have a superseding
definition.
There are a whole bunch of places where the code could be simplified
by factoring out timespec subtraction and/or comparison logic, but I
want to keep this patch as mechanical as possible.
Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc64le-linux-gnu,
powerpc64-linux-gnu, powerpc-linux-gnu, and aarch64-linux-gnu.
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Reviewed-by: Lukasz Majewski <lukma@denx.de>
2019-08-17 00:38:22 +00:00
|
|
|
__clock_gettime (CLOCK_REALTIME, &now);
|
2001-03-03 18:21:04 +00:00
|
|
|
wakeup_time.tv_sec = now.tv_sec + optim.gai_idle_time;
|
Change most internal uses of __gettimeofday to __clock_gettime.
Since gettimeofday will shortly be implemented in terms of
clock_gettime on all platforms, internal code should use clock_gettime
directly; in addition to removing a layer of indirection, this will
allow us to remove the PLT-bypass gunk for gettimeofday. (We can't
quite do that yet, but it'll be coming later in this patch series.)
In many cases, the changed code does fewer conversions.
The changed code always assumes __clock_gettime (CLOCK_REALTIME)
cannot fail. Most of the call sites were assuming gettimeofday could
not fail, but a few places were checking for errors. POSIX says
clock_gettime can only fail if the clock constant is invalid or
unsupported, and CLOCK_REALTIME is the one and only clock constant
that's required to be supported. For consistency I grepped the entire
source tree for any other places that checked for errors from
__clock_gettime (CLOCK_REALTIME), found one, and changed it too.
(For the record, POSIX also says gettimeofday can never fail.)
(It would be nice if we could declare that GNU systems will always
support CLOCK_MONOTONIC as well as CLOCK_REALTIME; there are several
places where we are using CLOCK_REALTIME where _MONOTONIC would be
more appropriate, and/or trying to use _MONOTONIC and then falling
back to _REALTIME. But the Hurd doesn't support CLOCK_MONOTONIC yet,
and it looks like adding it would involve substantial changes to
gnumach's internals and API. Oh well.)
A few Hurd-specific files were changed to use __host_get_time instead
of __clock_gettime, as this seemed tidier. We also assume this cannot
fail. Skimming the code in gnumach leads me to believe the only way
it could fail is if __mach_host_self also failed, and our
Hurd-specific code consistently assumes that can't happen, so I'm
going with that.
With the exception of support/support_test_main.c, test cases are not
modified, mainly because I didn't want to have to figure out which
test cases were testing gettimeofday specifically.
The definition of GETTIME in sysdeps/generic/memusage.h had a typo and
was not reading tv_sec at all. I fixed this. It appears nobody has been
generating malloc traces on a machine that doesn't have a superseding
definition.
There are a whole bunch of places where the code could be simplified
by factoring out timespec subtraction and/or comparison logic, but I
want to keep this patch as mechanical as possible.
Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc64le-linux-gnu,
powerpc64-linux-gnu, powerpc-linux-gnu, and aarch64-linux-gnu.
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Reviewed-by: Lukasz Majewski <lukma@denx.de>
2019-08-17 00:38:22 +00:00
|
|
|
wakeup_time.tv_nsec = now.tv_nsec;
|
2012-03-08 10:49:43 +00:00
|
|
|
if (wakeup_time.tv_nsec >= 1000000000)
|
2001-03-03 18:21:04 +00:00
|
|
|
{
|
|
|
|
wakeup_time.tv_nsec -= 1000000000;
|
|
|
|
++wakeup_time.tv_sec;
|
|
|
|
}
|
2021-07-02 09:45:00 +00:00
|
|
|
__pthread_cond_timedwait (&__gai_new_request_notification,
|
|
|
|
&__gai_requests_mutex, &wakeup_time);
|
2001-03-03 18:21:04 +00:00
|
|
|
--idle_thread_count;
|
|
|
|
runp = requests;
|
|
|
|
while (runp != NULL && runp->running != 0)
|
|
|
|
runp = runp->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (runp == NULL)
|
|
|
|
--nthreads;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Mark the request as being worked on. */
|
|
|
|
assert (runp->running == 0);
|
|
|
|
runp->running = 1;
|
|
|
|
|
|
|
|
/* If we have a request to process, and there's still another in
|
|
|
|
the run list, then we need to either wake up or create a new
|
|
|
|
thread to service the request that is still in the run list. */
|
|
|
|
if (requests != NULL)
|
|
|
|
{
|
|
|
|
/* There are at least two items in the work queue to work on.
|
|
|
|
If there are other idle threads, then we should wake them
|
|
|
|
up for these other work elements; otherwise, we should try
|
|
|
|
to create a new thread. */
|
|
|
|
if (idle_thread_count > 0)
|
2021-07-02 09:45:00 +00:00
|
|
|
__pthread_cond_signal (&__gai_new_request_notification);
|
2001-03-03 18:21:04 +00:00
|
|
|
else if (nthreads < optim.gai_threads)
|
|
|
|
{
|
|
|
|
pthread_t thid;
|
|
|
|
pthread_attr_t attr;
|
|
|
|
|
|
|
|
/* Make sure the thread is created detached. */
|
2021-07-02 09:45:00 +00:00
|
|
|
__pthread_attr_init (&attr);
|
|
|
|
__pthread_attr_setdetachstate (&attr,
|
|
|
|
PTHREAD_CREATE_DETACHED);
|
2001-03-03 18:21:04 +00:00
|
|
|
|
|
|
|
/* Now try to start a thread. If we fail, no big deal,
|
|
|
|
because we know that there is at least one thread (us)
|
|
|
|
that is working on lookup operations. */
|
2021-07-02 09:45:00 +00:00
|
|
|
if (__pthread_create (&thid, &attr, handle_requests, NULL)
|
2001-03-03 18:21:04 +00:00
|
|
|
== 0)
|
|
|
|
++nthreads;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Release the mutex. */
|
2021-07-02 09:45:00 +00:00
|
|
|
__pthread_mutex_unlock (&__gai_requests_mutex);
|
2001-03-03 18:21:04 +00:00
|
|
|
}
|
|
|
|
while (runp != NULL);
|
|
|
|
|
2021-07-02 09:45:00 +00:00
|
|
|
__pthread_exit (NULL);
|
2001-03-03 18:21:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Free allocated resources. */
|
2022-12-27 21:11:42 +00:00
|
|
|
#if !PTHREAD_IN_LIBC
|
|
|
|
__attribute__ ((__destructor__)) static
|
|
|
|
#endif
|
|
|
|
void
|
|
|
|
__gai_freemem (void)
|
2001-03-03 18:21:04 +00:00
|
|
|
{
|
|
|
|
size_t row;
|
|
|
|
|
2023-10-30 18:57:01 +00:00
|
|
|
for (row = 0; row < pool_size; ++row)
|
2001-03-03 18:21:04 +00:00
|
|
|
free (pool[row]);
|
|
|
|
|
|
|
|
free (pool);
|
|
|
|
}
|