Aarch64: Add memcpy for qualcomm's oryon-1 core
[glibc.git] / support / support_wait_for_thread_exit.c
blob449f1fc598a6eb1dea4c308a4bef47a6787d0807
1 /* Wait until all threads except the current thread has exited.
2 Copyright (C) 2021-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <dirent.h>
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <support/check.h>
24 #include <support/support.h>
25 #include <unistd.h>
27 void
28 support_wait_for_thread_exit (void)
30 #ifdef __linux__
31 DIR *proc_self_task = opendir ("/proc/self/task");
32 TEST_VERIFY_EXIT (proc_self_task != NULL);
34 while (true)
36 errno = 0;
37 struct dirent *e = readdir (proc_self_task);
38 if (e == NULL && errno != 0)
39 FAIL_EXIT1 ("readdir: %m");
40 if (e == NULL)
42 /* Only the main thread remains. Testing may continue. */
43 closedir (proc_self_task);
44 return;
47 /* In some kernels, "0" entries denote a thread that has just
48 exited. */
49 if (strcmp (e->d_name, ".") == 0 || strcmp (e->d_name, "..") == 0
50 || strcmp (e->d_name, "0") == 0)
51 continue;
53 int task_tid = atoi (e->d_name);
54 if (task_tid <= 0)
55 FAIL_EXIT1 ("Invalid /proc/self/task entry: %s", e->d_name);
57 if (task_tid == gettid ())
58 /* The current thread. Keep scanning for other
59 threads. */
60 continue;
62 /* task_tid does not refer to this thread here, i.e., there is
63 another running thread. */
65 /* Small timeout to give the thread a chance to exit. */
66 usleep (50 * 1000);
68 /* Start scanning the directory from the start. */
69 rewinddir (proc_self_task);
71 #else
72 /* Use a large timeout because we cannot verify that the thread has
73 exited. */
74 usleep (5 * 1000 * 1000);
75 #endif