blob: 1c0ae297ee214f05d50b40a64415d54d4af63af6 [file] [log] [blame]
tommic06b1332016-05-14 11:31:40 -07001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_TASK_QUEUE_H_
12#define RTC_BASE_TASK_QUEUE_H_
tommic06b1332016-05-14 11:31:40 -070013
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <stdint.h>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020015#include <memory>
Danil Chapovalov6f09ae22017-10-12 14:39:25 +020016#include <utility>
tommic06b1332016-05-14 11:31:40 -070017
Karl Wiberg918f50c2018-07-05 11:40:33 +020018#include "absl/memory/memory.h"
Danil Chapovalov959e9b62019-01-14 14:29:18 +010019#include "api/task_queue/queued_task.h"
Danil Chapovalovd00405f2019-02-25 15:06:13 +010020#include "api/task_queue/task_queue_base.h"
21#include "api/task_queue/task_queue_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/constructor_magic.h"
Mirko Bonadei3d255302018-10-11 10:50:45 +020023#include "rtc_base/system/rtc_export.h"
Danil Chapovalov3b548dd2019-03-01 14:58:44 +010024#include "rtc_base/task_utils/to_queued_task.h"
Danil Chapovalov02fddf62018-02-12 12:41:16 +010025#include "rtc_base/thread_annotations.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020026
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020027namespace rtc {
28
Danil Chapovalov959e9b62019-01-14 14:29:18 +010029// TODO(danilchap): Remove the alias when all of webrtc is updated to use
30// webrtc::QueuedTask directly.
31using ::webrtc::QueuedTask;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020032
33// Implements a task queue that asynchronously executes tasks in a way that
34// guarantees that they're executed in FIFO order and that tasks never overlap.
35// Tasks may always execute on the same worker thread and they may not.
36// To DCHECK that tasks are executing on a known task queue, use IsCurrent().
37//
38// Here are some usage examples:
39//
40// 1) Asynchronously running a lambda:
41//
42// class MyClass {
43// ...
44// TaskQueue queue_("MyQueue");
45// };
46//
47// void MyClass::StartWork() {
48// queue_.PostTask([]() { Work(); });
49// ...
50//
Danil Chapovalov1aa75812019-03-05 11:11:35 +010051// 2) Posting a custom task on a timer. The task posts itself again after
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020052// every running:
53//
54// class TimerTask : public QueuedTask {
55// public:
56// TimerTask() {}
57// private:
58// bool Run() override {
59// ++count_;
60// TaskQueue::Current()->PostDelayedTask(
61// std::unique_ptr<QueuedTask>(this), 1000);
62// // Ownership has been transferred to the next occurance,
63// // so return false to prevent from being deleted now.
64// return false;
65// }
66// int count_ = 0;
67// };
68// ...
69// queue_.PostDelayedTask(
70// std::unique_ptr<QueuedTask>(new TimerTask()), 1000);
71//
72// For more examples, see task_queue_unittests.cc.
73//
74// A note on destruction:
75//
76// When a TaskQueue is deleted, pending tasks will not be executed but they will
77// be deleted. The deletion of tasks may happen asynchronously after the
78// TaskQueue itself has been deleted or it may happen synchronously while the
79// TaskQueue instance is being deleted. This may vary from one OS to the next
80// so assumptions about lifetimes of pending tasks should not be made.
Mirko Bonadei3d255302018-10-11 10:50:45 +020081class RTC_LOCKABLE RTC_EXPORT TaskQueue {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020082 public:
83 // TaskQueue priority levels. On some platforms these will map to thread
84 // priorities, on others such as Mac and iOS, GCD queue priorities.
Danil Chapovalovd00405f2019-02-25 15:06:13 +010085 using Priority = ::webrtc::TaskQueueFactory::Priority;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020086
Danil Chapovalovf3280e92019-02-28 10:39:04 +010087 explicit TaskQueue(std::unique_ptr<webrtc::TaskQueueBase,
88 webrtc::TaskQueueDeleter> task_queue);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020089 explicit TaskQueue(const char* queue_name,
90 Priority priority = Priority::NORMAL);
91 ~TaskQueue();
92
93 static TaskQueue* Current();
94
95 // Used for DCHECKing the current queue.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020096 bool IsCurrent() const;
97
Danil Chapovalovf3280e92019-02-28 10:39:04 +010098 // Returns non-owning pointer to the task queue implementation.
99 webrtc::TaskQueueBase* Get() { return impl_; }
100
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200101 // TODO(tommi): For better debuggability, implement RTC_FROM_HERE.
102
103 // Ownership of the task is passed to PostTask.
104 void PostTask(std::unique_ptr<QueuedTask> task);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200105
106 // Schedules a task to execute a specified number of milliseconds from when
107 // the call is made. The precision should be considered as "best effort"
108 // and in some cases, such as on Windows when all high precision timers have
109 // been used up, can be off by as much as 15 millseconds (although 8 would be
110 // more likely). This can be mitigated by limiting the use of delayed tasks.
111 void PostDelayedTask(std::unique_ptr<QueuedTask> task, uint32_t milliseconds);
112
eladalonffe2e142017-08-31 04:36:05 -0700113 // std::enable_if is used here to make sure that calls to PostTask() with
114 // std::unique_ptr<SomeClassDerivedFromQueuedTask> would not end up being
115 // caught by this template.
116 template <class Closure,
Danil Chapovalov6f09ae22017-10-12 14:39:25 +0200117 typename std::enable_if<!std::is_convertible<
118 Closure,
119 std::unique_ptr<QueuedTask>>::value>::type* = nullptr>
120 void PostTask(Closure&& closure) {
Danil Chapovalov1aa75812019-03-05 11:11:35 +0100121 PostTask(webrtc::ToQueuedTask(std::forward<Closure>(closure)));
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200122 }
123
124 // See documentation above for performance expectations.
Danil Chapovalov6f09ae22017-10-12 14:39:25 +0200125 template <class Closure,
126 typename std::enable_if<!std::is_convertible<
127 Closure,
128 std::unique_ptr<QueuedTask>>::value>::type* = nullptr>
129 void PostDelayedTask(Closure&& closure, uint32_t milliseconds) {
Danil Chapovalov1aa75812019-03-05 11:11:35 +0100130 PostDelayedTask(webrtc::ToQueuedTask(std::forward<Closure>(closure)),
131 milliseconds);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200132 }
133
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200134 private:
Danil Chapovalovd00405f2019-02-25 15:06:13 +0100135 webrtc::TaskQueueBase* const impl_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200136
137 RTC_DISALLOW_COPY_AND_ASSIGN(TaskQueue);
138};
139
140} // namespace rtc
tommic06b1332016-05-14 11:31:40 -0700141
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200142#endif // RTC_BASE_TASK_QUEUE_H_