diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/gtest-port.cc | 30 |
1 files changed, 26 insertions, 4 deletions
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 <stdlib.h> #include <stdio.h> #include <string.h> +#include <fstream> #if GTEST_OS_WINDOWS # include <windows.h> @@ -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 <typename T> +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<int>(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 |