Merge "Implement GetThreadCount for Linux." am: 0374ec7859
am: ed8c307213  -s ours

* commit 'ed8c307213d2a88e4e256bdb3698f082e9adc7a2':
  Implement GetThreadCount for Linux.
diff --git a/README.version b/README.version
new file mode 100644
index 0000000..a94c35b
--- /dev/null
+++ b/README.version
@@ -0,0 +1,4 @@
+URL: https://googletest.googlecode.com/files/gtest-1.7.0.zip
+Version: 1.7.0
+BugComponent: 14890
+AllAdvisories: yes
diff --git a/src/gtest-port.cc b/src/gtest-port.cc
index 4847ddc..28fa09a 100644
--- a/src/gtest-port.cc
+++ b/src/gtest-port.cc
@@ -36,7 +36,6 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
-#include <fstream>
 
 #if GTEST_OS_WINDOWS_MOBILE
 # include <windows.h>  // For TerminateProcess()
@@ -84,31 +83,10 @@
 const int kStdErrFileno = STDERR_FILENO;
 #endif  // _MSC_VER
 
-#if GTEST_OS_LINUX
+#if GTEST_OS_MAC
 
-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;
@@ -154,7 +132,7 @@
   return 0;
 }
 
-#endif  // GTEST_OS_LINUX
+#endif  // GTEST_OS_MAC
 
 #if GTEST_USES_POSIX_RE
 
diff --git a/test/gtest-port_test.cc b/test/gtest-port_test.cc
index 9240996..43f1f20 100644
--- a/test/gtest-port_test.cc
+++ b/test/gtest-port_test.cc
@@ -304,51 +304,58 @@
   EXPECT_EQ("unknown file", FormatCompilerIndependentFileLocation(NULL, -1));
 }
 
-#if GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_QNX
+#if GTEST_OS_MAC || GTEST_OS_QNX
 void* ThreadFunc(void* data) {
-  internal::Mutex* mutex = static_cast<internal::Mutex*>(data);
-  mutex->Lock();
-  mutex->Unlock();
+  pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data);
+  pthread_mutex_lock(mutex);
+  pthread_mutex_unlock(mutex);
   return NULL;
 }
 
 TEST(GetThreadCountTest, ReturnsCorrectValue) {
-  const size_t starting_count = GetThreadCount();
+  EXPECT_EQ(1U, GetThreadCount());
+  pthread_mutex_t mutex;
+  pthread_attr_t  attr;
   pthread_t       thread_id;
 
-  internal::Mutex mutex;
-  {
-    internal::MutexLock lock(&mutex);
-    pthread_attr_t  attr;
-    ASSERT_EQ(0, pthread_attr_init(&attr));
-    ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
+  // TODO(vladl@google.com): turn mutex into internal::Mutex for automatic
+  // destruction.
+  pthread_mutex_init(&mutex, NULL);
+  pthread_mutex_lock(&mutex);
+  ASSERT_EQ(0, pthread_attr_init(&attr));
+  ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
 
-    const int status = pthread_create(&thread_id, &attr, &ThreadFunc, &mutex);
-    ASSERT_EQ(0, pthread_attr_destroy(&attr));
-    ASSERT_EQ(0, status);
-    EXPECT_EQ(starting_count + 1, GetThreadCount());
-  }
+  const int status = pthread_create(&thread_id, &attr, &ThreadFunc, &mutex);
+  ASSERT_EQ(0, pthread_attr_destroy(&attr));
+  ASSERT_EQ(0, status);
+  EXPECT_EQ(2U, GetThreadCount());
+  pthread_mutex_unlock(&mutex);
 
   void* dummy;
   ASSERT_EQ(0, pthread_join(thread_id, &dummy));
 
-  // The OS may not immediately report the updated thread count after
+# if GTEST_OS_MAC
+
+  // MacOS X may not immediately report the updated thread count after
   // joining a thread, causing flakiness in this test. To counter that, we
   // wait for up to .5 seconds for the OS to report the correct value.
   for (int i = 0; i < 5; ++i) {
-    if (GetThreadCount() == starting_count)
+    if (GetThreadCount() == 1)
       break;
 
     SleepMilliseconds(100);
   }
 
-  EXPECT_EQ(starting_count, GetThreadCount());
+# endif  // GTEST_OS_MAC
+
+  EXPECT_EQ(1U, GetThreadCount());
+  pthread_mutex_destroy(&mutex);
 }
 #else
 TEST(GetThreadCountTest, ReturnsZeroWhenUnableToCountThreads) {
   EXPECT_EQ(0U, GetThreadCount());
 }
-#endif  // GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_QNX
+#endif  // GTEST_OS_MAC || GTEST_OS_QNX
 
 TEST(GtestCheckDeathTest, DiesWithCorrectOutputOnFailure) {
   const bool a_false_condition = false;