Implement GetThreadCount for Linux.

This commit is contained in:
kosak
2015-07-17 22:53:00 +00:00
parent 4f8dc917eb
commit 060b7452ec
2 changed files with 46 additions and 31 deletions

View File

@@ -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