From d286efd3d292d69c2a89ee0639b08ba4c0c81258 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Tue, 6 May 2014 06:45:06 -0700 Subject: [PATCH] Implement GetThreadCount on Linux. --- gtest/src/gtest-port.cc | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/gtest/src/gtest-port.cc b/gtest/src/gtest-port.cc index b860d481..7f063972 100644 --- a/gtest/src/gtest-port.cc +++ b/gtest/src/gtest-port.cc @@ -42,6 +42,7 @@ # include # include #else +# include # include #endif // GTEST_OS_WINDOWS_MOBILE @@ -98,6 +99,22 @@ size_t GetThreadCount() { } } +#elif GTEST_OS_LINUX + +// Returns the number of threads running in the process, or 0 to indicate that +// we cannot detect it. +size_t GetThreadCount() { + size_t thread_count = 0; + if (DIR *dir = opendir("/proc/self/task")) { + while (dirent *entry = readdir(dir)) { + if (entry->d_name[0] != '.') + ++thread_count; + } + closedir(dir); + } + return thread_count; +} + #else size_t GetThreadCount() {