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

Side by Side Diff: mojo/public/cpp/bindings/tests/bind_task_runner_unittest.cc

Issue 2726523002: Pass Callback to TaskRunner by value and consume it on invocation (1) (Closed)
Patch Set: s/base::ResetAndReturn/std::move/ Created 3 years, 9 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 <utility>
6
5 #include "base/bind.h" 7 #include "base/bind.h"
6 #include "base/callback.h" 8 #include "base/callback.h"
7 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
8 #include "base/single_thread_task_runner.h" 10 #include "base/single_thread_task_runner.h"
9 #include "base/synchronization/lock.h" 11 #include "base/synchronization/lock.h"
10 #include "base/synchronization/waitable_event.h" 12 #include "base/synchronization/waitable_event.h"
11 #include "base/threading/platform_thread.h" 13 #include "base/threading/platform_thread.h"
12 #include "mojo/public/cpp/bindings/associated_binding.h" 14 #include "mojo/public/cpp/bindings/associated_binding.h"
13 #include "mojo/public/cpp/bindings/associated_interface_ptr.h" 15 #include "mojo/public/cpp/bindings/associated_interface_ptr.h"
14 #include "mojo/public/cpp/bindings/associated_interface_ptr_info.h" 16 #include "mojo/public/cpp/bindings/associated_interface_ptr_info.h"
15 #include "mojo/public/cpp/bindings/associated_interface_request.h" 17 #include "mojo/public/cpp/bindings/associated_interface_request.h"
16 #include "mojo/public/cpp/bindings/binding.h" 18 #include "mojo/public/cpp/bindings/binding.h"
17 #include "mojo/public/interfaces/bindings/tests/test_associated_interfaces.mojom .h" 19 #include "mojo/public/interfaces/bindings/tests/test_associated_interfaces.mojom .h"
18 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
19 21
20 namespace mojo { 22 namespace mojo {
21 namespace test { 23 namespace test {
22 namespace { 24 namespace {
23 25
24 class TestTaskRunner : public base::SingleThreadTaskRunner { 26 class TestTaskRunner : public base::SingleThreadTaskRunner {
25 public: 27 public:
26 TestTaskRunner() 28 TestTaskRunner()
27 : thread_id_(base::PlatformThread::CurrentRef()), 29 : thread_id_(base::PlatformThread::CurrentRef()),
28 quit_called_(false), 30 quit_called_(false),
29 task_ready_(base::WaitableEvent::ResetPolicy::AUTOMATIC, 31 task_ready_(base::WaitableEvent::ResetPolicy::AUTOMATIC,
30 base::WaitableEvent::InitialState::NOT_SIGNALED) {} 32 base::WaitableEvent::InitialState::NOT_SIGNALED) {}
31 33
32 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, 34 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here,
33 const base::Closure& task, 35 base::Closure task,
34 base::TimeDelta delay) override { 36 base::TimeDelta delay) override {
35 NOTREACHED(); 37 NOTREACHED();
36 return false; 38 return false;
37 } 39 }
38 40
39 bool PostDelayedTask(const tracked_objects::Location& from_here, 41 bool PostDelayedTask(const tracked_objects::Location& from_here,
40 const base::Closure& task, 42 base::Closure task,
41 base::TimeDelta delay) override { 43 base::TimeDelta delay) override {
42 { 44 {
43 base::AutoLock locker(lock_); 45 base::AutoLock locker(lock_);
44 tasks_.push(task); 46 tasks_.push(std::move(task));
45 } 47 }
46 task_ready_.Signal(); 48 task_ready_.Signal();
47 return true; 49 return true;
48 } 50 }
49 bool RunsTasksOnCurrentThread() const override { 51 bool RunsTasksOnCurrentThread() const override {
50 return base::PlatformThread::CurrentRef() == thread_id_; 52 return base::PlatformThread::CurrentRef() == thread_id_;
51 } 53 }
52 54
53 // Only quits when Quit() is called. 55 // Only quits when Quit() is called.
54 void Run() { 56 void Run() {
55 DCHECK(RunsTasksOnCurrentThread()); 57 DCHECK(RunsTasksOnCurrentThread());
56 quit_called_ = false; 58 quit_called_ = false;
57 59
58 while (true) { 60 while (true) {
59 { 61 {
60 base::AutoLock locker(lock_); 62 base::AutoLock locker(lock_);
61 while (!tasks_.empty()) { 63 while (!tasks_.empty()) {
62 auto task = tasks_.front(); 64 auto task = std::move(tasks_.front());
63 tasks_.pop(); 65 tasks_.pop();
64 66
65 { 67 {
66 base::AutoUnlock unlocker(lock_); 68 base::AutoUnlock unlocker(lock_);
67 task.Run(); 69 std::move(task).Run();
sky 2017/03/22 17:34:11 Is the std::move(task) really necessary?
68 if (quit_called_) 70 if (quit_called_)
69 return; 71 return;
70 } 72 }
71 } 73 }
72 } 74 }
73 task_ready_.Wait(); 75 task_ready_.Wait();
74 } 76 }
75 } 77 }
76 78
77 void Quit() { 79 void Quit() {
78 DCHECK(RunsTasksOnCurrentThread()); 80 DCHECK(RunsTasksOnCurrentThread());
79 quit_called_ = true; 81 quit_called_ = true;
80 } 82 }
81 83
82 // Waits until one task is ready and runs it. 84 // Waits until one task is ready and runs it.
83 void RunOneTask() { 85 void RunOneTask() {
84 DCHECK(RunsTasksOnCurrentThread()); 86 DCHECK(RunsTasksOnCurrentThread());
85 87
86 while (true) { 88 while (true) {
87 { 89 {
88 base::AutoLock locker(lock_); 90 base::AutoLock locker(lock_);
89 if (!tasks_.empty()) { 91 if (!tasks_.empty()) {
90 auto task = tasks_.front(); 92 auto task = std::move(tasks_.front());
91 tasks_.pop(); 93 tasks_.pop();
92 94
93 { 95 {
94 base::AutoUnlock unlocker(lock_); 96 base::AutoUnlock unlocker(lock_);
95 task.Run(); 97 std::move(task).Run();
sky 2017/03/22 17:34:11 Similar comment about the need for std::move(task)
96 return; 98 return;
97 } 99 }
98 } 100 }
99 } 101 }
100 task_ready_.Wait(); 102 task_ready_.Wait();
101 } 103 }
102 } 104 }
103 105
104 private: 106 private:
105 ~TestTaskRunner() override {} 107 ~TestTaskRunner() override {}
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after
384 EXPECT_TRUE(sender_impl_error); 386 EXPECT_TRUE(sender_impl_error);
385 connection_ptr_task_runner_->Run(); 387 connection_ptr_task_runner_->Run();
386 EXPECT_TRUE(connection_ptr_error); 388 EXPECT_TRUE(connection_ptr_error);
387 sender_ptr_task_runner_->Run(); 389 sender_ptr_task_runner_->Run();
388 EXPECT_TRUE(sender_ptr_error); 390 EXPECT_TRUE(sender_ptr_error);
389 } 391 }
390 392
391 } // namespace 393 } // namespace
392 } // namespace test 394 } // namespace test
393 } // namespace mojo 395 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698