Enable ThreadChecker in non-debug builds if DCHECK_ALWAYS_ON is
defined.

This brings thread checking to e.g. the *_rel trybots.

BUG=108227


Review URL: http://codereview.chromium.org/9020008

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@116350 0039d316-1c4b-4281-b951-d872f2087c98


CrOS-Libchrome-Original-Commit: 8c3881ab86792f0b7cfa1daf1684fd74b5ccc351
diff --git a/base/message_pump_libevent_unittest.cc b/base/message_pump_libevent_unittest.cc
index 9ec78de..c3ec97e 100644
--- a/base/message_pump_libevent_unittest.cc
+++ b/base/message_pump_libevent_unittest.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
@@ -59,7 +59,7 @@
   virtual void OnFileCanWriteWithoutBlocking(int fd) {}
 };
 
-#if GTEST_HAS_DEATH_TEST
+#if GTEST_HAS_DEATH_TEST && !defined(NDEBUG)
 
 // Test to make sure that we catch calling WatchFileDescriptor off of the
 // wrong thread.
@@ -67,13 +67,13 @@
   MessagePumpLibevent::FileDescriptorWatcher watcher;
   StupidWatcher delegate;
 
-  ASSERT_DEBUG_DEATH(io_loop()->WatchFileDescriptor(
+  ASSERT_DEATH(io_loop()->WatchFileDescriptor(
       STDOUT_FILENO, false, MessageLoopForIO::WATCH_READ, &watcher, &delegate),
       "Check failed: "
       "watch_file_descriptor_caller_checker_.CalledOnValidThread()");
 }
 
-#endif  // GTEST_HAS_DEATH_TEST
+#endif  // GTEST_HAS_DEATH_TEST && !defined(NDEBUG)
 
 class DeleteWatcher : public MessagePumpLibevent::Watcher {
  public:
diff --git a/base/threading/thread_checker.h b/base/threading/thread_checker.h
index 33b6764..02efb5d 100644
--- a/base/threading/thread_checker.h
+++ b/base/threading/thread_checker.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
@@ -6,7 +6,18 @@
 #define BASE_THREADING_THREAD_CHECKER_H_
 #pragma once
 
-#ifndef NDEBUG
+// Apart from debug builds, we also enable the thread checker in
+// builds with DCHECK_ALWAYS_ON so that trybots and waterfall bots
+// with this define will get the same level of thread checking as
+// debug bots.
+//
+// Note that this does not perfectly match situations where DCHECK is
+// enabled.  For example a non-official release build may have
+// DCHECK_ALWAYS_ON undefined (and therefore ThreadChecker would be
+// disabled) but have DCHECKs enabled at runtime.
+#define ENABLE_THREAD_CHECKER (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON))
+
+#if ENABLE_THREAD_CHECKER
 #include "base/threading/thread_checker_impl.h"
 #endif
 
@@ -46,13 +57,15 @@
 // }
 //
 // In Release mode, CalledOnValidThread will always return true.
-#ifndef NDEBUG
+#if ENABLE_THREAD_CHECKER
 class ThreadChecker : public ThreadCheckerImpl {
 };
 #else
 class ThreadChecker : public ThreadCheckerDoNothing {
 };
-#endif  // NDEBUG
+#endif  // ENABLE_THREAD_CHECKER
+
+#undef ENABLE_THREAD_CHECKER
 
 }  // namespace base
 
diff --git a/base/threading/thread_checker_unittest.cc b/base/threading/thread_checker_unittest.cc
index e1e5715..1760cd0 100644
--- a/base/threading/thread_checker_unittest.cc
+++ b/base/threading/thread_checker_unittest.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
@@ -9,6 +9,10 @@
 #include "base/threading/simple_thread.h"
 #include "testing/gtest/include/gtest/gtest.h"
 
+// Duplicated from base/threading/thread_checker.h so that we can be
+// good citizens there and undef the macro.
+#define ENABLE_THREAD_CHECKER (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON))
+
 namespace base {
 
 // Simple class to exercise the basics of ThreadChecker.
@@ -107,7 +111,7 @@
   call_on_thread.Join();
 }
 
-#if GTEST_HAS_DEATH_TEST || NDEBUG
+#if GTEST_HAS_DEATH_TEST || !ENABLE_THREAD_CHECKER
 
 void ThreadCheckerClass::MethodOnDifferentThreadImpl() {
   scoped_ptr<ThreadCheckerClass> thread_checker_class(
@@ -121,7 +125,7 @@
   call_on_thread.Join();
 }
 
-#ifndef NDEBUG
+#if ENABLE_THREAD_CHECKER
 TEST(ThreadCheckerDeathTest, MethodNotAllowedOnDifferentThreadInDebug) {
   ASSERT_DEBUG_DEATH({
       ThreadCheckerClass::MethodOnDifferentThreadImpl();
@@ -131,7 +135,7 @@
 TEST(ThreadCheckerTest, MethodAllowedOnDifferentThreadInRelease) {
   ThreadCheckerClass::MethodOnDifferentThreadImpl();
 }
-#endif  // NDEBUG
+#endif  // ENABLE_THREAD_CHECKER
 
 void ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl() {
   scoped_ptr<ThreadCheckerClass> thread_checker_class(
@@ -150,7 +154,7 @@
   thread_checker_class->DoStuff();
 }
 
-#ifndef NDEBUG
+#if ENABLE_THREAD_CHECKER
 TEST(ThreadCheckerDeathTest, DetachFromThreadInDebug) {
   ASSERT_DEBUG_DEATH({
     ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl();
@@ -160,8 +164,11 @@
 TEST(ThreadCheckerTest, DetachFromThreadInRelease) {
   ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl();
 }
-#endif  // NDEBUG
+#endif  // ENABLE_THREAD_CHECKER
 
-#endif  // GTEST_HAS_DEATH_TEST || NDEBUG
+#endif  // GTEST_HAS_DEATH_TEST || !ENABLE_THREAD_CHECKER
+
+// Just in case we ever get lumped together with other compilation units.
+#undef ENABLE_THREAD_CHECKER
 
 }  // namespace base