[PATCH 1/3] support: Add support_wait_for_thread_exit
Adhemerval Zanella
adhemerval.zanella@linaro.org
Thu Aug 19 16:12:51 GMT 2021
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.
More information about the Libc-alpha
mailing list