1 /*
2 * Copyright (C) 2015 Cyril Hrubis <chrubis@suse.cz>
3 *
4 * Licensed under the GNU GPLv2 or later.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program 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
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #ifndef FUTEX_UTILS_H__
21 #define FUTEX_UTILS_H__
22
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 /*
27 * Wait for nr_threads to be sleeping
28 */
wait_for_threads(unsigned int nr_threads)29 static inline int wait_for_threads(unsigned int nr_threads)
30 {
31 char thread_state, name[1024];
32 DIR *dir;
33 struct dirent *dent;
34 unsigned int cnt = 0;
35
36 snprintf(name, sizeof(name), "/proc/%i/task/", getpid());
37
38 dir = SAFE_OPENDIR(name);
39
40 while ((dent = SAFE_READDIR(dir))) {
41 /* skip ".", ".." and the main thread */
42 if (atoi(dent->d_name) == getpid() || atoi(dent->d_name) == 0)
43 continue;
44
45 snprintf(name, sizeof(name), "/proc/%i/task/%s/stat",
46 getpid(), dent->d_name);
47
48 SAFE_FILE_SCANF(name, "%*i %*s %c", &thread_state);
49
50 if (thread_state != 'S') {
51 tst_res(TINFO, "Thread %s not sleeping yet", dent->d_name);
52 SAFE_CLOSEDIR(dir);
53 return 1;
54 }
55 cnt++;
56 }
57
58 SAFE_CLOSEDIR(dir);
59
60 if (cnt != nr_threads) {
61 tst_res(TINFO, "%u threads sleeping, expected %u", cnt,
62 nr_threads);
63 }
64
65 return 0;
66 }
67
68 #endif /* FUTEX_UTILS_H__ */
69