| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/bind.h" |
| 5 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
| 6 #include "base/memory/weak_ptr.h" | 7 #include "base/memory/weak_ptr.h" |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 8 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 9 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
| 10 #include "base/threading/thread.h" | 11 #include "base/threading/thread.h" |
| 11 | 12 |
| 12 namespace base { | 13 namespace base { |
| 13 namespace { | 14 namespace { |
| 14 | 15 |
| 15 template <class T> | 16 template <class T> |
| 16 class OffThreadObjectCreator { | 17 class OffThreadObjectCreator { |
| 17 public: | 18 public: |
| 18 static T* NewObject() { | 19 static T* NewObject() { |
| 19 T* result; | 20 T* result; |
| 20 { | 21 { |
| 21 Thread creator_thread("creator_thread"); | 22 Thread creator_thread("creator_thread"); |
| 22 creator_thread.Start(); | 23 creator_thread.Start(); |
| 23 creator_thread.message_loop()->PostTask( | 24 creator_thread.message_loop()->PostTask( |
| 24 FROM_HERE, | 25 FROM_HERE, |
| 25 NewRunnableFunction(OffThreadObjectCreator::CreateObject, &result)); | 26 base::Bind(OffThreadObjectCreator::CreateObject, &result)); |
| 26 } | 27 } |
| 27 DCHECK(result); // We synchronized on thread destruction above. | 28 DCHECK(result); // We synchronized on thread destruction above. |
| 28 return result; | 29 return result; |
| 29 } | 30 } |
| 30 private: | 31 private: |
| 31 static void CreateObject(T** result) { | 32 static void CreateObject(T** result) { |
| 32 *result = new T; | 33 *result = new T; |
| 33 } | 34 } |
| 34 }; | 35 }; |
| 35 | 36 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 48 } | 49 } |
| 49 | 50 |
| 50 ~BackgroundThread() { | 51 ~BackgroundThread() { |
| 51 Stop(); | 52 Stop(); |
| 52 } | 53 } |
| 53 | 54 |
| 54 void CreateConsumerFromProducer(Consumer** consumer, Producer* producer) { | 55 void CreateConsumerFromProducer(Consumer** consumer, Producer* producer) { |
| 55 WaitableEvent completion(true, false); | 56 WaitableEvent completion(true, false); |
| 56 message_loop()->PostTask( | 57 message_loop()->PostTask( |
| 57 FROM_HERE, | 58 FROM_HERE, |
| 58 NewRunnableFunction(&BackgroundThread::DoCreateFromProducer, | 59 base::Bind(&BackgroundThread::DoCreateFromProducer, consumer, producer, |
| 59 consumer, | 60 &completion)); |
| 60 producer, | |
| 61 &completion)); | |
| 62 completion.Wait(); | 61 completion.Wait(); |
| 63 } | 62 } |
| 64 | 63 |
| 65 void CreateConsumerFromConsumer(Consumer** consumer, const Consumer* other) { | 64 void CreateConsumerFromConsumer(Consumer** consumer, const Consumer* other) { |
| 66 WaitableEvent completion(true, false); | 65 WaitableEvent completion(true, false); |
| 67 message_loop()->PostTask( | 66 message_loop()->PostTask( |
| 68 FROM_HERE, | 67 FROM_HERE, |
| 69 NewRunnableFunction(&BackgroundThread::DoCreateFromConsumer, | 68 base::Bind(&BackgroundThread::DoCreateFromConsumer, consumer, other, |
| 70 consumer, | 69 &completion)); |
| 71 other, | |
| 72 &completion)); | |
| 73 completion.Wait(); | 70 completion.Wait(); |
| 74 } | 71 } |
| 75 | 72 |
| 76 void DeleteProducer(Producer* object) { | 73 void DeleteProducer(Producer* object) { |
| 77 WaitableEvent completion(true, false); | 74 WaitableEvent completion(true, false); |
| 78 message_loop()->PostTask( | 75 message_loop()->PostTask( |
| 79 FROM_HERE, | 76 FROM_HERE, |
| 80 NewRunnableFunction(&BackgroundThread::DoDeleteProducer, | 77 base::Bind(&BackgroundThread::DoDeleteProducer, object, &completion)); |
| 81 object, | |
| 82 &completion)); | |
| 83 completion.Wait(); | 78 completion.Wait(); |
| 84 } | 79 } |
| 85 | 80 |
| 86 void DeleteConsumer(Consumer* object) { | 81 void DeleteConsumer(Consumer* object) { |
| 87 WaitableEvent completion(true, false); | 82 WaitableEvent completion(true, false); |
| 88 message_loop()->PostTask( | 83 message_loop()->PostTask( |
| 89 FROM_HERE, | 84 FROM_HERE, |
| 90 NewRunnableFunction(&BackgroundThread::DoDeleteConsumer, | 85 base::Bind(&BackgroundThread::DoDeleteConsumer, object, &completion)); |
| 91 object, | |
| 92 &completion)); | |
| 93 completion.Wait(); | 86 completion.Wait(); |
| 94 } | 87 } |
| 95 | 88 |
| 96 Producer* DeRef(const Consumer* consumer) { | 89 Producer* DeRef(const Consumer* consumer) { |
| 97 WaitableEvent completion(true, false); | 90 WaitableEvent completion(true, false); |
| 98 Producer* result = NULL; | 91 Producer* result = NULL; |
| 99 message_loop()->PostTask( | 92 message_loop()->PostTask( |
| 100 FROM_HERE, | 93 FROM_HERE, |
| 101 NewRunnableFunction(&BackgroundThread::DoDeRef, | 94 base::Bind(&BackgroundThread::DoDeRef, consumer, &result, &completion)); |
| 102 consumer, | |
| 103 &result, | |
| 104 &completion)); | |
| 105 completion.Wait(); | 95 completion.Wait(); |
| 106 return result; | 96 return result; |
| 107 } | 97 } |
| 108 | 98 |
| 109 protected: | 99 protected: |
| 110 static void DoCreateFromConsumer(Consumer** consumer, | 100 static void DoCreateFromConsumer(Consumer** consumer, |
| 111 const Consumer* other, | 101 const Consumer* other, |
| 112 WaitableEvent* completion) { | 102 WaitableEvent* completion) { |
| 113 *consumer = new Consumer; | 103 *consumer = new Consumer; |
| 114 **consumer = *other; | 104 **consumer = *other; |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 339 Producer producer; | 329 Producer producer; |
| 340 Consumer consumer; | 330 Consumer consumer; |
| 341 consumer.producer = producer.AsWeakPtr(); | 331 consumer.producer = producer.AsWeakPtr(); |
| 342 thread.CreateConsumerFromConsumer(&consumer_copy, &consumer); | 332 thread.CreateConsumerFromConsumer(&consumer_copy, &consumer); |
| 343 } | 333 } |
| 344 EXPECT_TRUE(consumer_copy->producer == NULL); | 334 EXPECT_TRUE(consumer_copy->producer == NULL); |
| 345 thread.DeleteConsumer(consumer_copy); | 335 thread.DeleteConsumer(consumer_copy); |
| 346 } | 336 } |
| 347 | 337 |
| 348 } // namespace base | 338 } // namespace base |
| OLD | NEW |