blob: 3bc1e1fdc3d6cb6fc1bcac36187ffa3a80709cd2 [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>
eladalonffe2e142017-08-31 04:36:05 -070016#include <type_traits>
Danil Chapovalov6f09ae22017-10-12 14:39:25 +020017#include <utility>
tommic06b1332016-05-14 11:31:40 -070018
Karl Wiberg918f50c2018-07-05 11:40:33 +020019#include "absl/memory/memory.h"
Danil Chapovalov959e9b62019-01-14 14:29:18 +010020#include "api/task_queue/queued_task.h"
Danil Chapovalovd00405f2019-02-25 15:06:13 +010021#include "api/task_queue/task_queue_base.h"
22#include "api/task_queue/task_queue_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "rtc_base/constructor_magic.h"
Mirko Bonadei3d255302018-10-11 10:50:45 +020024#include "rtc_base/system/rtc_export.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// Simple implementation of QueuedTask for use with rtc::Bind and lambdas.
34template <class Closure>
35class ClosureTask : public QueuedTask {
36 public:
Danil Chapovalov6f09ae22017-10-12 14:39:25 +020037 explicit ClosureTask(Closure&& closure)
38 : closure_(std::forward<Closure>(closure)) {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020039
40 private:
41 bool Run() override {
42 closure_();
43 return true;
44 }
45
Danil Chapovalov6f09ae22017-10-12 14:39:25 +020046 typename std::remove_const<
47 typename std::remove_reference<Closure>::type>::type closure_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020048};
49
50// Extends ClosureTask to also allow specifying cleanup code.
51// This is useful when using lambdas if guaranteeing cleanup, even if a task
52// was dropped (queue is too full), is required.
53template <class Closure, class Cleanup>
54class ClosureTaskWithCleanup : public ClosureTask<Closure> {
55 public:
Danil Chapovalov6f09ae22017-10-12 14:39:25 +020056 ClosureTaskWithCleanup(Closure&& closure, Cleanup&& cleanup)
57 : ClosureTask<Closure>(std::forward<Closure>(closure)),
58 cleanup_(std::forward<Cleanup>(cleanup)) {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020059 ~ClosureTaskWithCleanup() { cleanup_(); }
60
61 private:
Danil Chapovalov6f09ae22017-10-12 14:39:25 +020062 typename std::remove_const<
63 typename std::remove_reference<Cleanup>::type>::type cleanup_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020064};
65
66// Convenience function to construct closures that can be passed directly
67// to methods that support std::unique_ptr<QueuedTask> but not template
68// based parameters.
69template <class Closure>
Danil Chapovalov6f09ae22017-10-12 14:39:25 +020070static std::unique_ptr<QueuedTask> NewClosure(Closure&& closure) {
Karl Wiberg918f50c2018-07-05 11:40:33 +020071 return absl::make_unique<ClosureTask<Closure>>(
72 std::forward<Closure>(closure));
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020073}
74
75template <class Closure, class Cleanup>
Danil Chapovalov6f09ae22017-10-12 14:39:25 +020076static std::unique_ptr<QueuedTask> NewClosure(Closure&& closure,
77 Cleanup&& cleanup) {
Karl Wiberg918f50c2018-07-05 11:40:33 +020078 return absl::make_unique<ClosureTaskWithCleanup<Closure, Cleanup>>(
Danil Chapovalov6f09ae22017-10-12 14:39:25 +020079 std::forward<Closure>(closure), std::forward<Cleanup>(cleanup));
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020080}
81
82// Implements a task queue that asynchronously executes tasks in a way that
83// guarantees that they're executed in FIFO order and that tasks never overlap.
84// Tasks may always execute on the same worker thread and they may not.
85// To DCHECK that tasks are executing on a known task queue, use IsCurrent().
86//
87// Here are some usage examples:
88//
89// 1) Asynchronously running a lambda:
90//
91// class MyClass {
92// ...
93// TaskQueue queue_("MyQueue");
94// };
95//
96// void MyClass::StartWork() {
97// queue_.PostTask([]() { Work(); });
98// ...
99//
100// 2) Doing work asynchronously on a worker queue and providing a notification
101// callback on the current queue, when the work has been done:
102//
103// void MyClass::StartWorkAndLetMeKnowWhenDone(
104// std::unique_ptr<QueuedTask> callback) {
105// DCHECK(TaskQueue::Current()) << "Need to be running on a queue";
106// queue_.PostTaskAndReply([]() { Work(); }, std::move(callback));
107// }
108// ...
109// my_class->StartWorkAndLetMeKnowWhenDone(
Mirko Bonadei675513b2017-11-09 11:09:25 +0100110// NewClosure([]() { RTC_LOG(INFO) << "The work is done!";}));
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200111//
112// 3) Posting a custom task on a timer. The task posts itself again after
113// every running:
114//
115// class TimerTask : public QueuedTask {
116// public:
117// TimerTask() {}
118// private:
119// bool Run() override {
120// ++count_;
121// TaskQueue::Current()->PostDelayedTask(
122// std::unique_ptr<QueuedTask>(this), 1000);
123// // Ownership has been transferred to the next occurance,
124// // so return false to prevent from being deleted now.
125// return false;
126// }
127// int count_ = 0;
128// };
129// ...
130// queue_.PostDelayedTask(
131// std::unique_ptr<QueuedTask>(new TimerTask()), 1000);
132//
133// For more examples, see task_queue_unittests.cc.
134//
135// A note on destruction:
136//
137// When a TaskQueue is deleted, pending tasks will not be executed but they will
138// be deleted. The deletion of tasks may happen asynchronously after the
139// TaskQueue itself has been deleted or it may happen synchronously while the
140// TaskQueue instance is being deleted. This may vary from one OS to the next
141// so assumptions about lifetimes of pending tasks should not be made.
Mirko Bonadei3d255302018-10-11 10:50:45 +0200142class RTC_LOCKABLE RTC_EXPORT TaskQueue {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200143 public:
144 // TaskQueue priority levels. On some platforms these will map to thread
145 // priorities, on others such as Mac and iOS, GCD queue priorities.
Danil Chapovalovd00405f2019-02-25 15:06:13 +0100146 using Priority = ::webrtc::TaskQueueFactory::Priority;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200147
Danil Chapovalovf3280e92019-02-28 10:39:04 +0100148 explicit TaskQueue(std::unique_ptr<webrtc::TaskQueueBase,
149 webrtc::TaskQueueDeleter> task_queue);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200150 explicit TaskQueue(const char* queue_name,
151 Priority priority = Priority::NORMAL);
152 ~TaskQueue();
153
154 static TaskQueue* Current();
155
156 // Used for DCHECKing the current queue.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200157 bool IsCurrent() const;
158
Danil Chapovalovf3280e92019-02-28 10:39:04 +0100159 // Returns non-owning pointer to the task queue implementation.
160 webrtc::TaskQueueBase* Get() { return impl_; }
161
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200162 // TODO(tommi): For better debuggability, implement RTC_FROM_HERE.
163
164 // Ownership of the task is passed to PostTask.
165 void PostTask(std::unique_ptr<QueuedTask> task);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200166
167 // Schedules a task to execute a specified number of milliseconds from when
168 // the call is made. The precision should be considered as "best effort"
169 // and in some cases, such as on Windows when all high precision timers have
170 // been used up, can be off by as much as 15 millseconds (although 8 would be
171 // more likely). This can be mitigated by limiting the use of delayed tasks.
172 void PostDelayedTask(std::unique_ptr<QueuedTask> task, uint32_t milliseconds);
173
eladalonffe2e142017-08-31 04:36:05 -0700174 // std::enable_if is used here to make sure that calls to PostTask() with
175 // std::unique_ptr<SomeClassDerivedFromQueuedTask> would not end up being
176 // caught by this template.
177 template <class Closure,
Danil Chapovalov6f09ae22017-10-12 14:39:25 +0200178 typename std::enable_if<!std::is_convertible<
179 Closure,
180 std::unique_ptr<QueuedTask>>::value>::type* = nullptr>
181 void PostTask(Closure&& closure) {
182 PostTask(NewClosure(std::forward<Closure>(closure)));
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200183 }
184
185 // See documentation above for performance expectations.
Danil Chapovalov6f09ae22017-10-12 14:39:25 +0200186 template <class Closure,
187 typename std::enable_if<!std::is_convertible<
188 Closure,
189 std::unique_ptr<QueuedTask>>::value>::type* = nullptr>
190 void PostDelayedTask(Closure&& closure, uint32_t milliseconds) {
191 PostDelayedTask(NewClosure(std::forward<Closure>(closure)), milliseconds);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200192 }
193
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200194 private:
Danil Chapovalovd00405f2019-02-25 15:06:13 +0100195 webrtc::TaskQueueBase* const impl_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200196
197 RTC_DISALLOW_COPY_AND_ASSIGN(TaskQueue);
198};
199
200} // namespace rtc
tommic06b1332016-05-14 11:31:40 -0700201
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200202#endif // RTC_BASE_TASK_QUEUE_H_