diff options
Diffstat (limited to 'base/threading')
-rw-r--r-- | base/threading/non_thread_safe.h | 25 | ||||
-rw-r--r-- | base/threading/non_thread_safe_unittest.cc | 31 |
2 files changed, 40 insertions, 16 deletions
diff --git a/base/threading/non_thread_safe.h b/base/threading/non_thread_safe.h index be30269..a104ae3 100644 --- a/base/threading/non_thread_safe.h +++ b/base/threading/non_thread_safe.h @@ -6,13 +6,20 @@ #define BASE_THREADING_NON_THREAD_SAFE_H_ #pragma once -// Classes deriving from NonThreadSafe may need to supress MSVC warning 4275: +// Classes deriving from NonThreadSafe may need to suppress MSVC warning 4275: // non dll-interface class 'Bar' used as base for dll-interface class 'Foo'. // There is a specific macro to do it: NON_EXPORTED_BASE(), defined in // compiler_specific.h #include "base/compiler_specific.h" -#ifndef NDEBUG +// See comment at top of thread_checker.h +#if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) +#define ENABLE_NON_THREAD_SAFE 1 +#else +#define ENABLE_NON_THREAD_SAFE 0 +#endif + +#if ENABLE_NON_THREAD_SAFE #include "base/threading/non_thread_safe_impl.h" #endif @@ -54,13 +61,15 @@ class NonThreadSafeDoNothing { // to have a base::ThreadChecker as a member, rather than inherit from // NonThreadSafe. For more details about when to choose one over the other, see // the documentation for base::ThreadChecker. -// -// In Release mode, CalledOnValidThread will always return true. -#ifndef NDEBUG -typedef NonThreadSafeImpl NonThreadSafe; +#if ENABLE_NON_THREAD_SAFE +class NonThreadSafe : public NonThreadSafeImpl { +}; #else -typedef NonThreadSafeDoNothing NonThreadSafe; -#endif // NDEBUG +class NonThreadSafe : public NonThreadSafeDoNothing { +}; +#endif // ENABLE_NON_THREAD_SAFE + +#undef ENABLE_NON_THREAD_SAFE } // namespace base diff --git a/base/threading/non_thread_safe_unittest.cc b/base/threading/non_thread_safe_unittest.cc index b8376f3..ee31701 100644 --- a/base/threading/non_thread_safe_unittest.cc +++ b/base/threading/non_thread_safe_unittest.cc @@ -9,8 +9,18 @@ #include "base/threading/simple_thread.h" #include "testing/gtest/include/gtest/gtest.h" +// Duplicated from base/threading/non_thread_safe.h so that we can be +// good citizens there and undef the macro. +#if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) +#define ENABLE_NON_THREAD_SAFE 1 +#else +#define ENABLE_NON_THREAD_SAFE 0 +#endif + namespace base { +namespace { + // Simple class to exersice the basics of NonThreadSafe. // Both the destructor and DoStuff should verify that they were // called on the same thread as the constructor. @@ -70,6 +80,8 @@ class DeleteNonThreadSafeClassOnThread : public SimpleThread { DISALLOW_COPY_AND_ASSIGN(DeleteNonThreadSafeClassOnThread); }; +} // namespace + TEST(NonThreadSafeTest, CallsAllowedOnSameThread) { scoped_ptr<NonThreadSafeClass> non_thread_safe_class( new NonThreadSafeClass); @@ -95,7 +107,7 @@ TEST(NonThreadSafeTest, DetachThenDestructOnDifferentThread) { delete_on_thread.Join(); } -#if GTEST_HAS_DEATH_TEST || NDEBUG +#if GTEST_HAS_DEATH_TEST || !ENABLE_NON_THREAD_SAFE void NonThreadSafeClass::MethodOnDifferentThreadImpl() { scoped_ptr<NonThreadSafeClass> non_thread_safe_class( @@ -109,9 +121,9 @@ void NonThreadSafeClass::MethodOnDifferentThreadImpl() { call_on_thread.Join(); } -#ifndef NDEBUG +#if ENABLE_NON_THREAD_SAFE TEST(NonThreadSafeDeathTest, MethodNotAllowedOnDifferentThreadInDebug) { - ASSERT_DEBUG_DEATH({ + ASSERT_DEATH({ NonThreadSafeClass::MethodOnDifferentThreadImpl(); }, ""); } @@ -119,7 +131,7 @@ TEST(NonThreadSafeDeathTest, MethodNotAllowedOnDifferentThreadInDebug) { TEST(NonThreadSafeTest, MethodAllowedOnDifferentThreadInRelease) { NonThreadSafeClass::MethodOnDifferentThreadImpl(); } -#endif // NDEBUG +#endif // ENABLE_NON_THREAD_SAFE void NonThreadSafeClass::DestructorOnDifferentThreadImpl() { scoped_ptr<NonThreadSafeClass> non_thread_safe_class( @@ -134,9 +146,9 @@ void NonThreadSafeClass::DestructorOnDifferentThreadImpl() { delete_on_thread.Join(); } -#ifndef NDEBUG +#if ENABLE_NON_THREAD_SAFE TEST(NonThreadSafeDeathTest, DestructorNotAllowedOnDifferentThreadInDebug) { - ASSERT_DEBUG_DEATH({ + ASSERT_DEATH({ NonThreadSafeClass::DestructorOnDifferentThreadImpl(); }, ""); } @@ -144,8 +156,11 @@ TEST(NonThreadSafeDeathTest, DestructorNotAllowedOnDifferentThreadInDebug) { TEST(NonThreadSafeTest, DestructorAllowedOnDifferentThreadInRelease) { NonThreadSafeClass::DestructorOnDifferentThreadImpl(); } -#endif // NDEBUG +#endif // ENABLE_NON_THREAD_SAFE + +#endif // GTEST_HAS_DEATH_TEST || !ENABLE_NON_THREAD_SAFE -#endif // GTEST_HAS_DEATH_TEST || NDEBUG +// Just in case we ever get lumped together with other compilation units. +#undef ENABLE_NON_THREAD_SAFE } // namespace base |