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> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 15 | #include <memory> |
Danil Chapovalov | 6f09ae2 | 2017-10-12 14:39:25 +0200 | [diff] [blame] | 16 | #include <utility> |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 17 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 18 | #include "absl/memory/memory.h" |
Danil Chapovalov | 959e9b6 | 2019-01-14 14:29:18 +0100 | [diff] [blame] | 19 | #include "api/task_queue/queued_task.h" |
Danil Chapovalov | d00405f | 2019-02-25 15:06:13 +0100 | [diff] [blame] | 20 | #include "api/task_queue/task_queue_base.h" |
| 21 | #include "api/task_queue/task_queue_factory.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 22 | #include "rtc_base/constructor_magic.h" |
Mirko Bonadei | 3d25530 | 2018-10-11 10:50:45 +0200 | [diff] [blame] | 23 | #include "rtc_base/system/rtc_export.h" |
Danil Chapovalov | 3b548dd | 2019-03-01 14:58:44 +0100 | [diff] [blame] | 24 | #include "rtc_base/task_utils/to_queued_task.h" |
Danil Chapovalov | 02fddf6 | 2018-02-12 12:41:16 +0100 | [diff] [blame] | 25 | #include "rtc_base/thread_annotations.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 26 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 27 | namespace rtc { |
| 28 | |
Danil Chapovalov | 959e9b6 | 2019-01-14 14:29:18 +0100 | [diff] [blame] | 29 | // TODO(danilchap): Remove the alias when all of webrtc is updated to use |
| 30 | // webrtc::QueuedTask directly. |
| 31 | using ::webrtc::QueuedTask; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 32 | |
| 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 Chapovalov | 1aa7581 | 2019-03-05 11:11:35 +0100 | [diff] [blame] | 51 | // 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] | 52 | // every running: |
| 53 | // |
| 54 | // class TimerTask : public QueuedTask { |
| 55 | // public: |
| 56 | // TimerTask() {} |
| 57 | // private: |
| 58 | // bool Run() override { |
| 59 | // ++count_; |
Danil Chapovalov | 304e9d2 | 2019-03-07 17:41:37 +0100 | [diff] [blame] | 60 | // TaskQueueBase::Current()->PostDelayedTask( |
| 61 | // absl::WrapUnique(this), 1000); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 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 | // ... |
Danil Chapovalov | 304e9d2 | 2019-03-07 17:41:37 +0100 | [diff] [blame] | 69 | // queue_.PostDelayedTask(absl::make_unique<TimerTask>(), 1000); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 70 | // |
| 71 | // For more examples, see task_queue_unittests.cc. |
| 72 | // |
| 73 | // A note on destruction: |
| 74 | // |
| 75 | // When a TaskQueue is deleted, pending tasks will not be executed but they will |
| 76 | // be deleted. The deletion of tasks may happen asynchronously after the |
| 77 | // TaskQueue itself has been deleted or it may happen synchronously while the |
| 78 | // TaskQueue instance is being deleted. This may vary from one OS to the next |
| 79 | // so assumptions about lifetimes of pending tasks should not be made. |
Mirko Bonadei | 3d25530 | 2018-10-11 10:50:45 +0200 | [diff] [blame] | 80 | class RTC_LOCKABLE RTC_EXPORT TaskQueue { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 81 | public: |
| 82 | // TaskQueue priority levels. On some platforms these will map to thread |
| 83 | // priorities, on others such as Mac and iOS, GCD queue priorities. |
Danil Chapovalov | d00405f | 2019-02-25 15:06:13 +0100 | [diff] [blame] | 84 | using Priority = ::webrtc::TaskQueueFactory::Priority; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 85 | |
Danil Chapovalov | f3280e9 | 2019-02-28 10:39:04 +0100 | [diff] [blame] | 86 | explicit TaskQueue(std::unique_ptr<webrtc::TaskQueueBase, |
| 87 | webrtc::TaskQueueDeleter> task_queue); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 88 | explicit TaskQueue(const char* queue_name, |
| 89 | Priority priority = Priority::NORMAL); |
| 90 | ~TaskQueue(); |
| 91 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 92 | // Used for DCHECKing the current queue. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 93 | bool IsCurrent() const; |
| 94 | |
Danil Chapovalov | f3280e9 | 2019-02-28 10:39:04 +0100 | [diff] [blame] | 95 | // Returns non-owning pointer to the task queue implementation. |
| 96 | webrtc::TaskQueueBase* Get() { return impl_; } |
| 97 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 98 | // TODO(tommi): For better debuggability, implement RTC_FROM_HERE. |
| 99 | |
| 100 | // Ownership of the task is passed to PostTask. |
| 101 | void PostTask(std::unique_ptr<QueuedTask> task); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 102 | |
| 103 | // Schedules a task to execute a specified number of milliseconds from when |
| 104 | // the call is made. The precision should be considered as "best effort" |
| 105 | // and in some cases, such as on Windows when all high precision timers have |
| 106 | // been used up, can be off by as much as 15 millseconds (although 8 would be |
| 107 | // more likely). This can be mitigated by limiting the use of delayed tasks. |
| 108 | void PostDelayedTask(std::unique_ptr<QueuedTask> task, uint32_t milliseconds); |
| 109 | |
eladalon | ffe2e14 | 2017-08-31 04:36:05 -0700 | [diff] [blame] | 110 | // std::enable_if is used here to make sure that calls to PostTask() with |
| 111 | // std::unique_ptr<SomeClassDerivedFromQueuedTask> would not end up being |
| 112 | // caught by this template. |
| 113 | template <class Closure, |
Danil Chapovalov | 6f09ae2 | 2017-10-12 14:39:25 +0200 | [diff] [blame] | 114 | typename std::enable_if<!std::is_convertible< |
| 115 | Closure, |
| 116 | std::unique_ptr<QueuedTask>>::value>::type* = nullptr> |
| 117 | void PostTask(Closure&& closure) { |
Danil Chapovalov | 1aa7581 | 2019-03-05 11:11:35 +0100 | [diff] [blame] | 118 | PostTask(webrtc::ToQueuedTask(std::forward<Closure>(closure))); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | // See documentation above for performance expectations. |
Danil Chapovalov | 6f09ae2 | 2017-10-12 14:39:25 +0200 | [diff] [blame] | 122 | template <class Closure, |
| 123 | typename std::enable_if<!std::is_convertible< |
| 124 | Closure, |
| 125 | std::unique_ptr<QueuedTask>>::value>::type* = nullptr> |
| 126 | void PostDelayedTask(Closure&& closure, uint32_t milliseconds) { |
Danil Chapovalov | 1aa7581 | 2019-03-05 11:11:35 +0100 | [diff] [blame] | 127 | PostDelayedTask(webrtc::ToQueuedTask(std::forward<Closure>(closure)), |
| 128 | milliseconds); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 129 | } |
| 130 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 131 | private: |
Danil Chapovalov | d00405f | 2019-02-25 15:06:13 +0100 | [diff] [blame] | 132 | webrtc::TaskQueueBase* const impl_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 133 | |
| 134 | RTC_DISALLOW_COPY_AND_ASSIGN(TaskQueue); |
| 135 | }; |
| 136 | |
| 137 | } // namespace rtc |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 138 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 139 | #endif // RTC_BASE_TASK_QUEUE_H_ |