| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "testing/gtest/include/gtest/gtest.h" | 5 #include "testing/gtest/include/gtest/gtest.h" |
| 6 #include "base/thread.h" | 6 #include "base/thread.h" |
| 7 #include "base/weak_ptr.h" | 7 #include "base/weak_ptr.h" |
| 8 | 8 |
| 9 namespace base { | 9 namespace base { |
| 10 namespace { | 10 namespace { |
| 11 | 11 |
| 12 template <class T> | 12 template <class T> |
| 13 class OffThreadObjectCreator { | 13 class OffThreadObjectCreator { |
| 14 public: | 14 public: |
| 15 static T* NewObject() { | 15 static T* NewObject() { |
| 16 T* result; | 16 T* result; |
| 17 { | 17 { |
| 18 Thread creator_thread("creator_thread"); | 18 Thread creator_thread("creator_thread"); |
| 19 creator_thread.Start(); | 19 creator_thread.Start(); |
| 20 creator_thread.message_loop()->PostTask(FROM_HERE, | 20 creator_thread.message_loop()->PostTask( |
| 21 FROM_HERE, |
| 21 NewRunnableFunction(OffThreadObjectCreator::CreateObject, &result)); | 22 NewRunnableFunction(OffThreadObjectCreator::CreateObject, &result)); |
| 22 } | 23 } |
| 23 DCHECK(result); // We synchronized on thread destruction above. | 24 DCHECK(result); // We synchronized on thread destruction above. |
| 24 return result; | 25 return result; |
| 25 } | 26 } |
| 26 private: | 27 private: |
| 27 static void CreateObject(T** result) { | 28 static void CreateObject(T** result) { |
| 28 *result = new T; | 29 *result = new T; |
| 29 } | 30 } |
| 30 }; | 31 }; |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 // Test that it is OK to create a class that has a WeakPtr member on one | 126 // Test that it is OK to create a class that has a WeakPtr member on one |
| 126 // thread, but use it on another. This tests that we do not trip runtime | 127 // thread, but use it on another. This tests that we do not trip runtime |
| 127 // checks that ensure that a weak reference is not used by multiple threads. | 128 // checks that ensure that a weak reference is not used by multiple threads. |
| 128 scoped_ptr<Consumer> consumer(OffThreadObjectCreator<Consumer>::NewObject()); | 129 scoped_ptr<Consumer> consumer(OffThreadObjectCreator<Consumer>::NewObject()); |
| 129 Producer producer; | 130 Producer producer; |
| 130 consumer->producer = producer.AsWeakPtr(); | 131 consumer->producer = producer.AsWeakPtr(); |
| 131 EXPECT_EQ(&producer, consumer->producer.get()); | 132 EXPECT_EQ(&producer, consumer->producer.get()); |
| 132 } | 133 } |
| 133 | 134 |
| 134 } // namespace base | 135 } // namespace base |
| OLD | NEW |