1 /* 2 * Copyright (C) 2015 Cyril Hrubis <chrubis (at) 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 /* 24 * Wait for nr_threads to be sleeping 25 */ 26 static int wait_for_threads(unsigned int nr_threads) 27 { 28 char thread_state, name[1024]; 29 DIR *dir; 30 struct dirent *dent; 31 unsigned int cnt = 0; 32 33 snprintf(name, sizeof(name), "/proc/%i/task/", getpid()); 34 35 dir = SAFE_OPENDIR(NULL, name); 36 37 while ((dent = SAFE_READDIR(NULL, dir))) { 38 /* skip ".", ".." and the main thread */ 39 if (atoi(dent->d_name) == getpid() || atoi(dent->d_name) == 0) 40 continue; 41 42 snprintf(name, sizeof(name), "/proc/%i/task/%s/stat", 43 getpid(), dent->d_name); 44 45 SAFE_FILE_SCANF(NULL, name, "%*i %*s %c", &thread_state); 46 47 if (thread_state != 'S') { 48 tst_resm(TINFO, "Thread %s not sleeping yet", dent->d_name); 49 SAFE_CLOSEDIR(NULL, dir); 50 return 1; 51 } 52 cnt++; 53 } 54 55 SAFE_CLOSEDIR(NULL, dir); 56 57 if (cnt != nr_threads) { 58 tst_resm(TINFO, "%u threads sleeping, expected %u", 59 cnt, nr_threads); 60 } 61 62 return 0; 63 } 64 65 #endif /* FUTEX_UTILS_H__ */ 66