blob: 882f751f4ffac891819ce9506f8864aedcdf3435 [file] [log] [blame]
tommic06b1332016-05-14 18:31:401/*
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 04:47:3111#ifndef RTC_BASE_TASK_QUEUE_H_
12#define RTC_BASE_TASK_QUEUE_H_
tommic06b1332016-05-14 18:31:4013
Yves Gerey3e707812018-11-28 15:47:4914#include <stdint.h>
Jonas Olssona4d87372019-07-05 17:08:3315
Henrik Kjellanderec78f1c2017-06-29 05:52:5016#include <memory>
Danil Chapovalov6f09ae22017-10-12 12:39:2517#include <utility>
tommic06b1332016-05-14 18:31:4018
Karl Wiberg918f50c2018-07-05 09:40:3319#include "absl/memory/memory.h"
Danil Chapovalov959e9b62019-01-14 13:29:1820#include "api/task_queue/queued_task.h"
Danil Chapovalovd00405f2019-02-25 14:06:1321#include "api/task_queue/task_queue_base.h"
22#include "api/task_queue/task_queue_factory.h"
Artem Titovc374d112022-06-16 19:27:4523#include "api/task_queue/to_queued_task.h"
Mirko Bonadei3d255302018-10-11 08:50:4524#include "rtc_base/system/rtc_export.h"
Danil Chapovalov02fddf62018-02-12 11:41:1625#include "rtc_base/thread_annotations.h"
Henrik Kjellanderec78f1c2017-06-29 05:52:5026
Henrik Kjellanderec78f1c2017-06-29 05:52:5027namespace rtc {
Henrik Kjellanderec78f1c2017-06-29 05:52:5028// Implements a task queue that asynchronously executes tasks in a way that
29// guarantees that they're executed in FIFO order and that tasks never overlap.
30// Tasks may always execute on the same worker thread and they may not.
31// To DCHECK that tasks are executing on a known task queue, use IsCurrent().
32//
33// Here are some usage examples:
34//
35// 1) Asynchronously running a lambda:
36//
37// class MyClass {
38// ...
39// TaskQueue queue_("MyQueue");
40// };
41//
42// void MyClass::StartWork() {
43// queue_.PostTask([]() { Work(); });
44// ...
45//
Danil Chapovalov1aa75812019-03-05 10:11:3546// 2) Posting a custom task on a timer. The task posts itself again after
Henrik Kjellanderec78f1c2017-06-29 05:52:5047// every running:
48//
49// class TimerTask : public QueuedTask {
50// public:
51// TimerTask() {}
52// private:
53// bool Run() override {
54// ++count_;
Danil Chapovalovad895282019-03-11 10:28:0555// TaskQueueBase::Current()->PostDelayedTask(
56// absl::WrapUnique(this), 1000);
Henrik Kjellanderec78f1c2017-06-29 05:52:5057// // Ownership has been transferred to the next occurance,
58// // so return false to prevent from being deleted now.
59// return false;
60// }
61// int count_ = 0;
62// };
63// ...
Mirko Bonadei317a1f02019-09-17 15:06:1864// queue_.PostDelayedTask(std::make_unique<TimerTask>(), 1000);
Henrik Kjellanderec78f1c2017-06-29 05:52:5065//
66// For more examples, see task_queue_unittests.cc.
67//
68// A note on destruction:
69//
70// When a TaskQueue is deleted, pending tasks will not be executed but they will
71// be deleted. The deletion of tasks may happen asynchronously after the
72// TaskQueue itself has been deleted or it may happen synchronously while the
73// TaskQueue instance is being deleted. This may vary from one OS to the next
74// so assumptions about lifetimes of pending tasks should not be made.
Mirko Bonadei3d255302018-10-11 08:50:4575class RTC_LOCKABLE RTC_EXPORT TaskQueue {
Henrik Kjellanderec78f1c2017-06-29 05:52:5076 public:
77 // TaskQueue priority levels. On some platforms these will map to thread
78 // priorities, on others such as Mac and iOS, GCD queue priorities.
Danil Chapovalovd00405f2019-02-25 14:06:1379 using Priority = ::webrtc::TaskQueueFactory::Priority;
Henrik Kjellanderec78f1c2017-06-29 05:52:5080
Danil Chapovalovf3280e92019-02-28 09:39:0481 explicit TaskQueue(std::unique_ptr<webrtc::TaskQueueBase,
82 webrtc::TaskQueueDeleter> task_queue);
Henrik Kjellanderec78f1c2017-06-29 05:52:5083 ~TaskQueue();
84
Byoungchan Lee14af7622022-01-11 20:24:5885 TaskQueue(const TaskQueue&) = delete;
86 TaskQueue& operator=(const TaskQueue&) = delete;
87
Henrik Kjellanderec78f1c2017-06-29 05:52:5088 // Used for DCHECKing the current queue.
Henrik Kjellanderec78f1c2017-06-29 05:52:5089 bool IsCurrent() const;
90
Danil Chapovalovf3280e92019-02-28 09:39:0491 // Returns non-owning pointer to the task queue implementation.
92 webrtc::TaskQueueBase* Get() { return impl_; }
93
Henrik Kjellanderec78f1c2017-06-29 05:52:5094 // TODO(tommi): For better debuggability, implement RTC_FROM_HERE.
95
96 // Ownership of the task is passed to PostTask.
Danil Chapovalov471783f2019-03-11 13:26:0297 void PostTask(std::unique_ptr<webrtc::QueuedTask> task);
Henrik Boström2dd39152022-01-25 07:20:3398 // See webrtc::TaskQueueBase for precision expectations.
Danil Chapovalov471783f2019-03-11 13:26:0299 void PostDelayedTask(std::unique_ptr<webrtc::QueuedTask> task,
100 uint32_t milliseconds);
Henrik Boström2dd39152022-01-25 07:20:33101 void PostDelayedHighPrecisionTask(std::unique_ptr<webrtc::QueuedTask> task,
102 uint32_t milliseconds);
Henrik Boström19ba5522022-03-14 14:19:43103 void PostDelayedTaskWithPrecision(
104 webrtc::TaskQueueBase::DelayPrecision precision,
105 std::unique_ptr<webrtc::QueuedTask> task,
106 uint32_t milliseconds);
Henrik Kjellanderec78f1c2017-06-29 05:52:50107
eladalonffe2e142017-08-31 11:36:05108 // std::enable_if is used here to make sure that calls to PostTask() with
109 // std::unique_ptr<SomeClassDerivedFromQueuedTask> would not end up being
110 // caught by this template.
111 template <class Closure,
Danil Chapovalov6f09ae22017-10-12 12:39:25112 typename std::enable_if<!std::is_convertible<
113 Closure,
Danil Chapovalov471783f2019-03-11 13:26:02114 std::unique_ptr<webrtc::QueuedTask>>::value>::type* = nullptr>
Danil Chapovalov6f09ae22017-10-12 12:39:25115 void PostTask(Closure&& closure) {
Danil Chapovalov1aa75812019-03-05 10:11:35116 PostTask(webrtc::ToQueuedTask(std::forward<Closure>(closure)));
Henrik Kjellanderec78f1c2017-06-29 05:52:50117 }
Henrik Kjellanderec78f1c2017-06-29 05:52:50118
Henrik Kjellanderec78f1c2017-06-29 05:52:50119 private:
Danil Chapovalovd00405f2019-02-25 14:06:13120 webrtc::TaskQueueBase* const impl_;
Henrik Kjellanderec78f1c2017-06-29 05:52:50121};
122
123} // namespace rtc
tommic06b1332016-05-14 18:31:40124
Mirko Bonadei92ea95e2017-09-15 04:47:31125#endif // RTC_BASE_TASK_QUEUE_H_