You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
4.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. // This file contains the implementation of TaskQueue for Mac and iOS.
  11. // The implementation uses Grand Central Dispatch queues (GCD) to
  12. // do the actual task queuing.
  13. #include "rtc_base/task_queue_gcd.h"
  14. #include <dispatch/dispatch.h>
  15. #include <string.h>
  16. #include <memory>
  17. #include "absl/strings/string_view.h"
  18. #include "api/task_queue/queued_task.h"
  19. #include "api/task_queue/task_queue_base.h"
  20. #include "rtc_base/checks.h"
  21. #include "rtc_base/logging.h"
  22. namespace webrtc {
  23. namespace {
  24. int TaskQueuePriorityToGCD(TaskQueueFactory::Priority priority) {
  25. switch (priority) {
  26. case TaskQueueFactory::Priority::NORMAL:
  27. return DISPATCH_QUEUE_PRIORITY_DEFAULT;
  28. case TaskQueueFactory::Priority::HIGH:
  29. return DISPATCH_QUEUE_PRIORITY_HIGH;
  30. case TaskQueueFactory::Priority::LOW:
  31. return DISPATCH_QUEUE_PRIORITY_LOW;
  32. }
  33. }
  34. class TaskQueueGcd : public TaskQueueBase {
  35. public:
  36. TaskQueueGcd(absl::string_view queue_name, int gcd_priority);
  37. void Delete() override;
  38. void PostTask(std::unique_ptr<QueuedTask> task) override;
  39. void PostDelayedTask(std::unique_ptr<QueuedTask> task,
  40. uint32_t milliseconds) override;
  41. private:
  42. struct TaskContext {
  43. TaskContext(TaskQueueGcd* queue, std::unique_ptr<QueuedTask> task)
  44. : queue(queue), task(std::move(task)) {}
  45. TaskQueueGcd* const queue;
  46. std::unique_ptr<QueuedTask> task;
  47. };
  48. ~TaskQueueGcd() override;
  49. static void RunTask(void* task_context);
  50. static void SetNotActive(void* task_queue);
  51. static void DeleteQueue(void* task_queue);
  52. dispatch_queue_t queue_;
  53. bool is_active_;
  54. };
  55. TaskQueueGcd::TaskQueueGcd(absl::string_view queue_name, int gcd_priority)
  56. : queue_(dispatch_queue_create(std::string(queue_name).c_str(),
  57. DISPATCH_QUEUE_SERIAL)),
  58. is_active_(true) {
  59. RTC_CHECK(queue_);
  60. dispatch_set_context(queue_, this);
  61. // Assign a finalizer that will delete the queue when the last reference
  62. // is released. This may run after the TaskQueue::Delete.
  63. dispatch_set_finalizer_f(queue_, &DeleteQueue);
  64. dispatch_set_target_queue(queue_, dispatch_get_global_queue(gcd_priority, 0));
  65. }
  66. TaskQueueGcd::~TaskQueueGcd() = default;
  67. void TaskQueueGcd::Delete() {
  68. RTC_DCHECK(!IsCurrent());
  69. // Implementation/behavioral note:
  70. // Dispatch queues are reference counted via calls to dispatch_retain and
  71. // dispatch_release. Pending blocks submitted to a queue also hold a
  72. // reference to the queue until they have finished. Once all references to a
  73. // queue have been released, the queue will be deallocated by the system.
  74. // This is why we check the is_active_ before running tasks.
  75. // Use dispatch_sync to set the is_active_ to guarantee that there's not a
  76. // race with checking it from a task.
  77. dispatch_sync_f(queue_, this, &SetNotActive);
  78. dispatch_release(queue_);
  79. }
  80. void TaskQueueGcd::PostTask(std::unique_ptr<QueuedTask> task) {
  81. auto* context = new TaskContext(this, std::move(task));
  82. dispatch_async_f(queue_, context, &RunTask);
  83. }
  84. void TaskQueueGcd::PostDelayedTask(std::unique_ptr<QueuedTask> task,
  85. uint32_t milliseconds) {
  86. auto* context = new TaskContext(this, std::move(task));
  87. dispatch_after_f(
  88. dispatch_time(DISPATCH_TIME_NOW, milliseconds * NSEC_PER_MSEC), queue_,
  89. context, &RunTask);
  90. }
  91. // static
  92. void TaskQueueGcd::RunTask(void* task_context) {
  93. std::unique_ptr<TaskContext> tc(static_cast<TaskContext*>(task_context));
  94. if (!tc->queue->is_active_)
  95. return;
  96. CurrentTaskQueueSetter set_current(tc->queue);
  97. auto* task = tc->task.release();
  98. if (task->Run()) {
  99. // Delete the task before CurrentTaskQueueSetter clears state that this code
  100. // is running on the task queue.
  101. delete task;
  102. }
  103. }
  104. // static
  105. void TaskQueueGcd::SetNotActive(void* task_queue) {
  106. static_cast<TaskQueueGcd*>(task_queue)->is_active_ = false;
  107. }
  108. // static
  109. void TaskQueueGcd::DeleteQueue(void* task_queue) {
  110. delete static_cast<TaskQueueGcd*>(task_queue);
  111. }
  112. class TaskQueueGcdFactory final : public TaskQueueFactory {
  113. public:
  114. std::unique_ptr<TaskQueueBase, TaskQueueDeleter> CreateTaskQueue(
  115. absl::string_view name,
  116. Priority priority) const override {
  117. return std::unique_ptr<TaskQueueBase, TaskQueueDeleter>(
  118. new TaskQueueGcd(name, TaskQueuePriorityToGCD(priority)));
  119. }
  120. };
  121. } // namespace
  122. std::unique_ptr<TaskQueueFactory> CreateTaskQueueGcdFactory() {
  123. return std::make_unique<TaskQueueGcdFactory>();
  124. }
  125. } // namespace webrtc