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

Side by Side Diff: third_party/webrtc_overrides/webrtc/base/task_queue.cc

Issue 2291033002: Override the webrtc TaskQueue implementation. (Closed)
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
« no previous file with comments | « third_party/webrtc_overrides/webrtc/base/task_queue.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "third_party/webrtc_overrides/webrtc/base/task_queue.h"
12
13 #include "base/bind.h"
14 #include "base/lazy_instance.h"
15 #include "base/threading/thread.h"
16 #include "base/threading/thread_local.h"
17
18 namespace rtc {
19 namespace {
20
21 void RunTask(std::unique_ptr<QueuedTask> task) {
22 if (!task->Run()) {
tommi (sloooow) - chröme 2016/08/30 11:49:24 nit: remove {}
perkj_chrome 2016/08/30 12:19:10 Done.
23 task.release();
24 }
25 }
26
27 class PostAndReplyTask : public QueuedTask {
28 public:
29 PostAndReplyTask(
30 std::unique_ptr<QueuedTask> task,
31 std::unique_ptr<QueuedTask> reply,
32 const scoped_refptr<base::SingleThreadTaskRunner>& reply_task_runner)
33 : task_(std::move(task)),
34 reply_(std::move(reply)),
35 reply_task_runner_(reply_task_runner) {}
36
37 ~PostAndReplyTask() override {}
38
39 private:
40 bool Run() override {
41 if (!task_->Run())
42 task_.release();
43
44 reply_task_runner_->PostTask(FROM_HERE,
45 base::Bind(&RunTask, base::Passed(&reply_)));
46 return true;
47 }
48
49 std::unique_ptr<QueuedTask> task_;
50 std::unique_ptr<QueuedTask> reply_;
51 scoped_refptr<base::SingleThreadTaskRunner> reply_task_runner_;
52 };
53
54 } // namespace
55
56 // A lazily created thread local storage for quick access to a TaskQueue.
57 base::LazyInstance<base::ThreadLocalPointer<TaskQueue>>::Leaky lazy_tls_ptr =
58 LAZY_INSTANCE_INITIALIZER;
59
60 bool TaskQueue::IsCurrent() const {
61 return Current() == this;
62 }
63
64 class TaskQueue::WorkerThread : public base::Thread {
65 public:
66 WorkerThread(const char* queue_name, TaskQueue* queue);
67 ~WorkerThread();
tommi (sloooow) - chröme 2016/08/30 11:49:24 override?
perkj_chrome 2016/08/30 12:19:10 Done.
68
69 private:
70 virtual void Init() override;
71
72 TaskQueue* const queue_;
73 };
74
75 TaskQueue::WorkerThread::WorkerThread(const char* queue_name, TaskQueue* queue)
76 : base::Thread(queue_name), queue_(queue) {}
77
78 void TaskQueue::WorkerThread::Init() {
79 lazy_tls_ptr.Pointer()->Set(queue_);
80 }
81
82 TaskQueue::WorkerThread::~WorkerThread() {
83 DCHECK(!Thread::IsRunning());
84 }
85
86 TaskQueue::TaskQueue(const char* queue_name)
87 : thread_(
88 std::unique_ptr<WorkerThread>(new WorkerThread(queue_name, this))) {
89 DCHECK(queue_name);
90 bool result = thread_->Start();
91 CHECK(result);
92 }
93
94 TaskQueue::~TaskQueue() {
95 DCHECK(!IsCurrent());
96 thread_->Stop();
97 }
98
99 // static
100 TaskQueue* TaskQueue::Current() {
101 return lazy_tls_ptr.Pointer()->Get();
102 }
103
104 // static
105 bool TaskQueue::IsCurrent(const char* queue_name) {
106 TaskQueue* current = Current();
107 return current && current->thread_->thread_name().compare(queue_name) == 0;
108 }
109
110 void TaskQueue::PostTask(std::unique_ptr<QueuedTask> task) {
111 thread_->task_runner()->PostTask(FROM_HERE,
112 base::Bind(&RunTask, base::Passed(&task)));
113 }
114
115 void TaskQueue::PostDelayedTask(std::unique_ptr<QueuedTask> task,
116 uint32_t milliseconds) {
117 thread_->task_runner()->PostDelayedTask(
118 FROM_HERE, base::Bind(&RunTask, base::Passed(&task)),
119 base::TimeDelta::FromMilliseconds(milliseconds));
120 }
121
122 void TaskQueue::PostTaskAndReply(std::unique_ptr<QueuedTask> task,
123 std::unique_ptr<QueuedTask> reply,
124 TaskQueue* reply_queue) {
125 PostTask(std::unique_ptr<QueuedTask>(new PostAndReplyTask(
126 std::move(task), std::move(reply), reply_queue->thread_->task_runner())));
127 }
128
129 void TaskQueue::PostTaskAndReply(std::unique_ptr<QueuedTask> task,
130 std::unique_ptr<QueuedTask> reply) {
131 thread_->task_runner()->PostTaskAndReply(
132 FROM_HERE, base::Bind(&RunTask, base::Passed(&task)),
133 base::Bind(&RunTask, base::Passed(&reply)));
134 }
135
136 } // namespace rtc
OLDNEW
« no previous file with comments | « third_party/webrtc_overrides/webrtc/base/task_queue.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698