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