| 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" |
| 6 |
| 7 #include <memory> |
| 8 |
| 5 #include "base/logging.h" | 9 #include "base/logging.h" |
| 6 #include "base/macros.h" | 10 #include "base/macros.h" |
| 7 #include "base/memory/scoped_ptr.h" | |
| 8 #include "base/threading/non_thread_safe.h" | |
| 9 #include "base/threading/simple_thread.h" | 11 #include "base/threading/simple_thread.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 13 |
| 12 // Duplicated from base/threading/non_thread_safe.h so that we can be | 14 // Duplicated from base/threading/non_thread_safe.h so that we can be |
| 13 // good citizens there and undef the macro. | 15 // good citizens there and undef the macro. |
| 14 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) | 16 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) |
| 15 #define ENABLE_NON_THREAD_SAFE 1 | 17 #define ENABLE_NON_THREAD_SAFE 1 |
| 16 #else | 18 #else |
| 17 #define ENABLE_NON_THREAD_SAFE 0 | 19 #define ENABLE_NON_THREAD_SAFE 0 |
| 18 #endif | 20 #endif |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 public: | 67 public: |
| 66 explicit DeleteNonThreadSafeClassOnThread( | 68 explicit DeleteNonThreadSafeClassOnThread( |
| 67 NonThreadSafeClass* non_thread_safe_class) | 69 NonThreadSafeClass* non_thread_safe_class) |
| 68 : SimpleThread("delete_non_thread_safe_class_on_thread"), | 70 : SimpleThread("delete_non_thread_safe_class_on_thread"), |
| 69 non_thread_safe_class_(non_thread_safe_class) { | 71 non_thread_safe_class_(non_thread_safe_class) { |
| 70 } | 72 } |
| 71 | 73 |
| 72 void Run() override { non_thread_safe_class_.reset(); } | 74 void Run() override { non_thread_safe_class_.reset(); } |
| 73 | 75 |
| 74 private: | 76 private: |
| 75 scoped_ptr<NonThreadSafeClass> non_thread_safe_class_; | 77 std::unique_ptr<NonThreadSafeClass> non_thread_safe_class_; |
| 76 | 78 |
| 77 DISALLOW_COPY_AND_ASSIGN(DeleteNonThreadSafeClassOnThread); | 79 DISALLOW_COPY_AND_ASSIGN(DeleteNonThreadSafeClassOnThread); |
| 78 }; | 80 }; |
| 79 | 81 |
| 80 } // namespace | 82 } // namespace |
| 81 | 83 |
| 82 TEST(NonThreadSafeTest, CallsAllowedOnSameThread) { | 84 TEST(NonThreadSafeTest, CallsAllowedOnSameThread) { |
| 83 scoped_ptr<NonThreadSafeClass> non_thread_safe_class( | 85 std::unique_ptr<NonThreadSafeClass> non_thread_safe_class( |
| 84 new NonThreadSafeClass); | 86 new NonThreadSafeClass); |
| 85 | 87 |
| 86 // Verify that DoStuff doesn't assert. | 88 // Verify that DoStuff doesn't assert. |
| 87 non_thread_safe_class->DoStuff(); | 89 non_thread_safe_class->DoStuff(); |
| 88 | 90 |
| 89 // Verify that the destructor doesn't assert. | 91 // Verify that the destructor doesn't assert. |
| 90 non_thread_safe_class.reset(); | 92 non_thread_safe_class.reset(); |
| 91 } | 93 } |
| 92 | 94 |
| 93 TEST(NonThreadSafeTest, DetachThenDestructOnDifferentThread) { | 95 TEST(NonThreadSafeTest, DetachThenDestructOnDifferentThread) { |
| 94 scoped_ptr<NonThreadSafeClass> non_thread_safe_class( | 96 std::unique_ptr<NonThreadSafeClass> non_thread_safe_class( |
| 95 new NonThreadSafeClass); | 97 new NonThreadSafeClass); |
| 96 | 98 |
| 97 // Verify that the destructor doesn't assert when called on a different thread | 99 // Verify that the destructor doesn't assert when called on a different thread |
| 98 // after a detach. | 100 // after a detach. |
| 99 non_thread_safe_class->DetachFromThread(); | 101 non_thread_safe_class->DetachFromThread(); |
| 100 DeleteNonThreadSafeClassOnThread delete_on_thread( | 102 DeleteNonThreadSafeClassOnThread delete_on_thread( |
| 101 non_thread_safe_class.release()); | 103 non_thread_safe_class.release()); |
| 102 | 104 |
| 103 delete_on_thread.Start(); | 105 delete_on_thread.Start(); |
| 104 delete_on_thread.Join(); | 106 delete_on_thread.Join(); |
| 105 } | 107 } |
| 106 | 108 |
| 107 #if GTEST_HAS_DEATH_TEST || !ENABLE_NON_THREAD_SAFE | 109 #if GTEST_HAS_DEATH_TEST || !ENABLE_NON_THREAD_SAFE |
| 108 | 110 |
| 109 void NonThreadSafeClass::MethodOnDifferentThreadImpl() { | 111 void NonThreadSafeClass::MethodOnDifferentThreadImpl() { |
| 110 scoped_ptr<NonThreadSafeClass> non_thread_safe_class( | 112 std::unique_ptr<NonThreadSafeClass> non_thread_safe_class( |
| 111 new NonThreadSafeClass); | 113 new NonThreadSafeClass); |
| 112 | 114 |
| 113 // Verify that DoStuff asserts in debug builds only when called | 115 // Verify that DoStuff asserts in debug builds only when called |
| 114 // on a different thread. | 116 // on a different thread. |
| 115 CallDoStuffOnThread call_on_thread(non_thread_safe_class.get()); | 117 CallDoStuffOnThread call_on_thread(non_thread_safe_class.get()); |
| 116 | 118 |
| 117 call_on_thread.Start(); | 119 call_on_thread.Start(); |
| 118 call_on_thread.Join(); | 120 call_on_thread.Join(); |
| 119 } | 121 } |
| 120 | 122 |
| 121 #if ENABLE_NON_THREAD_SAFE | 123 #if ENABLE_NON_THREAD_SAFE |
| 122 TEST(NonThreadSafeDeathTest, MethodNotAllowedOnDifferentThreadInDebug) { | 124 TEST(NonThreadSafeDeathTest, MethodNotAllowedOnDifferentThreadInDebug) { |
| 123 ASSERT_DEATH({ | 125 ASSERT_DEATH({ |
| 124 NonThreadSafeClass::MethodOnDifferentThreadImpl(); | 126 NonThreadSafeClass::MethodOnDifferentThreadImpl(); |
| 125 }, ""); | 127 }, ""); |
| 126 } | 128 } |
| 127 #else | 129 #else |
| 128 TEST(NonThreadSafeTest, MethodAllowedOnDifferentThreadInRelease) { | 130 TEST(NonThreadSafeTest, MethodAllowedOnDifferentThreadInRelease) { |
| 129 NonThreadSafeClass::MethodOnDifferentThreadImpl(); | 131 NonThreadSafeClass::MethodOnDifferentThreadImpl(); |
| 130 } | 132 } |
| 131 #endif // ENABLE_NON_THREAD_SAFE | 133 #endif // ENABLE_NON_THREAD_SAFE |
| 132 | 134 |
| 133 void NonThreadSafeClass::DestructorOnDifferentThreadImpl() { | 135 void NonThreadSafeClass::DestructorOnDifferentThreadImpl() { |
| 134 scoped_ptr<NonThreadSafeClass> non_thread_safe_class( | 136 std::unique_ptr<NonThreadSafeClass> non_thread_safe_class( |
| 135 new NonThreadSafeClass); | 137 new NonThreadSafeClass); |
| 136 | 138 |
| 137 // Verify that the destructor asserts in debug builds only | 139 // Verify that the destructor asserts in debug builds only |
| 138 // when called on a different thread. | 140 // when called on a different thread. |
| 139 DeleteNonThreadSafeClassOnThread delete_on_thread( | 141 DeleteNonThreadSafeClassOnThread delete_on_thread( |
| 140 non_thread_safe_class.release()); | 142 non_thread_safe_class.release()); |
| 141 | 143 |
| 142 delete_on_thread.Start(); | 144 delete_on_thread.Start(); |
| 143 delete_on_thread.Join(); | 145 delete_on_thread.Join(); |
| 144 } | 146 } |
| 145 | 147 |
| 146 #if ENABLE_NON_THREAD_SAFE | 148 #if ENABLE_NON_THREAD_SAFE |
| 147 TEST(NonThreadSafeDeathTest, DestructorNotAllowedOnDifferentThreadInDebug) { | 149 TEST(NonThreadSafeDeathTest, DestructorNotAllowedOnDifferentThreadInDebug) { |
| 148 ASSERT_DEATH({ | 150 ASSERT_DEATH({ |
| 149 NonThreadSafeClass::DestructorOnDifferentThreadImpl(); | 151 NonThreadSafeClass::DestructorOnDifferentThreadImpl(); |
| 150 }, ""); | 152 }, ""); |
| 151 } | 153 } |
| 152 #else | 154 #else |
| 153 TEST(NonThreadSafeTest, DestructorAllowedOnDifferentThreadInRelease) { | 155 TEST(NonThreadSafeTest, DestructorAllowedOnDifferentThreadInRelease) { |
| 154 NonThreadSafeClass::DestructorOnDifferentThreadImpl(); | 156 NonThreadSafeClass::DestructorOnDifferentThreadImpl(); |
| 155 } | 157 } |
| 156 #endif // ENABLE_NON_THREAD_SAFE | 158 #endif // ENABLE_NON_THREAD_SAFE |
| 157 | 159 |
| 158 #endif // GTEST_HAS_DEATH_TEST || !ENABLE_NON_THREAD_SAFE | 160 #endif // GTEST_HAS_DEATH_TEST || !ENABLE_NON_THREAD_SAFE |
| 159 | 161 |
| 160 // Just in case we ever get lumped together with other compilation units. | 162 // Just in case we ever get lumped together with other compilation units. |
| 161 #undef ENABLE_NON_THREAD_SAFE | 163 #undef ENABLE_NON_THREAD_SAFE |
| 162 | 164 |
| 163 } // namespace base | 165 } // namespace base |
| OLD | NEW |