[1/3] support: Add support_wait_for_thread_exit

Message ID 3c76c76c7f0a25bfb9581a43693ebab13b50043d.1629208174.git.fweimer@redhat.com
State Superseded
Headers
Series Fix ESRCH issues in pthread_cancel, pthread_kill |

Checks

Context Check Description
dj/TryBot-apply_patch success Patch applied to master at the time it was sent

Commit Message

Florian Weimer Aug. 17, 2021, 1:50 p.m. UTC
  ---
 support/support.h                      |  4 ++
 support/support_wait_for_thread_exit.c | 67 ++++++++++++++++++++++++++
 2 files changed, 71 insertions(+)
 create mode 100644 support/support_wait_for_thread_exit.c
  

Comments

Adhemerval Zanella Netto Aug. 19, 2021, 4:12 p.m. UTC | #1
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.
  
Florian Weimer Aug. 19, 2021, 4:22 p.m. UTC | #2
* Adhemerval Zanella:

> 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

Huh, right.  I must have put that into one of the other patches.  Will
reshuffle.

>> +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.

I think we could use pidfd_open and poll once we have a wrapper for
pidfd_open.  Not sure if it's possible to get a pollable descriptor via
/proc.

Thanks,
Florian
  

Patch

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;
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;
+            }
+        }
+    }
+}