0b26f7
commit addc9d62d61eea790a35328cbfce53333a07bd3e
0b26f7
Author: Florian Weimer <fweimer@redhat.com>
0b26f7
Date:   Mon Aug 30 13:43:56 2021 +0200
0b26f7
0b26f7
    support: Add support_wait_for_thread_exit
0b26f7
    
0b26f7
    (cherry picked from commit 032d74eaf6179100048a5bf0ce942e97dc8b9a60)
0b26f7
0b26f7
diff --git a/support/Makefile b/support/Makefile
0b26f7
index a462781718426d35..ef2b1a980a407f8f 100644
0b26f7
--- a/support/Makefile
0b26f7
+++ b/support/Makefile
0b26f7
@@ -82,9 +82,10 @@ libsupport-routines = \
0b26f7
   support_test_compare_blob \
0b26f7
   support_test_compare_failure \
0b26f7
   support_test_compare_string \
0b26f7
-  support_write_file_string \
0b26f7
   support_test_main \
0b26f7
   support_test_verify_impl \
0b26f7
+  support_wait_for_thread_exit \
0b26f7
+  support_write_file_string \
0b26f7
   temp_file \
0b26f7
   timespec \
0b26f7
   timespec-time64 \
0b26f7
diff --git a/support/support.h b/support/support.h
0b26f7
index 834dba909770a992..a5978b939af2fb41 100644
0b26f7
--- a/support/support.h
0b26f7
+++ b/support/support.h
0b26f7
@@ -174,6 +174,10 @@ timer_t support_create_timer (uint64_t sec, long int nsec, bool repeat,
0b26f7
 /* Disable the timer TIMER.  */
0b26f7
 void support_delete_timer (timer_t timer);
0b26f7
 
0b26f7
+/* Wait until all threads except the current thread have exited (as
0b26f7
+   far as the kernel is concerned).  */
0b26f7
+void support_wait_for_thread_exit (void);
0b26f7
+
0b26f7
 struct support_stack
0b26f7
 {
0b26f7
   void *stack;
0b26f7
diff --git a/support/support_wait_for_thread_exit.c b/support/support_wait_for_thread_exit.c
0b26f7
new file mode 100644
0b26f7
index 0000000000000000..658a81381006ea62
0b26f7
--- /dev/null
0b26f7
+++ b/support/support_wait_for_thread_exit.c
0b26f7
@@ -0,0 +1,72 @@
0b26f7
+/* Wait until all threads except the current thread has exited.
0b26f7
+   Copyright (C) 2021 Free Software Foundation, Inc.
0b26f7
+   This file is part of the GNU C Library.
0b26f7
+
0b26f7
+   The GNU C Library is free software; you can redistribute it and/or
0b26f7
+   modify it under the terms of the GNU Lesser General Public
0b26f7
+   License as published by the Free Software Foundation; either
0b26f7
+   version 2.1 of the License, or (at your option) any later version.
0b26f7
+
0b26f7
+   The GNU C Library is distributed in the hope that it will be useful,
0b26f7
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
0b26f7
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0b26f7
+   Lesser General Public License for more details.
0b26f7
+
0b26f7
+   You should have received a copy of the GNU Lesser General Public
0b26f7
+   License along with the GNU C Library; if not, see
0b26f7
+   <https://www.gnu.org/licenses/>.  */
0b26f7
+
0b26f7
+#include <dirent.h>
0b26f7
+#include <errno.h>
0b26f7
+#include <string.h>
0b26f7
+#include <support/check.h>
0b26f7
+#include <support/support.h>
0b26f7
+#include <unistd.h>
0b26f7
+
0b26f7
+void
0b26f7
+support_wait_for_thread_exit (void)
0b26f7
+{
0b26f7
+#ifdef __linux__
0b26f7
+  DIR *proc_self_task = opendir ("/proc/self/task");
0b26f7
+  TEST_VERIFY_EXIT (proc_self_task != NULL);
0b26f7
+
0b26f7
+  while (true)
0b26f7
+    {
0b26f7
+      errno = 0;
0b26f7
+      struct dirent *e = readdir (proc_self_task);
0b26f7
+      if (e == NULL && errno != 0)
0b26f7
+        FAIL_EXIT1 ("readdir: %m");
0b26f7
+      if (e == NULL)
0b26f7
+        {
0b26f7
+          /* Only the main thread remains.  Testing may continue.  */
0b26f7
+          closedir (proc_self_task);
0b26f7
+          return;
0b26f7
+        }
0b26f7
+
0b26f7
+      if (strcmp (e->d_name, ".") == 0 || strcmp (e->d_name, "..") == 0)
0b26f7
+        continue;
0b26f7
+
0b26f7
+      int task_tid = atoi (e->d_name);
0b26f7
+      if (task_tid <= 0)
0b26f7
+        FAIL_EXIT1 ("Invalid /proc/self/task entry: %s", e->d_name);
0b26f7
+
0b26f7
+      if (task_tid == gettid ())
0b26f7
+        /* The current thread.  Keep scanning for other
0b26f7
+           threads.  */
0b26f7
+        continue;
0b26f7
+
0b26f7
+      /* task_tid does not refer to this thread here, i.e., there is
0b26f7
+         another running thread.  */
0b26f7
+
0b26f7
+      /* Small timeout to give the thread a chance to exit.  */
0b26f7
+      usleep (50 * 1000);
0b26f7
+
0b26f7
+      /* Start scanning the directory from the start.  */
0b26f7
+      rewinddir (proc_self_task);
0b26f7
+    }
0b26f7
+#else
0b26f7
+  /* Use a large timeout because we cannot verify that the thread has
0b26f7
+     exited.  */
0b26f7
+  usleep (5 * 1000 * 1000);
0b26f7
+#endif
0b26f7
+}