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 <memory> | 7 #include <memory> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 | 62 |
63 // Helper class to create and destroy weak pointer copies | 63 // Helper class to create and destroy weak pointer copies |
64 // and delete objects on a background thread. | 64 // and delete objects on a background thread. |
65 class BackgroundThread : public Thread { | 65 class BackgroundThread : public Thread { |
66 public: | 66 public: |
67 BackgroundThread() : Thread("owner_thread") {} | 67 BackgroundThread() : Thread("owner_thread") {} |
68 | 68 |
69 ~BackgroundThread() override { Stop(); } | 69 ~BackgroundThread() override { Stop(); } |
70 | 70 |
71 void CreateArrowFromTarget(Arrow** arrow, Target* target) { | 71 void CreateArrowFromTarget(Arrow** arrow, Target* target) { |
72 WaitableEvent completion(true, false); | 72 WaitableEvent completion(WaitableEvent::ResetPolicy::MANUAL, |
| 73 WaitableEvent::InitialState::NOT_SIGNALED); |
73 task_runner()->PostTask( | 74 task_runner()->PostTask( |
74 FROM_HERE, base::Bind(&BackgroundThread::DoCreateArrowFromTarget, arrow, | 75 FROM_HERE, base::Bind(&BackgroundThread::DoCreateArrowFromTarget, arrow, |
75 target, &completion)); | 76 target, &completion)); |
76 completion.Wait(); | 77 completion.Wait(); |
77 } | 78 } |
78 | 79 |
79 void CreateArrowFromArrow(Arrow** arrow, const Arrow* other) { | 80 void CreateArrowFromArrow(Arrow** arrow, const Arrow* other) { |
80 WaitableEvent completion(true, false); | 81 WaitableEvent completion(WaitableEvent::ResetPolicy::MANUAL, |
| 82 WaitableEvent::InitialState::NOT_SIGNALED); |
81 task_runner()->PostTask( | 83 task_runner()->PostTask( |
82 FROM_HERE, base::Bind(&BackgroundThread::DoCreateArrowFromArrow, arrow, | 84 FROM_HERE, base::Bind(&BackgroundThread::DoCreateArrowFromArrow, arrow, |
83 other, &completion)); | 85 other, &completion)); |
84 completion.Wait(); | 86 completion.Wait(); |
85 } | 87 } |
86 | 88 |
87 void DeleteTarget(Target* object) { | 89 void DeleteTarget(Target* object) { |
88 WaitableEvent completion(true, false); | 90 WaitableEvent completion(WaitableEvent::ResetPolicy::MANUAL, |
| 91 WaitableEvent::InitialState::NOT_SIGNALED); |
89 task_runner()->PostTask( | 92 task_runner()->PostTask( |
90 FROM_HERE, | 93 FROM_HERE, |
91 base::Bind(&BackgroundThread::DoDeleteTarget, object, &completion)); | 94 base::Bind(&BackgroundThread::DoDeleteTarget, object, &completion)); |
92 completion.Wait(); | 95 completion.Wait(); |
93 } | 96 } |
94 | 97 |
95 void CopyAndAssignArrow(Arrow* object) { | 98 void CopyAndAssignArrow(Arrow* object) { |
96 WaitableEvent completion(true, false); | 99 WaitableEvent completion(WaitableEvent::ResetPolicy::MANUAL, |
| 100 WaitableEvent::InitialState::NOT_SIGNALED); |
97 task_runner()->PostTask( | 101 task_runner()->PostTask( |
98 FROM_HERE, base::Bind(&BackgroundThread::DoCopyAndAssignArrow, object, | 102 FROM_HERE, base::Bind(&BackgroundThread::DoCopyAndAssignArrow, object, |
99 &completion)); | 103 &completion)); |
100 completion.Wait(); | 104 completion.Wait(); |
101 } | 105 } |
102 | 106 |
103 void CopyAndAssignArrowBase(Arrow* object) { | 107 void CopyAndAssignArrowBase(Arrow* object) { |
104 WaitableEvent completion(true, false); | 108 WaitableEvent completion(WaitableEvent::ResetPolicy::MANUAL, |
| 109 WaitableEvent::InitialState::NOT_SIGNALED); |
105 task_runner()->PostTask( | 110 task_runner()->PostTask( |
106 FROM_HERE, base::Bind(&BackgroundThread::DoCopyAndAssignArrowBase, | 111 FROM_HERE, base::Bind(&BackgroundThread::DoCopyAndAssignArrowBase, |
107 object, &completion)); | 112 object, &completion)); |
108 completion.Wait(); | 113 completion.Wait(); |
109 } | 114 } |
110 | 115 |
111 void DeleteArrow(Arrow* object) { | 116 void DeleteArrow(Arrow* object) { |
112 WaitableEvent completion(true, false); | 117 WaitableEvent completion(WaitableEvent::ResetPolicy::MANUAL, |
| 118 WaitableEvent::InitialState::NOT_SIGNALED); |
113 task_runner()->PostTask( | 119 task_runner()->PostTask( |
114 FROM_HERE, | 120 FROM_HERE, |
115 base::Bind(&BackgroundThread::DoDeleteArrow, object, &completion)); | 121 base::Bind(&BackgroundThread::DoDeleteArrow, object, &completion)); |
116 completion.Wait(); | 122 completion.Wait(); |
117 } | 123 } |
118 | 124 |
119 Target* DeRef(const Arrow* arrow) { | 125 Target* DeRef(const Arrow* arrow) { |
120 WaitableEvent completion(true, false); | 126 WaitableEvent completion(WaitableEvent::ResetPolicy::MANUAL, |
| 127 WaitableEvent::InitialState::NOT_SIGNALED); |
121 Target* result = nullptr; | 128 Target* result = nullptr; |
122 task_runner()->PostTask(FROM_HERE, base::Bind(&BackgroundThread::DoDeRef, | 129 task_runner()->PostTask(FROM_HERE, base::Bind(&BackgroundThread::DoDeRef, |
123 arrow, &result, &completion)); | 130 arrow, &result, &completion)); |
124 completion.Wait(); | 131 completion.Wait(); |
125 return result; | 132 return result; |
126 } | 133 } |
127 | 134 |
128 protected: | 135 protected: |
129 static void DoCreateArrowFromArrow(Arrow** arrow, | 136 static void DoCreateArrowFromArrow(Arrow** arrow, |
130 const Arrow* other, | 137 const Arrow* other, |
(...skipping 511 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
642 background.Start(); | 649 background.Start(); |
643 background.DeleteTarget(target.release()); | 650 background.DeleteTarget(target.release()); |
644 | 651 |
645 // Main thread attempts to dereference the target, violating thread binding. | 652 // Main thread attempts to dereference the target, violating thread binding. |
646 ASSERT_DEATH(arrow.target.get(), ""); | 653 ASSERT_DEATH(arrow.target.get(), ""); |
647 } | 654 } |
648 | 655 |
649 #endif | 656 #endif |
650 | 657 |
651 } // namespace base | 658 } // namespace base |
OLD | NEW |