tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef RTC_BASE_TASK_QUEUE_H_ |
| 12 | #define RTC_BASE_TASK_QUEUE_H_ |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 13 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 14 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 15 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 16 | #include <memory> |
Danil Chapovalov | 6f09ae2 | 2017-10-12 14:39:25 +0200 | [diff] [blame] | 17 | #include <utility> |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 18 | |
Danil Chapovalov | a7e15a2 | 2022-07-05 16:03:03 +0200 | [diff] [blame] | 19 | #include "absl/functional/any_invocable.h" |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 20 | #include "absl/memory/memory.h" |
Danil Chapovalov | 959e9b6 | 2019-01-14 14:29:18 +0100 | [diff] [blame] | 21 | #include "api/task_queue/queued_task.h" |
Danil Chapovalov | d00405f | 2019-02-25 15:06:13 +0100 | [diff] [blame] | 22 | #include "api/task_queue/task_queue_base.h" |
| 23 | #include "api/task_queue/task_queue_factory.h" |
Artem Titov | c374d11 | 2022-06-16 21:27:45 +0200 | [diff] [blame] | 24 | #include "api/task_queue/to_queued_task.h" |
Mirko Bonadei | 3d25530 | 2018-10-11 10:50:45 +0200 | [diff] [blame] | 25 | #include "rtc_base/system/rtc_export.h" |
Danil Chapovalov | 02fddf6 | 2018-02-12 12:41:16 +0100 | [diff] [blame] | 26 | #include "rtc_base/thread_annotations.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 27 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 28 | namespace rtc { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 29 | // Implements a task queue that asynchronously executes tasks in a way that |
| 30 | // guarantees that they're executed in FIFO order and that tasks never overlap. |
| 31 | // Tasks may always execute on the same worker thread and they may not. |
| 32 | // To DCHECK that tasks are executing on a known task queue, use IsCurrent(). |
| 33 | // |
| 34 | // Here are some usage examples: |
| 35 | // |
| 36 | // 1) Asynchronously running a lambda: |
| 37 | // |
| 38 | // class MyClass { |
| 39 | // ... |
| 40 | // TaskQueue queue_("MyQueue"); |
| 41 | // }; |
| 42 | // |
| 43 | // void MyClass::StartWork() { |
| 44 | // queue_.PostTask([]() { Work(); }); |
| 45 | // ... |
| 46 | // |
Danil Chapovalov | 1aa7581 | 2019-03-05 11:11:35 +0100 | [diff] [blame] | 47 | // 2) Posting a custom task on a timer. The task posts itself again after |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 48 | // every running: |
| 49 | // |
| 50 | // class TimerTask : public QueuedTask { |
| 51 | // public: |
| 52 | // TimerTask() {} |
| 53 | // private: |
| 54 | // bool Run() override { |
| 55 | // ++count_; |
Danil Chapovalov | ad89528 | 2019-03-11 10:28:05 +0000 | [diff] [blame] | 56 | // TaskQueueBase::Current()->PostDelayedTask( |
| 57 | // absl::WrapUnique(this), 1000); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 58 | // // Ownership has been transferred to the next occurance, |
| 59 | // // so return false to prevent from being deleted now. |
| 60 | // return false; |
| 61 | // } |
| 62 | // int count_ = 0; |
| 63 | // }; |
| 64 | // ... |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 65 | // queue_.PostDelayedTask(std::make_unique<TimerTask>(), 1000); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 66 | // |
| 67 | // For more examples, see task_queue_unittests.cc. |
| 68 | // |
| 69 | // A note on destruction: |
| 70 | // |
| 71 | // When a TaskQueue is deleted, pending tasks will not be executed but they will |
| 72 | // be deleted. The deletion of tasks may happen asynchronously after the |
| 73 | // TaskQueue itself has been deleted or it may happen synchronously while the |
| 74 | // TaskQueue instance is being deleted. This may vary from one OS to the next |
| 75 | // so assumptions about lifetimes of pending tasks should not be made. |
Mirko Bonadei | 3d25530 | 2018-10-11 10:50:45 +0200 | [diff] [blame] | 76 | class RTC_LOCKABLE RTC_EXPORT TaskQueue { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 77 | public: |
| 78 | // TaskQueue priority levels. On some platforms these will map to thread |
| 79 | // priorities, on others such as Mac and iOS, GCD queue priorities. |
Danil Chapovalov | d00405f | 2019-02-25 15:06:13 +0100 | [diff] [blame] | 80 | using Priority = ::webrtc::TaskQueueFactory::Priority; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 81 | |
Danil Chapovalov | f3280e9 | 2019-02-28 10:39:04 +0100 | [diff] [blame] | 82 | explicit TaskQueue(std::unique_ptr<webrtc::TaskQueueBase, |
| 83 | webrtc::TaskQueueDeleter> task_queue); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 84 | ~TaskQueue(); |
| 85 | |
Byoungchan Lee | 14af762 | 2022-01-12 05:24:58 +0900 | [diff] [blame] | 86 | TaskQueue(const TaskQueue&) = delete; |
| 87 | TaskQueue& operator=(const TaskQueue&) = delete; |
| 88 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 89 | // Used for DCHECKing the current queue. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 90 | bool IsCurrent() const; |
| 91 | |
Danil Chapovalov | f3280e9 | 2019-02-28 10:39:04 +0100 | [diff] [blame] | 92 | // Returns non-owning pointer to the task queue implementation. |
| 93 | webrtc::TaskQueueBase* Get() { return impl_; } |
| 94 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 95 | // TODO(tommi): For better debuggability, implement RTC_FROM_HERE. |
| 96 | |
| 97 | // Ownership of the task is passed to PostTask. |
Danil Chapovalov | 471783f | 2019-03-11 14:26:02 +0100 | [diff] [blame] | 98 | void PostTask(std::unique_ptr<webrtc::QueuedTask> task); |
Henrik Boström | 2dd3915 | 2022-01-25 08:20:33 +0100 | [diff] [blame] | 99 | // See webrtc::TaskQueueBase for precision expectations. |
Danil Chapovalov | 471783f | 2019-03-11 14:26:02 +0100 | [diff] [blame] | 100 | void PostDelayedTask(std::unique_ptr<webrtc::QueuedTask> task, |
| 101 | uint32_t milliseconds); |
Henrik Boström | 2dd3915 | 2022-01-25 08:20:33 +0100 | [diff] [blame] | 102 | void PostDelayedHighPrecisionTask(std::unique_ptr<webrtc::QueuedTask> task, |
| 103 | uint32_t milliseconds); |
Henrik Boström | 19ba552 | 2022-03-14 15:19:43 +0100 | [diff] [blame] | 104 | void PostDelayedTaskWithPrecision( |
| 105 | webrtc::TaskQueueBase::DelayPrecision precision, |
| 106 | std::unique_ptr<webrtc::QueuedTask> task, |
| 107 | uint32_t milliseconds); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 108 | |
Danil Chapovalov | a7e15a2 | 2022-07-05 16:03:03 +0200 | [diff] [blame] | 109 | void PostTask(absl::AnyInvocable<void() &&> task) { |
| 110 | impl_->PostTask(std::move(task)); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 111 | } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 112 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 113 | private: |
Danil Chapovalov | d00405f | 2019-02-25 15:06:13 +0100 | [diff] [blame] | 114 | webrtc::TaskQueueBase* const impl_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | } // namespace rtc |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 118 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 119 | #endif // RTC_BASE_TASK_QUEUE_H_ |