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