From 060b7452ec0f3f0a7fa09914e4044349dc9990c6 Mon Sep 17 00:00:00 2001 From: kosak Date: Fri, 17 Jul 2015 22:53:00 +0000 Subject: Implement GetThreadCount for Linux. --- src/gtest-port.cc | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/gtest-port.cc b/src/gtest-port.cc index 448a807f..19fa0280 100644 --- a/src/gtest-port.cc +++ b/src/gtest-port.cc @@ -35,6 +35,7 @@ #include #include #include +#include #if GTEST_OS_WINDOWS # include @@ -83,10 +84,31 @@ const int kStdOutFileno = STDOUT_FILENO; const int kStdErrFileno = STDERR_FILENO; #endif // _MSC_VER -#if GTEST_OS_MAC +#if GTEST_OS_LINUX + +namespace { +template +T ReadProcFileField(const string& filename, int field) { + std::string dummy; + std::ifstream file(filename.c_str()); + while (field-- > 0) { + file >> dummy; + } + T output = 0; + file >> output; + return output; +} +} // namespace + +// Returns the number of active threads, or 0 when there is an error. +size_t GetThreadCount() { + const string filename = + (Message() << "/proc/" << getpid() << "/stat").GetString(); + return ReadProcFileField(filename, 19); +} + +#elif GTEST_OS_MAC -// Returns the number of threads running in the process, or 0 to indicate that -// we cannot detect it. size_t GetThreadCount() { const task_t task = mach_task_self(); mach_msg_type_number_t thread_count; @@ -132,7 +154,7 @@ size_t GetThreadCount() { return 0; } -#endif // GTEST_OS_MAC +#endif // GTEST_OS_LINUX #if GTEST_IS_THREADSAFE && GTEST_OS_WINDOWS -- cgit v1.2.3