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

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

Issue 2122543002: Replace Closure in TaskRunner::PostTask with OneShotCallback (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@07_oneshot
Patch Set: fix Created 4 years, 3 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 "base/bind.h" 5 #include "base/bind.h"
6 #include "base/callback.h" 6 #include "base/callback.h"
7 #include "base/message_loop/message_loop.h" 7 #include "base/message_loop/message_loop.h"
8 #include "base/single_thread_task_runner.h" 8 #include "base/single_thread_task_runner.h"
9 #include "base/synchronization/lock.h" 9 #include "base/synchronization/lock.h"
10 #include "base/synchronization/waitable_event.h" 10 #include "base/synchronization/waitable_event.h"
(...skipping 13 matching lines...) Expand all
24 24
25 class TestTaskRunner : public base::SingleThreadTaskRunner { 25 class TestTaskRunner : public base::SingleThreadTaskRunner {
26 public: 26 public:
27 TestTaskRunner() 27 TestTaskRunner()
28 : thread_id_(base::PlatformThread::CurrentRef()), 28 : thread_id_(base::PlatformThread::CurrentRef()),
29 quit_called_(false), 29 quit_called_(false),
30 task_ready_(base::WaitableEvent::ResetPolicy::AUTOMATIC, 30 task_ready_(base::WaitableEvent::ResetPolicy::AUTOMATIC,
31 base::WaitableEvent::InitialState::NOT_SIGNALED) {} 31 base::WaitableEvent::InitialState::NOT_SIGNALED) {}
32 32
33 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, 33 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here,
34 const base::Closure& task, 34 base::OnceClosure task,
35 base::TimeDelta delay) override { 35 base::TimeDelta delay) override {
36 NOTREACHED(); 36 NOTREACHED();
37 return false; 37 return false;
38 } 38 }
39 39
40 bool PostDelayedTask(const tracked_objects::Location& from_here, 40 bool PostDelayedTask(const tracked_objects::Location& from_here,
41 const base::Closure& task, 41 base::OnceClosure task,
42 base::TimeDelta delay) override { 42 base::TimeDelta delay) override {
43 { 43 {
44 base::AutoLock locker(lock_); 44 base::AutoLock locker(lock_);
45 tasks_.push(task); 45 tasks_.push(std::move(task));
46 } 46 }
47 task_ready_.Signal(); 47 task_ready_.Signal();
48 return true; 48 return true;
49 } 49 }
50 bool RunsTasksOnCurrentThread() const override { 50 bool RunsTasksOnCurrentThread() const override {
51 return base::PlatformThread::CurrentRef() == thread_id_; 51 return base::PlatformThread::CurrentRef() == thread_id_;
52 } 52 }
53 53
54 // Only quits when Quit() is called. 54 // Only quits when Quit() is called.
55 void Run() { 55 void Run() {
56 DCHECK(RunsTasksOnCurrentThread()); 56 DCHECK(RunsTasksOnCurrentThread());
57 quit_called_ = false; 57 quit_called_ = false;
58 58
59 while (true) { 59 while (true) {
60 { 60 {
61 base::AutoLock locker(lock_); 61 base::AutoLock locker(lock_);
62 while (!tasks_.empty()) { 62 while (!tasks_.empty()) {
63 auto task = tasks_.front(); 63 auto task = std::move(tasks_.front());
64 tasks_.pop(); 64 tasks_.pop();
65 65
66 { 66 {
67 base::AutoUnlock unlocker(lock_); 67 base::AutoUnlock unlocker(lock_);
68 task.Run(); 68 std::move(task).Run();
69 if (quit_called_) 69 if (quit_called_)
70 return; 70 return;
71 } 71 }
72 } 72 }
73 } 73 }
74 task_ready_.Wait(); 74 task_ready_.Wait();
75 } 75 }
76 } 76 }
77 77
78 void Quit() { 78 void Quit() {
79 DCHECK(RunsTasksOnCurrentThread()); 79 DCHECK(RunsTasksOnCurrentThread());
80 quit_called_ = true; 80 quit_called_ = true;
81 } 81 }
82 82
83 // Waits until one task is ready and runs it. 83 // Waits until one task is ready and runs it.
84 void RunOneTask() { 84 void RunOneTask() {
85 DCHECK(RunsTasksOnCurrentThread()); 85 DCHECK(RunsTasksOnCurrentThread());
86 86
87 while (true) { 87 while (true) {
88 { 88 {
89 base::AutoLock locker(lock_); 89 base::AutoLock locker(lock_);
90 if (!tasks_.empty()) { 90 if (!tasks_.empty()) {
91 auto task = tasks_.front(); 91 auto task = std::move(tasks_.front());
92 tasks_.pop(); 92 tasks_.pop();
93 93
94 { 94 {
95 base::AutoUnlock unlocker(lock_); 95 base::AutoUnlock unlocker(lock_);
96 task.Run(); 96 std::move(task).Run();
97 return; 97 return;
98 } 98 }
99 } 99 }
100 } 100 }
101 task_ready_.Wait(); 101 task_ready_.Wait();
102 } 102 }
103 } 103 }
104 104
105 private: 105 private:
106 ~TestTaskRunner() override {} 106 ~TestTaskRunner() override {}
107 107
108 const base::PlatformThreadRef thread_id_; 108 const base::PlatformThreadRef thread_id_;
109 bool quit_called_; 109 bool quit_called_;
110 base::WaitableEvent task_ready_; 110 base::WaitableEvent task_ready_;
111 111
112 // Protect |tasks_|. 112 // Protect |tasks_|.
113 base::Lock lock_; 113 base::Lock lock_;
114 std::queue<base::Closure> tasks_; 114 std::queue<base::OnceClosure> tasks_;
115 115
116 DISALLOW_COPY_AND_ASSIGN(TestTaskRunner); 116 DISALLOW_COPY_AND_ASSIGN(TestTaskRunner);
117 }; 117 };
118 118
119 template <typename BindingType, typename RequestType> 119 template <typename BindingType, typename RequestType>
120 class IntegerSenderImpl : public IntegerSender { 120 class IntegerSenderImpl : public IntegerSender {
121 public: 121 public:
122 IntegerSenderImpl(RequestType request, 122 IntegerSenderImpl(RequestType request,
123 scoped_refptr<base::SingleThreadTaskRunner> runner) 123 scoped_refptr<base::SingleThreadTaskRunner> runner)
124 : binding_(this, std::move(request), std::move(runner)) {} 124 : binding_(this, std::move(request), std::move(runner)) {}
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after
386 EXPECT_TRUE(sender_impl_error); 386 EXPECT_TRUE(sender_impl_error);
387 connection_ptr_task_runner_->Run(); 387 connection_ptr_task_runner_->Run();
388 EXPECT_TRUE(connection_ptr_error); 388 EXPECT_TRUE(connection_ptr_error);
389 sender_ptr_task_runner_->Run(); 389 sender_ptr_task_runner_->Run();
390 EXPECT_TRUE(sender_ptr_error); 390 EXPECT_TRUE(sender_ptr_error);
391 } 391 }
392 392
393 } // namespace 393 } // namespace
394 } // namespace test 394 } // namespace test
395 } // namespace mojo 395 } // namespace mojo
OLDNEW
« no previous file with comments | « media/cast/test/skewed_single_thread_task_runner.cc ('k') | net/quic/test_tools/test_task_runner.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698