| OLD | NEW |
| (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()) | |
| 23 task.release(); | |
| 24 } | |
| 25 | |
| 26 class PostAndReplyTask : public QueuedTask { | |
| 27 public: | |
| 28 PostAndReplyTask( | |
| 29 std::unique_ptr<QueuedTask> task, | |
| 30 std::unique_ptr<QueuedTask> reply, | |
| 31 const scoped_refptr<base::SingleThreadTaskRunner>& reply_task_runner) | |
| 32 : task_(std::move(task)), | |
| 33 reply_(std::move(reply)), | |
| 34 reply_task_runner_(reply_task_runner) {} | |
| 35 | |
| 36 ~PostAndReplyTask() override {} | |
| 37 | |
| 38 private: | |
| 39 bool Run() override { | |
| 40 if (!task_->Run()) | |
| 41 task_.release(); | |
| 42 | |
| 43 reply_task_runner_->PostTask(FROM_HERE, | |
| 44 base::Bind(&RunTask, base::Passed(&reply_))); | |
| 45 return true; | |
| 46 } | |
| 47 | |
| 48 std::unique_ptr<QueuedTask> task_; | |
| 49 std::unique_ptr<QueuedTask> reply_; | |
| 50 scoped_refptr<base::SingleThreadTaskRunner> reply_task_runner_; | |
| 51 }; | |
| 52 | |
| 53 // A lazily created thread local storage for quick access to a TaskQueue. | |
| 54 base::LazyInstance<base::ThreadLocalPointer<TaskQueue>>::Leaky lazy_tls_ptr = | |
| 55 LAZY_INSTANCE_INITIALIZER; | |
| 56 | |
| 57 } // namespace | |
| 58 | |
| 59 bool TaskQueue::IsCurrent() const { | |
| 60 return Current() == this; | |
| 61 } | |
| 62 | |
| 63 class TaskQueue::WorkerThread : public base::Thread { | |
| 64 public: | |
| 65 WorkerThread(const char* queue_name, TaskQueue* queue); | |
| 66 ~WorkerThread() override; | |
| 67 | |
| 68 private: | |
| 69 virtual void Init() override; | |
| 70 | |
| 71 TaskQueue* const queue_; | |
| 72 }; | |
| 73 | |
| 74 TaskQueue::WorkerThread::WorkerThread(const char* queue_name, TaskQueue* queue) | |
| 75 : base::Thread(queue_name), queue_(queue) {} | |
| 76 | |
| 77 void TaskQueue::WorkerThread::Init() { | |
| 78 lazy_tls_ptr.Pointer()->Set(queue_); | |
| 79 } | |
| 80 | |
| 81 TaskQueue::WorkerThread::~WorkerThread() { | |
| 82 DCHECK(!Thread::IsRunning()); | |
| 83 } | |
| 84 | |
| 85 TaskQueue::TaskQueue(const char* queue_name) | |
| 86 : thread_( | |
| 87 std::unique_ptr<WorkerThread>(new WorkerThread(queue_name, this))) { | |
| 88 DCHECK(queue_name); | |
| 89 bool result = thread_->Start(); | |
| 90 CHECK(result); | |
| 91 } | |
| 92 | |
| 93 TaskQueue::~TaskQueue() { | |
| 94 DCHECK(!IsCurrent()); | |
| 95 thread_->Stop(); | |
| 96 } | |
| 97 | |
| 98 // static | |
| 99 TaskQueue* TaskQueue::Current() { | |
| 100 return lazy_tls_ptr.Pointer()->Get(); | |
| 101 } | |
| 102 | |
| 103 // static | |
| 104 bool TaskQueue::IsCurrent(const char* queue_name) { | |
| 105 TaskQueue* current = Current(); | |
| 106 return current && current->thread_->thread_name().compare(queue_name) == 0; | |
| 107 } | |
| 108 | |
| 109 void TaskQueue::PostTask(std::unique_ptr<QueuedTask> task) { | |
| 110 thread_->task_runner()->PostTask(FROM_HERE, | |
| 111 base::Bind(&RunTask, base::Passed(&task))); | |
| 112 } | |
| 113 | |
| 114 void TaskQueue::PostDelayedTask(std::unique_ptr<QueuedTask> task, | |
| 115 uint32_t milliseconds) { | |
| 116 thread_->task_runner()->PostDelayedTask( | |
| 117 FROM_HERE, base::Bind(&RunTask, base::Passed(&task)), | |
| 118 base::TimeDelta::FromMilliseconds(milliseconds)); | |
| 119 } | |
| 120 | |
| 121 void TaskQueue::PostTaskAndReply(std::unique_ptr<QueuedTask> task, | |
| 122 std::unique_ptr<QueuedTask> reply, | |
| 123 TaskQueue* reply_queue) { | |
| 124 PostTask(std::unique_ptr<QueuedTask>(new PostAndReplyTask( | |
| 125 std::move(task), std::move(reply), reply_queue->thread_->task_runner()))); | |
| 126 } | |
| 127 | |
| 128 void TaskQueue::PostTaskAndReply(std::unique_ptr<QueuedTask> task, | |
| 129 std::unique_ptr<QueuedTask> reply) { | |
| 130 thread_->task_runner()->PostTaskAndReply( | |
| 131 FROM_HERE, base::Bind(&RunTask, base::Passed(&task)), | |
| 132 base::Bind(&RunTask, base::Passed(&reply))); | |
| 133 } | |
| 134 | |
| 135 } // namespace rtc | |
| OLD | NEW |