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