task_queue.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #ifndef RTC_BASE_TASK_QUEUE_H_
  11. #define RTC_BASE_TASK_QUEUE_H_
  12. #include <stdint.h>
  13. #include <memory>
  14. #include <utility>
  15. #include "absl/memory/memory.h"
  16. #include "api/task_queue/queued_task.h"
  17. #include "api/task_queue/task_queue_base.h"
  18. #include "api/task_queue/task_queue_factory.h"
  19. #include "rtc_base/constructor_magic.h"
  20. #include "rtc_base/system/rtc_export.h"
  21. #include "rtc_base/task_utils/to_queued_task.h"
  22. #include "rtc_base/thread_annotations.h"
  23. namespace rtc {
  24. // Implements a task queue that asynchronously executes tasks in a way that
  25. // guarantees that they're executed in FIFO order and that tasks never overlap.
  26. // Tasks may always execute on the same worker thread and they may not.
  27. // To DCHECK that tasks are executing on a known task queue, use IsCurrent().
  28. //
  29. // Here are some usage examples:
  30. //
  31. // 1) Asynchronously running a lambda:
  32. //
  33. // class MyClass {
  34. // ...
  35. // TaskQueue queue_("MyQueue");
  36. // };
  37. //
  38. // void MyClass::StartWork() {
  39. // queue_.PostTask([]() { Work(); });
  40. // ...
  41. //
  42. // 2) Posting a custom task on a timer. The task posts itself again after
  43. // every running:
  44. //
  45. // class TimerTask : public QueuedTask {
  46. // public:
  47. // TimerTask() {}
  48. // private:
  49. // bool Run() override {
  50. // ++count_;
  51. // TaskQueueBase::Current()->PostDelayedTask(
  52. // absl::WrapUnique(this), 1000);
  53. // // Ownership has been transferred to the next occurance,
  54. // // so return false to prevent from being deleted now.
  55. // return false;
  56. // }
  57. // int count_ = 0;
  58. // };
  59. // ...
  60. // queue_.PostDelayedTask(std::make_unique<TimerTask>(), 1000);
  61. //
  62. // For more examples, see task_queue_unittests.cc.
  63. //
  64. // A note on destruction:
  65. //
  66. // When a TaskQueue is deleted, pending tasks will not be executed but they will
  67. // be deleted. The deletion of tasks may happen asynchronously after the
  68. // TaskQueue itself has been deleted or it may happen synchronously while the
  69. // TaskQueue instance is being deleted. This may vary from one OS to the next
  70. // so assumptions about lifetimes of pending tasks should not be made.
  71. class RTC_LOCKABLE RTC_EXPORT TaskQueue {
  72. public:
  73. // TaskQueue priority levels. On some platforms these will map to thread
  74. // priorities, on others such as Mac and iOS, GCD queue priorities.
  75. using Priority = ::webrtc::TaskQueueFactory::Priority;
  76. explicit TaskQueue(std::unique_ptr<webrtc::TaskQueueBase,
  77. webrtc::TaskQueueDeleter> task_queue);
  78. ~TaskQueue();
  79. // Used for DCHECKing the current queue.
  80. bool IsCurrent() const;
  81. // Returns non-owning pointer to the task queue implementation.
  82. webrtc::TaskQueueBase* Get() { return impl_; }
  83. // TODO(tommi): For better debuggability, implement RTC_FROM_HERE.
  84. // Ownership of the task is passed to PostTask.
  85. void PostTask(std::unique_ptr<webrtc::QueuedTask> task);
  86. // Schedules a task to execute a specified number of milliseconds from when
  87. // the call is made. The precision should be considered as "best effort"
  88. // and in some cases, such as on Windows when all high precision timers have
  89. // been used up, can be off by as much as 15 millseconds (although 8 would be
  90. // more likely). This can be mitigated by limiting the use of delayed tasks.
  91. void PostDelayedTask(std::unique_ptr<webrtc::QueuedTask> task,
  92. uint32_t milliseconds);
  93. // std::enable_if is used here to make sure that calls to PostTask() with
  94. // std::unique_ptr<SomeClassDerivedFromQueuedTask> would not end up being
  95. // caught by this template.
  96. template <class Closure,
  97. typename std::enable_if<!std::is_convertible<
  98. Closure,
  99. std::unique_ptr<webrtc::QueuedTask>>::value>::type* = nullptr>
  100. void PostTask(Closure&& closure) {
  101. PostTask(webrtc::ToQueuedTask(std::forward<Closure>(closure)));
  102. }
  103. // See documentation above for performance expectations.
  104. template <class Closure,
  105. typename std::enable_if<!std::is_convertible<
  106. Closure,
  107. std::unique_ptr<webrtc::QueuedTask>>::value>::type* = nullptr>
  108. void PostDelayedTask(Closure&& closure, uint32_t milliseconds) {
  109. PostDelayedTask(webrtc::ToQueuedTask(std::forward<Closure>(closure)),
  110. milliseconds);
  111. }
  112. private:
  113. webrtc::TaskQueueBase* const impl_;
  114. RTC_DISALLOW_COPY_AND_ASSIGN(TaskQueue);
  115. };
  116. } // namespace rtc
  117. #endif // RTC_BASE_TASK_QUEUE_H_