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/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
6 #include "base/memory/weak_ptr.h" | 6 #include "base/memory/weak_ptr.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
10 #include "base/threading/thread.h" | 10 #include "base/threading/thread.h" |
(...skipping 29 matching lines...) Expand all Loading... |
40 struct Consumer { WeakPtr<Producer> producer; }; | 40 struct Consumer { WeakPtr<Producer> producer; }; |
41 | 41 |
42 // Helper class to create and destroy weak pointer copies | 42 // Helper class to create and destroy weak pointer copies |
43 // and delete objects on a background thread. | 43 // and delete objects on a background thread. |
44 class BackgroundThread : public Thread { | 44 class BackgroundThread : public Thread { |
45 public: | 45 public: |
46 BackgroundThread() | 46 BackgroundThread() |
47 : Thread("owner_thread") { | 47 : Thread("owner_thread") { |
48 } | 48 } |
49 | 49 |
| 50 ~BackgroundThread() { |
| 51 Stop(); |
| 52 } |
| 53 |
50 void CreateConsumerFromProducer(Consumer** consumer, Producer* producer) { | 54 void CreateConsumerFromProducer(Consumer** consumer, Producer* producer) { |
51 WaitableEvent completion(true, false); | 55 WaitableEvent completion(true, false); |
52 message_loop()->PostTask( | 56 message_loop()->PostTask( |
53 FROM_HERE, | 57 FROM_HERE, |
54 NewRunnableFunction(&BackgroundThread::DoCreateFromProducer, | 58 NewRunnableFunction(&BackgroundThread::DoCreateFromProducer, |
55 consumer, | 59 consumer, |
56 producer, | 60 producer, |
57 &completion)); | 61 &completion)); |
58 completion.Wait(); | 62 completion.Wait(); |
59 } | 63 } |
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
335 Producer producer; | 339 Producer producer; |
336 Consumer consumer; | 340 Consumer consumer; |
337 consumer.producer = producer.AsWeakPtr(); | 341 consumer.producer = producer.AsWeakPtr(); |
338 thread.CreateConsumerFromConsumer(&consumer_copy, &consumer); | 342 thread.CreateConsumerFromConsumer(&consumer_copy, &consumer); |
339 } | 343 } |
340 EXPECT_TRUE(consumer_copy->producer == NULL); | 344 EXPECT_TRUE(consumer_copy->producer == NULL); |
341 thread.DeleteConsumer(consumer_copy); | 345 thread.DeleteConsumer(consumer_copy); |
342 } | 346 } |
343 | 347 |
344 } // namespace base | 348 } // namespace base |
OLD | NEW |