Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(510)

Side by Side Diff: base/memory/weak_ptr_unittest.cc

Issue 2791243002: Rewrite base::Bind into base::BindOnce on trivial cases in base (Closed)
Patch Set: rebase Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 14 matching lines...) Expand all
25 25
26 template <class T> 26 template <class T>
27 class OffThreadObjectCreator { 27 class OffThreadObjectCreator {
28 public: 28 public:
29 static T* NewObject() { 29 static T* NewObject() {
30 T* result; 30 T* result;
31 { 31 {
32 Thread creator_thread("creator_thread"); 32 Thread creator_thread("creator_thread");
33 creator_thread.Start(); 33 creator_thread.Start();
34 creator_thread.task_runner()->PostTask( 34 creator_thread.task_runner()->PostTask(
35 FROM_HERE, base::Bind(OffThreadObjectCreator::CreateObject, &result)); 35 FROM_HERE,
36 base::BindOnce(OffThreadObjectCreator::CreateObject, &result));
36 } 37 }
37 DCHECK(result); // We synchronized on thread destruction above. 38 DCHECK(result); // We synchronized on thread destruction above.
38 return result; 39 return result;
39 } 40 }
40 private: 41 private:
41 static void CreateObject(T** result) { 42 static void CreateObject(T** result) {
42 *result = new T; 43 *result = new T;
43 } 44 }
44 }; 45 };
45 46
(...skipping 20 matching lines...) Expand all
66 class BackgroundThread : public Thread { 67 class BackgroundThread : public Thread {
67 public: 68 public:
68 BackgroundThread() : Thread("owner_thread") {} 69 BackgroundThread() : Thread("owner_thread") {}
69 70
70 ~BackgroundThread() override { Stop(); } 71 ~BackgroundThread() override { Stop(); }
71 72
72 void CreateArrowFromTarget(Arrow** arrow, Target* target) { 73 void CreateArrowFromTarget(Arrow** arrow, Target* target) {
73 WaitableEvent completion(WaitableEvent::ResetPolicy::MANUAL, 74 WaitableEvent completion(WaitableEvent::ResetPolicy::MANUAL,
74 WaitableEvent::InitialState::NOT_SIGNALED); 75 WaitableEvent::InitialState::NOT_SIGNALED);
75 task_runner()->PostTask( 76 task_runner()->PostTask(
76 FROM_HERE, base::Bind(&BackgroundThread::DoCreateArrowFromTarget, arrow, 77 FROM_HERE, base::BindOnce(&BackgroundThread::DoCreateArrowFromTarget,
77 target, &completion)); 78 arrow, target, &completion));
78 completion.Wait(); 79 completion.Wait();
79 } 80 }
80 81
81 void CreateArrowFromArrow(Arrow** arrow, const Arrow* other) { 82 void CreateArrowFromArrow(Arrow** arrow, const Arrow* other) {
82 WaitableEvent completion(WaitableEvent::ResetPolicy::MANUAL, 83 WaitableEvent completion(WaitableEvent::ResetPolicy::MANUAL,
83 WaitableEvent::InitialState::NOT_SIGNALED); 84 WaitableEvent::InitialState::NOT_SIGNALED);
84 task_runner()->PostTask( 85 task_runner()->PostTask(
85 FROM_HERE, base::Bind(&BackgroundThread::DoCreateArrowFromArrow, arrow, 86 FROM_HERE, base::BindOnce(&BackgroundThread::DoCreateArrowFromArrow,
86 other, &completion)); 87 arrow, other, &completion));
87 completion.Wait(); 88 completion.Wait();
88 } 89 }
89 90
90 void DeleteTarget(Target* object) { 91 void DeleteTarget(Target* object) {
91 WaitableEvent completion(WaitableEvent::ResetPolicy::MANUAL, 92 WaitableEvent completion(WaitableEvent::ResetPolicy::MANUAL,
92 WaitableEvent::InitialState::NOT_SIGNALED); 93 WaitableEvent::InitialState::NOT_SIGNALED);
93 task_runner()->PostTask( 94 task_runner()->PostTask(
94 FROM_HERE, 95 FROM_HERE,
95 base::Bind(&BackgroundThread::DoDeleteTarget, object, &completion)); 96 base::BindOnce(&BackgroundThread::DoDeleteTarget, object, &completion));
96 completion.Wait(); 97 completion.Wait();
97 } 98 }
98 99
99 void CopyAndAssignArrow(Arrow* object) { 100 void CopyAndAssignArrow(Arrow* object) {
100 WaitableEvent completion(WaitableEvent::ResetPolicy::MANUAL, 101 WaitableEvent completion(WaitableEvent::ResetPolicy::MANUAL,
101 WaitableEvent::InitialState::NOT_SIGNALED); 102 WaitableEvent::InitialState::NOT_SIGNALED);
102 task_runner()->PostTask( 103 task_runner()->PostTask(
103 FROM_HERE, base::Bind(&BackgroundThread::DoCopyAndAssignArrow, object, 104 FROM_HERE, base::BindOnce(&BackgroundThread::DoCopyAndAssignArrow,
104 &completion)); 105 object, &completion));
105 completion.Wait(); 106 completion.Wait();
106 } 107 }
107 108
108 void CopyAndAssignArrowBase(Arrow* object) { 109 void CopyAndAssignArrowBase(Arrow* object) {
109 WaitableEvent completion(WaitableEvent::ResetPolicy::MANUAL, 110 WaitableEvent completion(WaitableEvent::ResetPolicy::MANUAL,
110 WaitableEvent::InitialState::NOT_SIGNALED); 111 WaitableEvent::InitialState::NOT_SIGNALED);
111 task_runner()->PostTask( 112 task_runner()->PostTask(
112 FROM_HERE, base::Bind(&BackgroundThread::DoCopyAndAssignArrowBase, 113 FROM_HERE, base::BindOnce(&BackgroundThread::DoCopyAndAssignArrowBase,
113 object, &completion)); 114 object, &completion));
114 completion.Wait(); 115 completion.Wait();
115 } 116 }
116 117
117 void DeleteArrow(Arrow* object) { 118 void DeleteArrow(Arrow* object) {
118 WaitableEvent completion(WaitableEvent::ResetPolicy::MANUAL, 119 WaitableEvent completion(WaitableEvent::ResetPolicy::MANUAL,
119 WaitableEvent::InitialState::NOT_SIGNALED); 120 WaitableEvent::InitialState::NOT_SIGNALED);
120 task_runner()->PostTask( 121 task_runner()->PostTask(
121 FROM_HERE, 122 FROM_HERE,
122 base::Bind(&BackgroundThread::DoDeleteArrow, object, &completion)); 123 base::BindOnce(&BackgroundThread::DoDeleteArrow, object, &completion));
123 completion.Wait(); 124 completion.Wait();
124 } 125 }
125 126
126 Target* DeRef(const Arrow* arrow) { 127 Target* DeRef(const Arrow* arrow) {
127 WaitableEvent completion(WaitableEvent::ResetPolicy::MANUAL, 128 WaitableEvent completion(WaitableEvent::ResetPolicy::MANUAL,
128 WaitableEvent::InitialState::NOT_SIGNALED); 129 WaitableEvent::InitialState::NOT_SIGNALED);
129 Target* result = nullptr; 130 Target* result = nullptr;
130 task_runner()->PostTask(FROM_HERE, base::Bind(&BackgroundThread::DoDeRef, 131 task_runner()->PostTask(
131 arrow, &result, &completion)); 132 FROM_HERE, base::BindOnce(&BackgroundThread::DoDeRef, arrow, &result,
133 &completion));
132 completion.Wait(); 134 completion.Wait();
133 return result; 135 return result;
134 } 136 }
135 137
136 protected: 138 protected:
137 static void DoCreateArrowFromArrow(Arrow** arrow, 139 static void DoCreateArrowFromArrow(Arrow** arrow,
138 const Arrow* other, 140 const Arrow* other,
139 WaitableEvent* completion) { 141 WaitableEvent* completion) {
140 *arrow = new Arrow; 142 *arrow = new Arrow;
141 **arrow = *other; 143 **arrow = *other;
(...skipping 527 matching lines...) Expand 10 before | Expand all | Expand 10 after
669 // Background thread tries to delete target, binding the object to the thread. 671 // Background thread tries to delete target, binding the object to the thread.
670 BackgroundThread background; 672 BackgroundThread background;
671 background.Start(); 673 background.Start();
672 background.DeleteTarget(target.release()); 674 background.DeleteTarget(target.release());
673 675
674 // Main thread attempts to dereference the target, violating thread binding. 676 // Main thread attempts to dereference the target, violating thread binding.
675 ASSERT_DCHECK_DEATH(arrow.target.get()); 677 ASSERT_DCHECK_DEATH(arrow.target.get());
676 } 678 }
677 679
678 } // namespace base 680 } // namespace base
OLDNEW
« no previous file with comments | « base/files/important_file_writer_unittest.cc ('k') | base/message_loop/message_loop_task_runner_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698