summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorkosak@google.com <kosak@google.com@861a406c-534a-0410-8894-cb66d6ee9925>2015-07-17 22:53:00 +0000
committerkosak@google.com <kosak@google.com@861a406c-534a-0410-8894-cb66d6ee9925>2015-07-17 22:53:00 +0000
commit9ee09fed2fcd57093fc83dc8e53301270e7227c9 (patch)
tree2ea4cec130f05cc618d89d056304a7d15419318c /src
parent9e1d563866472949308083821242032a0891b88e (diff)
downloadgoogletest-9ee09fed2fcd57093fc83dc8e53301270e7227c9.tar.gz
Implement GetThreadCount for Linux.
git-svn-id: http://googletest.googlecode.com/svn/trunk@726 861a406c-534a-0410-8894-cb66d6ee9925
Diffstat (limited to 'src')
-rw-r--r--src/gtest-port.cc30
1 files changed, 26 insertions, 4 deletions
diff --git a/src/gtest-port.cc b/src/gtest-port.cc
index 448a807..19fa028 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