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 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 19 | #include "absl/memory/memory.h" |
Danil Chapovalov | 959e9b6 | 2019-01-14 14:29:18 +0100 | [diff] [blame] | 20 | #include "api/task_queue/queued_task.h" |
Danil Chapovalov | d00405f | 2019-02-25 15:06:13 +0100 | [diff] [blame] | 21 | #include "api/task_queue/task_queue_base.h" |
| 22 | #include "api/task_queue/task_queue_factory.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 23 | #include "rtc_base/constructor_magic.h" |
Mirko Bonadei | 3d25530 | 2018-10-11 10:50:45 +0200 | [diff] [blame] | 24 | #include "rtc_base/system/rtc_export.h" |
Danil Chapovalov | 3b548dd | 2019-03-01 14:58:44 +0100 | [diff] [blame] | 25 | #include "rtc_base/task_utils/to_queued_task.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 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 86 | // Used for DCHECKing the current queue. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 87 | bool IsCurrent() const; |
| 88 | |
Danil Chapovalov | f3280e9 | 2019-02-28 10:39:04 +0100 | [diff] [blame] | 89 | // Returns non-owning pointer to the task queue implementation. |
| 90 | webrtc::TaskQueueBase* Get() { return impl_; } |
| 91 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 92 | // TODO(tommi): For better debuggability, implement RTC_FROM_HERE. |
| 93 | |
| 94 | // Ownership of the task is passed to PostTask. |
Danil Chapovalov | 471783f | 2019-03-11 14:26:02 +0100 | [diff] [blame] | 95 | void PostTask(std::unique_ptr<webrtc::QueuedTask> task); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 96 | |
| 97 | // Schedules a task to execute a specified number of milliseconds from when |
| 98 | // the call is made. The precision should be considered as "best effort" |
| 99 | // and in some cases, such as on Windows when all high precision timers have |
| 100 | // been used up, can be off by as much as 15 millseconds (although 8 would be |
| 101 | // more likely). This can be mitigated by limiting the use of delayed tasks. |
Danil Chapovalov | 471783f | 2019-03-11 14:26:02 +0100 | [diff] [blame] | 102 | void PostDelayedTask(std::unique_ptr<webrtc::QueuedTask> task, |
| 103 | uint32_t milliseconds); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 104 | |
eladalon | ffe2e14 | 2017-08-31 04:36:05 -0700 | [diff] [blame] | 105 | // std::enable_if is used here to make sure that calls to PostTask() with |
| 106 | // std::unique_ptr<SomeClassDerivedFromQueuedTask> would not end up being |
| 107 | // caught by this template. |
| 108 | template <class Closure, |
Danil Chapovalov | 6f09ae2 | 2017-10-12 14:39:25 +0200 | [diff] [blame] | 109 | typename std::enable_if<!std::is_convertible< |
| 110 | Closure, |
Danil Chapovalov | 471783f | 2019-03-11 14:26:02 +0100 | [diff] [blame] | 111 | std::unique_ptr<webrtc::QueuedTask>>::value>::type* = nullptr> |
Danil Chapovalov | 6f09ae2 | 2017-10-12 14:39:25 +0200 | [diff] [blame] | 112 | void PostTask(Closure&& closure) { |
Danil Chapovalov | 1aa7581 | 2019-03-05 11:11:35 +0100 | [diff] [blame] | 113 | PostTask(webrtc::ToQueuedTask(std::forward<Closure>(closure))); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | // See documentation above for performance expectations. |
Danil Chapovalov | 6f09ae2 | 2017-10-12 14:39:25 +0200 | [diff] [blame] | 117 | template <class Closure, |
| 118 | typename std::enable_if<!std::is_convertible< |
| 119 | Closure, |
Danil Chapovalov | 471783f | 2019-03-11 14:26:02 +0100 | [diff] [blame] | 120 | std::unique_ptr<webrtc::QueuedTask>>::value>::type* = nullptr> |
Danil Chapovalov | 6f09ae2 | 2017-10-12 14:39:25 +0200 | [diff] [blame] | 121 | void PostDelayedTask(Closure&& closure, uint32_t milliseconds) { |
Danil Chapovalov | 1aa7581 | 2019-03-05 11:11:35 +0100 | [diff] [blame] | 122 | PostDelayedTask(webrtc::ToQueuedTask(std::forward<Closure>(closure)), |
| 123 | milliseconds); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 124 | } |
| 125 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 126 | private: |
Danil Chapovalov | d00405f | 2019-02-25 15:06:13 +0100 | [diff] [blame] | 127 | webrtc::TaskQueueBase* const impl_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 128 | |
| 129 | RTC_DISALLOW_COPY_AND_ASSIGN(TaskQueue); |
| 130 | }; |
| 131 | |
| 132 | } // namespace rtc |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 133 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 134 | #endif // RTC_BASE_TASK_QUEUE_H_ |