blob: 4a33d48f8df50cd2560c2cd9aa517565299b11c5 [file] [log] [blame]
Danil Chapovalovb4c6d1e2019-01-21 13:52:59 +01001/*
2 * Copyright 2019 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#include "rtc_base/task_queue.h"
11
12#include "api/task_queue/global_task_queue_factory.h"
13#include "api/task_queue/task_queue_base.h"
14
15namespace rtc {
16
17TaskQueue::TaskQueue(const char* queue_name, Priority priority)
18 // For backward compatibility impl_ need to be scoped_refptr<Impl>,
19 // But this implementation treat impl_ as
20 // std::unique_ptr<webrtc::TaskQueueBase, webrtc::TaskQueueDeleter> abusing
21 // fact that both classes are wrappers around raw pointer.
22 : impl_(webrtc::GlobalTaskQueueFactory()
23 .CreateTaskQueue(queue_name, priority)
24 .release()) {
25 impl_->task_queue_ = this;
26}
27
28TaskQueue::~TaskQueue() {
29 // TODO(danilchap): change impl_ to webrtc::TaskQueueBase* when dependenent
30 // projects stop using link-injection to override task queue and thus do not
31 // rely on exact TaskQueue layout.
32 // There might running task that tries to rescheduler itself to the TaskQueue
33 // and not yet away TaskQueue destructor is called.
34 // Calling back to TaskQueue::PostTask need impl_ pointer still be valid, so
35 // Start the destruction first, ...
36 impl_->Delete();
37 // release the pointer later.
38 const_cast<rtc::scoped_refptr<Impl>&>(impl_).release();
39}
40
41// static
42TaskQueue* TaskQueue::Current() {
43 webrtc::TaskQueueBase* impl = webrtc::TaskQueueBase::Current();
44 if (impl == nullptr) {
45 return nullptr;
46 }
47 return impl->task_queue_;
48}
49
50bool TaskQueue::IsCurrent() const {
51 return Current() == this;
52}
53
54void TaskQueue::PostTask(std::unique_ptr<QueuedTask> task) {
55 return impl_->PostTask(std::move(task));
56}
57
58void TaskQueue::PostDelayedTask(std::unique_ptr<QueuedTask> task,
59 uint32_t milliseconds) {
60 return impl_->PostDelayedTask(std::move(task), milliseconds);
61}
62
63} // namespace rtc