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/observer_list.h" | 5 #include "base/observer_list.h" |
6 #include "base/observer_list_threadsafe.h" | 6 #include "base/observer_list_threadsafe.h" |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 11 #include "base/location.h" |
11 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
12 #include "base/message_loop/message_loop.h" | |
13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "base/single_thread_task_runner.h" |
14 #include "base/threading/platform_thread.h" | 15 #include "base/threading/platform_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 class Foo { | 21 class Foo { |
21 public: | 22 public: |
22 virtual void Observe(int x) = 0; | 23 virtual void Observe(int x) = 0; |
23 virtual ~Foo() {} | 24 virtual ~Foo() {} |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 count_observes_(0), | 101 count_observes_(0), |
101 count_addtask_(0), | 102 count_addtask_(0), |
102 do_notifies_(notify), | 103 do_notifies_(notify), |
103 weak_factory_(this) { | 104 weak_factory_(this) { |
104 } | 105 } |
105 | 106 |
106 ~AddRemoveThread() override {} | 107 ~AddRemoveThread() override {} |
107 | 108 |
108 void ThreadMain() override { | 109 void ThreadMain() override { |
109 loop_ = new MessageLoop(); // Fire up a message loop. | 110 loop_ = new MessageLoop(); // Fire up a message loop. |
110 loop_->PostTask( | 111 loop_->task_runner()->PostTask( |
111 FROM_HERE, | 112 FROM_HERE, |
112 base::Bind(&AddRemoveThread::AddTask, weak_factory_.GetWeakPtr())); | 113 base::Bind(&AddRemoveThread::AddTask, weak_factory_.GetWeakPtr())); |
113 loop_->Run(); | 114 loop_->Run(); |
114 //LOG(ERROR) << "Loop 0x" << std::hex << loop_ << " done. " << | 115 //LOG(ERROR) << "Loop 0x" << std::hex << loop_ << " done. " << |
115 // count_observes_ << ", " << count_addtask_; | 116 // count_observes_ << ", " << count_addtask_; |
116 delete loop_; | 117 delete loop_; |
117 loop_ = reinterpret_cast<MessageLoop*>(0xdeadbeef); | 118 loop_ = reinterpret_cast<MessageLoop*>(0xdeadbeef); |
118 delete this; | 119 delete this; |
119 } | 120 } |
120 | 121 |
121 // This task just keeps posting to itself in an attempt | 122 // This task just keeps posting to itself in an attempt |
122 // to race with the notifier. | 123 // to race with the notifier. |
123 void AddTask() { | 124 void AddTask() { |
124 count_addtask_++; | 125 count_addtask_++; |
125 | 126 |
126 if ((Time::Now() - start_).InMilliseconds() > kThreadRunTime) { | 127 if ((Time::Now() - start_).InMilliseconds() > kThreadRunTime) { |
127 VLOG(1) << "DONE!"; | 128 VLOG(1) << "DONE!"; |
128 return; | 129 return; |
129 } | 130 } |
130 | 131 |
131 if (!in_list_) { | 132 if (!in_list_) { |
132 list_->AddObserver(this); | 133 list_->AddObserver(this); |
133 in_list_ = true; | 134 in_list_ = true; |
134 } | 135 } |
135 | 136 |
136 if (do_notifies_) { | 137 if (do_notifies_) { |
137 list_->Notify(FROM_HERE, &Foo::Observe, 10); | 138 list_->Notify(FROM_HERE, &Foo::Observe, 10); |
138 } | 139 } |
139 | 140 |
140 loop_->PostTask( | 141 loop_->task_runner()->PostTask( |
141 FROM_HERE, | 142 FROM_HERE, |
142 base::Bind(&AddRemoveThread::AddTask, weak_factory_.GetWeakPtr())); | 143 base::Bind(&AddRemoveThread::AddTask, weak_factory_.GetWeakPtr())); |
143 } | 144 } |
144 | 145 |
145 void Quit() { | 146 void Quit() { |
146 loop_->PostTask(FROM_HERE, MessageLoop::QuitWhenIdleClosure()); | 147 loop_->task_runner()->PostTask(FROM_HERE, |
| 148 MessageLoop::QuitWhenIdleClosure()); |
147 } | 149 } |
148 | 150 |
149 void Observe(int x) override { | 151 void Observe(int x) override { |
150 count_observes_++; | 152 count_observes_++; |
151 | 153 |
152 // If we're getting called after we removed ourselves from | 154 // If we're getting called after we removed ourselves from |
153 // the list, that is very bad! | 155 // the list, that is very bad! |
154 DCHECK(in_list_); | 156 DCHECK(in_list_); |
155 | 157 |
156 // This callback should fire on the appropriate thread | 158 // This callback should fire on the appropriate thread |
(...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
534 ListDestructor a(observer_list); | 536 ListDestructor a(observer_list); |
535 observer_list->AddObserver(&a); | 537 observer_list->AddObserver(&a); |
536 | 538 |
537 FOR_EACH_OBSERVER(Foo, *observer_list, Observe(0)); | 539 FOR_EACH_OBSERVER(Foo, *observer_list, Observe(0)); |
538 // If this test fails, there'll be Valgrind errors when this function goes out | 540 // If this test fails, there'll be Valgrind errors when this function goes out |
539 // of scope. | 541 // of scope. |
540 } | 542 } |
541 | 543 |
542 } // namespace | 544 } // namespace |
543 } // namespace base | 545 } // namespace base |
OLD | NEW |