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.

135 lines
4.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2017 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 TEST_SINGLE_THREADED_TASK_QUEUE_H_
  11. #define TEST_SINGLE_THREADED_TASK_QUEUE_H_
  12. #include <functional>
  13. #include <map>
  14. #include <memory>
  15. #include <utility>
  16. #include "api/task_queue/task_queue_base.h"
  17. #include "rtc_base/critical_section.h"
  18. #include "rtc_base/deprecation.h"
  19. #include "rtc_base/event.h"
  20. #include "rtc_base/platform_thread.h"
  21. #include "rtc_base/task_queue_for_test.h"
  22. #include "rtc_base/task_utils/to_queued_task.h"
  23. #include "rtc_base/thread_checker.h"
  24. namespace webrtc {
  25. namespace test {
  26. // DEPRECATED. This class doesn't striclty follow rtc::TaskQueue semantics,
  27. // which makes it surprising and hard to use correctly.
  28. // Please use TaskQueueForTest instead.
  29. // This class gives capabilities similar to rtc::TaskQueue, but ensures
  30. // everything happens on the same thread. This is intended to make the
  31. // threading model of unit-tests (specifically end-to-end tests) more closely
  32. // resemble that of real WebRTC, thereby allowing us to replace some critical
  33. // sections by thread-checkers.
  34. // This task is NOT tuned for performance, but rather for simplicity.
  35. class DEPRECATED_SingleThreadedTaskQueueForTesting : public TaskQueueBase {
  36. public:
  37. using Task = std::function<void()>;
  38. using TaskId = size_t;
  39. constexpr static TaskId kInvalidTaskId = static_cast<TaskId>(-1);
  40. explicit DEPRECATED_SingleThreadedTaskQueueForTesting(const char* name);
  41. ~DEPRECATED_SingleThreadedTaskQueueForTesting() override;
  42. // Sends one task to the task-queue, and returns a handle by which the
  43. // task can be cancelled.
  44. // This mimics the behavior of TaskQueue, but only for lambdas, rather than
  45. // for both lambdas and QueuedTask objects.
  46. TaskId PostTask(Task task) {
  47. return PostDelayed(ToQueuedTask(std::move(task)), /*delay_ms=*/0);
  48. }
  49. // Same as PostTask(), but ensures that the task will not begin execution
  50. // less than |delay_ms| milliseconds after being posted; an upper bound
  51. // is not provided.
  52. TaskId PostDelayedTask(Task task, int64_t delay_ms) {
  53. return PostDelayed(ToQueuedTask(std::move(task)), delay_ms);
  54. }
  55. // Given an identifier to the task, attempts to eject it from the queue.
  56. // Returns true if the task was found and cancelled. Failure possible
  57. // only for invalid task IDs, or for tasks which have already been executed.
  58. bool CancelTask(TaskId task_id);
  59. // Returns true iff called on the thread associated with the task queue.
  60. bool IsCurrent();
  61. // Returns true iff the task queue is actively being serviced.
  62. bool IsRunning();
  63. bool HasPendingTasks() const;
  64. void Stop();
  65. // Implements TaskQueueBase.
  66. void Delete() override;
  67. void PostTask(std::unique_ptr<QueuedTask> task) override {
  68. PostDelayed(std::move(task), /*delay_ms=*/0);
  69. }
  70. void PostDelayedTask(std::unique_ptr<QueuedTask> task,
  71. uint32_t delay_ms) override {
  72. PostDelayed(std::move(task), delay_ms);
  73. }
  74. private:
  75. struct StoredTask {
  76. StoredTask(TaskId task_id, std::unique_ptr<QueuedTask> task);
  77. ~StoredTask();
  78. TaskId task_id;
  79. std::unique_ptr<QueuedTask> task;
  80. };
  81. TaskId PostDelayed(std::unique_ptr<QueuedTask> task, int64_t delay_ms);
  82. static void Run(void* obj);
  83. void RunLoop();
  84. rtc::CriticalSection cs_;
  85. // Tasks are ordered by earliest execution time.
  86. std::multimap<int64_t, StoredTask> tasks_ RTC_GUARDED_BY(cs_);
  87. rtc::ThreadChecker owner_thread_checker_;
  88. rtc::PlatformThread thread_;
  89. bool running_ RTC_GUARDED_BY(cs_);
  90. TaskId next_task_id_;
  91. // The task-queue will sleep when not executing a task. Wake up occurs when:
  92. // * Upon destruction, to make sure that the |thead_| terminates, so that it
  93. // may be joined. [Event will be set.]
  94. // * New task added. Because we optimize for simplicity rahter than for
  95. // performance (this class is a testing facility only), waking up occurs
  96. // when we get a new task even if it is scheduled with a delay. The RunLoop
  97. // is in charge of sending itself back to sleep if the next task is only
  98. // to be executed at a later time. [Event will be set.]
  99. // * When the next task in the queue is a delayed-task, and the time for
  100. // its execution has come. [Event will time-out.]
  101. rtc::Event wake_up_;
  102. };
  103. // Warn if new usage.
  104. typedef DEPRECATED_SingleThreadedTaskQueueForTesting RTC_DEPRECATED
  105. SingleThreadedTaskQueueForTesting;
  106. } // namespace test
  107. } // namespace webrtc
  108. #endif // TEST_SINGLE_THREADED_TASK_QUEUE_H_