| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/threading/non_thread_safe.h" | 5 #include "base/threading/non_thread_safe.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/test/gtest_util.h" | 11 #include "base/test/gtest_util.h" |
| 12 #include "base/threading/simple_thread.h" | 12 #include "base/threading/simple_thread.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 // Duplicated from base/threading/non_thread_safe.h so that we can be | |
| 16 // good citizens there and undef the macro. | |
| 17 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) | |
| 18 #define ENABLE_NON_THREAD_SAFE 1 | |
| 19 #else | |
| 20 #define ENABLE_NON_THREAD_SAFE 0 | |
| 21 #endif | |
| 22 | |
| 23 namespace base { | 15 namespace base { |
| 24 | 16 |
| 25 namespace { | 17 namespace { |
| 26 | 18 |
| 27 // Simple class to exersice the basics of NonThreadSafe. | 19 // Simple class to exersice the basics of NonThreadSafe. |
| 28 // Both the destructor and DoStuff should verify that they were | 20 // Both the destructor and DoStuff should verify that they were |
| 29 // called on the same thread as the constructor. | 21 // called on the same thread as the constructor. |
| 30 class NonThreadSafeClass : public NonThreadSafe { | 22 class NonThreadSafeClass : public NonThreadSafe { |
| 31 public: | 23 public: |
| 32 NonThreadSafeClass() {} | 24 NonThreadSafeClass() {} |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 new NonThreadSafeClass); | 104 new NonThreadSafeClass); |
| 113 | 105 |
| 114 // Verify that DoStuff asserts in debug builds only when called | 106 // Verify that DoStuff asserts in debug builds only when called |
| 115 // on a different thread. | 107 // on a different thread. |
| 116 CallDoStuffOnThread call_on_thread(non_thread_safe_class.get()); | 108 CallDoStuffOnThread call_on_thread(non_thread_safe_class.get()); |
| 117 | 109 |
| 118 call_on_thread.Start(); | 110 call_on_thread.Start(); |
| 119 call_on_thread.Join(); | 111 call_on_thread.Join(); |
| 120 } | 112 } |
| 121 | 113 |
| 122 #if ENABLE_NON_THREAD_SAFE | 114 #if DCHECK_IS_ON() |
| 123 TEST(NonThreadSafeDeathTest, MethodNotAllowedOnDifferentThreadInDebug) { | 115 TEST(NonThreadSafeDeathTest, MethodNotAllowedOnDifferentThreadInDebug) { |
| 124 ASSERT_DCHECK_DEATH({ NonThreadSafeClass::MethodOnDifferentThreadImpl(); }, | 116 ASSERT_DCHECK_DEATH({ NonThreadSafeClass::MethodOnDifferentThreadImpl(); }, |
| 125 ""); | 117 ""); |
| 126 } | 118 } |
| 127 #else | 119 #else |
| 128 TEST(NonThreadSafeTest, MethodAllowedOnDifferentThreadInRelease) { | 120 TEST(NonThreadSafeTest, MethodAllowedOnDifferentThreadInRelease) { |
| 129 NonThreadSafeClass::MethodOnDifferentThreadImpl(); | 121 NonThreadSafeClass::MethodOnDifferentThreadImpl(); |
| 130 } | 122 } |
| 131 #endif // ENABLE_NON_THREAD_SAFE | 123 #endif // DCHECK_IS_ON() |
| 132 | 124 |
| 133 void NonThreadSafeClass::DestructorOnDifferentThreadImpl() { | 125 void NonThreadSafeClass::DestructorOnDifferentThreadImpl() { |
| 134 std::unique_ptr<NonThreadSafeClass> non_thread_safe_class( | 126 std::unique_ptr<NonThreadSafeClass> non_thread_safe_class( |
| 135 new NonThreadSafeClass); | 127 new NonThreadSafeClass); |
| 136 | 128 |
| 137 // Verify that the destructor asserts in debug builds only | 129 // Verify that the destructor asserts in debug builds only |
| 138 // when called on a different thread. | 130 // when called on a different thread. |
| 139 DeleteNonThreadSafeClassOnThread delete_on_thread( | 131 DeleteNonThreadSafeClassOnThread delete_on_thread( |
| 140 non_thread_safe_class.release()); | 132 non_thread_safe_class.release()); |
| 141 | 133 |
| 142 delete_on_thread.Start(); | 134 delete_on_thread.Start(); |
| 143 delete_on_thread.Join(); | 135 delete_on_thread.Join(); |
| 144 } | 136 } |
| 145 | 137 |
| 146 #if ENABLE_NON_THREAD_SAFE | 138 #if DCHECK_IS_ON() |
| 147 TEST(NonThreadSafeDeathTest, DestructorNotAllowedOnDifferentThreadInDebug) { | 139 TEST(NonThreadSafeDeathTest, DestructorNotAllowedOnDifferentThreadInDebug) { |
| 148 ASSERT_DCHECK_DEATH( | 140 ASSERT_DCHECK_DEATH( |
| 149 { NonThreadSafeClass::DestructorOnDifferentThreadImpl(); }, ""); | 141 { NonThreadSafeClass::DestructorOnDifferentThreadImpl(); }, ""); |
| 150 } | 142 } |
| 151 #else | 143 #else |
| 152 TEST(NonThreadSafeTest, DestructorAllowedOnDifferentThreadInRelease) { | 144 TEST(NonThreadSafeTest, DestructorAllowedOnDifferentThreadInRelease) { |
| 153 NonThreadSafeClass::DestructorOnDifferentThreadImpl(); | 145 NonThreadSafeClass::DestructorOnDifferentThreadImpl(); |
| 154 } | 146 } |
| 155 #endif // ENABLE_NON_THREAD_SAFE | 147 #endif // DCHECK_IS_ON() |
| 156 | |
| 157 // Just in case we ever get lumped together with other compilation units. | |
| 158 #undef ENABLE_NON_THREAD_SAFE | |
| 159 | 148 |
| 160 } // namespace base | 149 } // namespace base |
| OLD | NEW |