From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
To: Florian Weimer <fweimer@redhat.com>, libc-alpha@sourceware.org
Subject: Re: [PATCH 1/3] support: Add support_wait_for_thread_exit
Date: Thu, 19 Aug 2021 13:12:51 -0300 [thread overview]
Message-ID: <5c0a15c2-8447-86fe-73f2-8f27147423cc@linaro.org> (raw)
In-Reply-To: <3c76c76c7f0a25bfb9581a43693ebab13b50043d.1629208174.git.fweimer@redhat.com>
On 17/08/2021 10:50, Florian Weimer via Libc-alpha wrote:
> ---
> support/support.h | 4 ++
> support/support_wait_for_thread_exit.c | 67 ++++++++++++++++++++++++++
Missing the Makefile entry for support_wait_for_thread_exit
> 2 files changed, 71 insertions(+)
> create mode 100644 support/support_wait_for_thread_exit.c
>
> diff --git a/support/support.h b/support/support.h
> index 834dba9097..a5978b939a 100644
> --- a/support/support.h
> +++ b/support/support.h
> @@ -174,6 +174,10 @@ timer_t support_create_timer (uint64_t sec, long int nsec, bool repeat,
> /* Disable the timer TIMER. */
> void support_delete_timer (timer_t timer);
>
> +/* Wait until all threads except the current thread have exited (as
> + far as the kernel is concerned). */
> +void support_wait_for_thread_exit (void);
> +
> struct support_stack
> {
> void *stack;
Ok.
> diff --git a/support/support_wait_for_thread_exit.c b/support/support_wait_for_thread_exit.c
> new file mode 100644
> index 0000000000..808901f746
> --- /dev/null
> +++ b/support/support_wait_for_thread_exit.c
> @@ -0,0 +1,67 @@
> +/* Wait until all threads except the current thread has exited.
> + Copyright (C) 2021 Free Software Foundation, Inc.
> + This file is part of the GNU C Library.
> +
> + The GNU C Library is free software; you can redistribute it and/or
> + 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.
> +
> + 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
> + Lesser General Public License for more details.
> +
> + You should have received a copy of the GNU Lesser General Public
> + License along with the GNU C Library; if not, see
> + <https://www.gnu.org/licenses/>. */
> +
> +#include <dirent.h>
> +#include <errno.h>
> +#include <string.h>
> +#include <support/check.h>
> +#include <support/support.h>
> +#include <unistd.h>
> +
> +void
> +support_wait_for_thread_exit (void)
> +{
> + DIR *proc_self_task = opendir ("/proc/self/task");
> + if (proc_self_task == NULL)
> + /* Use a large timeout because we cannot verify that the thread
> + has exited. */
> + usleep (5 * 1000 * 1000);
> + else
> + {
> + int count;
> +
> + wait_again:
> + count = 0;
> + rewinddir (proc_self_task);
> + while (true)
> + {
> + errno = 0;
> + struct dirent *e = readdir (proc_self_task);
> + if (e == NULL && errno != 0)
> + FAIL_EXIT1 ("readdir: %m");
> + if (e == NULL)
> + {
> + /* Only the main thread remains. Testing may continue. */
> + TEST_VERIFY (count >= 1);
> + closedir (proc_self_task);
> + return;
> + }
> +
> + if (strcmp (e->d_name, ".") == 0 || strcmp (e->d_name, "..") == 0)
> + continue;
> +
> + ++count;
> + if (count > 1)
> + {
> + /* Small timeout to give the thread a chance to exit. */
> + usleep (50 * 1000);
> + goto wait_again;
> + }
> + }
> + }
> +}
>
I am not very found of interfaces that poll kernel resources using timeouts,
but I don't think we have a better Linux interface to handle this.
next prev parent reply other threads:[~2021-08-19 16:12 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-08-17 13:50 [PATCH 0/3] Fix ESRCH issues in pthread_cancel, pthread_kill Florian Weimer
2021-08-17 13:50 ` [PATCH 1/3] support: Add support_wait_for_thread_exit Florian Weimer
2021-08-19 16:12 ` Adhemerval Zanella [this message]
2021-08-19 16:22 ` Florian Weimer
2021-08-17 13:51 ` [PATCH 2/3] nptl: pthread_kill, pthread_cancel should fail after exit (bug 19193) Florian Weimer
2021-08-17 13:51 ` [PATCH 3/3] nptl: Fix race between pthread_kill and thread exit (bug 12889) Florian Weimer
2021-08-19 16:12 ` Adhemerval Zanella
2021-08-19 16:37 ` Florian Weimer
2021-08-19 16:49 ` Adhemerval Zanella
2021-08-20 16:24 ` Florian Weimer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5c0a15c2-8447-86fe-73f2-8f27147423cc@linaro.org \
--to=adhemerval.zanella@linaro.org \
--cc=fweimer@redhat.com \
--cc=libc-alpha@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).