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/memory/weak_ptr.h" | 5 #include "base/memory/weak_ptr.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/debug/leak_annotations.h" | 10 #include "base/debug/leak_annotations.h" |
11 #include "base/location.h" | |
12 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
13 #include "base/single_thread_task_runner.h" | 12 #include "base/message_loop/message_loop.h" |
14 #include "base/synchronization/waitable_event.h" | 13 #include "base/synchronization/waitable_event.h" |
15 #include "base/threading/thread.h" | 14 #include "base/threading/thread.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
17 | 16 |
18 namespace base { | 17 namespace base { |
19 namespace { | 18 namespace { |
20 | 19 |
21 template <class T> | 20 template <class T> |
22 class OffThreadObjectCreator { | 21 class OffThreadObjectCreator { |
23 public: | 22 public: |
24 static T* NewObject() { | 23 static T* NewObject() { |
25 T* result; | 24 T* result; |
26 { | 25 { |
27 Thread creator_thread("creator_thread"); | 26 Thread creator_thread("creator_thread"); |
28 creator_thread.Start(); | 27 creator_thread.Start(); |
29 creator_thread.task_runner()->PostTask( | 28 creator_thread.message_loop()->PostTask( |
30 FROM_HERE, base::Bind(OffThreadObjectCreator::CreateObject, &result)); | 29 FROM_HERE, |
| 30 base::Bind(OffThreadObjectCreator::CreateObject, &result)); |
31 } | 31 } |
32 DCHECK(result); // We synchronized on thread destruction above. | 32 DCHECK(result); // We synchronized on thread destruction above. |
33 return result; | 33 return result; |
34 } | 34 } |
35 private: | 35 private: |
36 static void CreateObject(T** result) { | 36 static void CreateObject(T** result) { |
37 *result = new T; | 37 *result = new T; |
38 } | 38 } |
39 }; | 39 }; |
40 | 40 |
(...skipping 18 matching lines...) Expand all Loading... |
59 // Helper class to create and destroy weak pointer copies | 59 // Helper class to create and destroy weak pointer copies |
60 // and delete objects on a background thread. | 60 // and delete objects on a background thread. |
61 class BackgroundThread : public Thread { | 61 class BackgroundThread : public Thread { |
62 public: | 62 public: |
63 BackgroundThread() : Thread("owner_thread") {} | 63 BackgroundThread() : Thread("owner_thread") {} |
64 | 64 |
65 ~BackgroundThread() override { Stop(); } | 65 ~BackgroundThread() override { Stop(); } |
66 | 66 |
67 void CreateArrowFromTarget(Arrow** arrow, Target* target) { | 67 void CreateArrowFromTarget(Arrow** arrow, Target* target) { |
68 WaitableEvent completion(true, false); | 68 WaitableEvent completion(true, false); |
69 task_runner()->PostTask( | 69 message_loop()->PostTask( |
70 FROM_HERE, base::Bind(&BackgroundThread::DoCreateArrowFromTarget, arrow, | 70 FROM_HERE, |
71 target, &completion)); | 71 base::Bind(&BackgroundThread::DoCreateArrowFromTarget, |
| 72 arrow, target, &completion)); |
72 completion.Wait(); | 73 completion.Wait(); |
73 } | 74 } |
74 | 75 |
75 void CreateArrowFromArrow(Arrow** arrow, const Arrow* other) { | 76 void CreateArrowFromArrow(Arrow** arrow, const Arrow* other) { |
76 WaitableEvent completion(true, false); | 77 WaitableEvent completion(true, false); |
77 task_runner()->PostTask( | 78 message_loop()->PostTask( |
78 FROM_HERE, base::Bind(&BackgroundThread::DoCreateArrowFromArrow, arrow, | 79 FROM_HERE, |
79 other, &completion)); | 80 base::Bind(&BackgroundThread::DoCreateArrowFromArrow, |
| 81 arrow, other, &completion)); |
80 completion.Wait(); | 82 completion.Wait(); |
81 } | 83 } |
82 | 84 |
83 void DeleteTarget(Target* object) { | 85 void DeleteTarget(Target* object) { |
84 WaitableEvent completion(true, false); | 86 WaitableEvent completion(true, false); |
85 task_runner()->PostTask( | 87 message_loop()->PostTask( |
86 FROM_HERE, | 88 FROM_HERE, |
87 base::Bind(&BackgroundThread::DoDeleteTarget, object, &completion)); | 89 base::Bind(&BackgroundThread::DoDeleteTarget, object, &completion)); |
88 completion.Wait(); | 90 completion.Wait(); |
89 } | 91 } |
90 | 92 |
91 void CopyAndAssignArrow(Arrow* object) { | 93 void CopyAndAssignArrow(Arrow* object) { |
92 WaitableEvent completion(true, false); | 94 WaitableEvent completion(true, false); |
93 task_runner()->PostTask( | 95 message_loop()->PostTask( |
94 FROM_HERE, base::Bind(&BackgroundThread::DoCopyAndAssignArrow, object, | 96 FROM_HERE, |
95 &completion)); | 97 base::Bind(&BackgroundThread::DoCopyAndAssignArrow, |
| 98 object, &completion)); |
96 completion.Wait(); | 99 completion.Wait(); |
97 } | 100 } |
98 | 101 |
99 void CopyAndAssignArrowBase(Arrow* object) { | 102 void CopyAndAssignArrowBase(Arrow* object) { |
100 WaitableEvent completion(true, false); | 103 WaitableEvent completion(true, false); |
101 task_runner()->PostTask( | 104 message_loop()->PostTask( |
102 FROM_HERE, base::Bind(&BackgroundThread::DoCopyAndAssignArrowBase, | 105 FROM_HERE, |
103 object, &completion)); | 106 base::Bind(&BackgroundThread::DoCopyAndAssignArrowBase, |
| 107 object, &completion)); |
104 completion.Wait(); | 108 completion.Wait(); |
105 } | 109 } |
106 | 110 |
107 void DeleteArrow(Arrow* object) { | 111 void DeleteArrow(Arrow* object) { |
108 WaitableEvent completion(true, false); | 112 WaitableEvent completion(true, false); |
109 task_runner()->PostTask( | 113 message_loop()->PostTask( |
110 FROM_HERE, | 114 FROM_HERE, |
111 base::Bind(&BackgroundThread::DoDeleteArrow, object, &completion)); | 115 base::Bind(&BackgroundThread::DoDeleteArrow, object, &completion)); |
112 completion.Wait(); | 116 completion.Wait(); |
113 } | 117 } |
114 | 118 |
115 Target* DeRef(const Arrow* arrow) { | 119 Target* DeRef(const Arrow* arrow) { |
116 WaitableEvent completion(true, false); | 120 WaitableEvent completion(true, false); |
117 Target* result = NULL; | 121 Target* result = NULL; |
118 task_runner()->PostTask(FROM_HERE, base::Bind(&BackgroundThread::DoDeRef, | 122 message_loop()->PostTask( |
119 arrow, &result, &completion)); | 123 FROM_HERE, |
| 124 base::Bind(&BackgroundThread::DoDeRef, arrow, &result, &completion)); |
120 completion.Wait(); | 125 completion.Wait(); |
121 return result; | 126 return result; |
122 } | 127 } |
123 | 128 |
124 protected: | 129 protected: |
125 static void DoCreateArrowFromArrow(Arrow** arrow, | 130 static void DoCreateArrowFromArrow(Arrow** arrow, |
126 const Arrow* other, | 131 const Arrow* other, |
127 WaitableEvent* completion) { | 132 WaitableEvent* completion) { |
128 *arrow = new Arrow; | 133 *arrow = new Arrow; |
129 **arrow = *other; | 134 **arrow = *other; |
(...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
602 background.Start(); | 607 background.Start(); |
603 background.DeleteTarget(target.release()); | 608 background.DeleteTarget(target.release()); |
604 | 609 |
605 // Main thread attempts to dereference the target, violating thread binding. | 610 // Main thread attempts to dereference the target, violating thread binding. |
606 ASSERT_DEATH(arrow.target.get(), ""); | 611 ASSERT_DEATH(arrow.target.get(), ""); |
607 } | 612 } |
608 | 613 |
609 #endif | 614 #endif |
610 | 615 |
611 } // namespace base | 616 } // namespace base |
OLD | NEW |