eladalon | 413ee9a | 2017-08-22 04:02:52 -0700 | [diff] [blame] | 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 | */ |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 10 | #ifndef TEST_SINGLE_THREADED_TASK_QUEUE_H_ |
| 11 | #define TEST_SINGLE_THREADED_TASK_QUEUE_H_ |
eladalon | 413ee9a | 2017-08-22 04:02:52 -0700 | [diff] [blame] | 12 | |
| 13 | #include <functional> |
Danil Chapovalov | 71037a8 | 2019-09-25 17:21:52 +0200 | [diff] [blame] | 14 | #include <map> |
eladalon | 413ee9a | 2017-08-22 04:02:52 -0700 | [diff] [blame] | 15 | #include <memory> |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame] | 16 | #include <utility> |
eladalon | 413ee9a | 2017-08-22 04:02:52 -0700 | [diff] [blame] | 17 | |
Danil Chapovalov | 71037a8 | 2019-09-25 17:21:52 +0200 | [diff] [blame] | 18 | #include "api/task_queue/task_queue_base.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 19 | #include "rtc_base/critical_section.h" |
Yves Gerey | 6516f76 | 2019-08-29 11:50:23 +0200 | [diff] [blame] | 20 | #include "rtc_base/deprecation.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "rtc_base/event.h" |
| 22 | #include "rtc_base/platform_thread.h" |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame] | 23 | #include "rtc_base/task_queue_for_test.h" |
Danil Chapovalov | 71037a8 | 2019-09-25 17:21:52 +0200 | [diff] [blame] | 24 | #include "rtc_base/task_utils/to_queued_task.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "rtc_base/thread_checker.h" |
eladalon | 413ee9a | 2017-08-22 04:02:52 -0700 | [diff] [blame] | 26 | |
| 27 | namespace webrtc { |
| 28 | namespace test { |
| 29 | |
Yves Gerey | 6516f76 | 2019-08-29 11:50:23 +0200 | [diff] [blame] | 30 | // DEPRECATED. This class doesn't striclty follow rtc::TaskQueue semantics, |
| 31 | // which makes it surprising and hard to use correctly. |
| 32 | // Please use TaskQueueForTest instead. |
| 33 | |
eladalon | 413ee9a | 2017-08-22 04:02:52 -0700 | [diff] [blame] | 34 | // This class gives capabilities similar to rtc::TaskQueue, but ensures |
| 35 | // everything happens on the same thread. This is intended to make the |
| 36 | // threading model of unit-tests (specifically end-to-end tests) more closely |
| 37 | // resemble that of real WebRTC, thereby allowing us to replace some critical |
| 38 | // sections by thread-checkers. |
| 39 | // This task is NOT tuned for performance, but rather for simplicity. |
Danil Chapovalov | 71037a8 | 2019-09-25 17:21:52 +0200 | [diff] [blame] | 40 | class DEPRECATED_SingleThreadedTaskQueueForTesting : public TaskQueueBase { |
eladalon | 413ee9a | 2017-08-22 04:02:52 -0700 | [diff] [blame] | 41 | public: |
| 42 | using Task = std::function<void()>; |
| 43 | using TaskId = size_t; |
Tommi | 31d1bce | 2019-08-27 11:34:20 +0200 | [diff] [blame] | 44 | constexpr static TaskId kInvalidTaskId = static_cast<TaskId>(-1); |
eladalon | 413ee9a | 2017-08-22 04:02:52 -0700 | [diff] [blame] | 45 | |
Yves Gerey | 6516f76 | 2019-08-29 11:50:23 +0200 | [diff] [blame] | 46 | explicit DEPRECATED_SingleThreadedTaskQueueForTesting(const char* name); |
Danil Chapovalov | 71037a8 | 2019-09-25 17:21:52 +0200 | [diff] [blame] | 47 | ~DEPRECATED_SingleThreadedTaskQueueForTesting() override; |
eladalon | 413ee9a | 2017-08-22 04:02:52 -0700 | [diff] [blame] | 48 | |
| 49 | // Sends one task to the task-queue, and returns a handle by which the |
| 50 | // task can be cancelled. |
| 51 | // This mimics the behavior of TaskQueue, but only for lambdas, rather than |
| 52 | // for both lambdas and QueuedTask objects. |
Danil Chapovalov | 71037a8 | 2019-09-25 17:21:52 +0200 | [diff] [blame] | 53 | TaskId PostTask(Task task) { |
| 54 | return PostDelayed(ToQueuedTask(std::move(task)), /*delay_ms=*/0); |
| 55 | } |
eladalon | 413ee9a | 2017-08-22 04:02:52 -0700 | [diff] [blame] | 56 | |
| 57 | // Same as PostTask(), but ensures that the task will not begin execution |
| 58 | // less than |delay_ms| milliseconds after being posted; an upper bound |
| 59 | // is not provided. |
Danil Chapovalov | 71037a8 | 2019-09-25 17:21:52 +0200 | [diff] [blame] | 60 | TaskId PostDelayedTask(Task task, int64_t delay_ms) { |
| 61 | return PostDelayed(ToQueuedTask(std::move(task)), delay_ms); |
| 62 | } |
eladalon | 413ee9a | 2017-08-22 04:02:52 -0700 | [diff] [blame] | 63 | |
| 64 | // Send one task to the queue. The function does not return until the task |
| 65 | // has finished executing. No support for canceling the task. |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame] | 66 | // TODO(bugs.webrtc.org/10933): Remove this function in favor of free SendTask |
| 67 | // to reduce direct mentioning of the SingleThreadedTaskQueueForTesting class. |
| 68 | template <typename Closure> |
| 69 | void SendTask(Closure&& task) { |
| 70 | ::webrtc::SendTask(this, std::forward<Closure>(task), RTC_FROM_HERE); |
| 71 | } |
eladalon | 413ee9a | 2017-08-22 04:02:52 -0700 | [diff] [blame] | 72 | |
| 73 | // Given an identifier to the task, attempts to eject it from the queue. |
| 74 | // Returns true if the task was found and cancelled. Failure possible |
| 75 | // only for invalid task IDs, or for tasks which have already been executed. |
| 76 | bool CancelTask(TaskId task_id); |
| 77 | |
Tommi | 6e4791f | 2019-08-14 23:05:44 +0200 | [diff] [blame] | 78 | // Returns true iff called on the thread associated with the task queue. |
| 79 | bool IsCurrent(); |
| 80 | |
Tommi | 31d1bce | 2019-08-27 11:34:20 +0200 | [diff] [blame] | 81 | // Returns true iff the task queue is actively being serviced. |
| 82 | bool IsRunning(); |
| 83 | |
| 84 | bool HasPendingTasks() const; |
| 85 | |
| 86 | void Stop(); |
| 87 | |
Danil Chapovalov | 71037a8 | 2019-09-25 17:21:52 +0200 | [diff] [blame] | 88 | // Implements TaskQueueBase. |
| 89 | void Delete() override; |
| 90 | |
| 91 | void PostTask(std::unique_ptr<QueuedTask> task) override { |
| 92 | PostDelayed(std::move(task), /*delay_ms=*/0); |
| 93 | } |
| 94 | |
| 95 | void PostDelayedTask(std::unique_ptr<QueuedTask> task, |
| 96 | uint32_t delay_ms) override { |
| 97 | PostDelayed(std::move(task), delay_ms); |
| 98 | } |
| 99 | |
eladalon | 413ee9a | 2017-08-22 04:02:52 -0700 | [diff] [blame] | 100 | private: |
Danil Chapovalov | 71037a8 | 2019-09-25 17:21:52 +0200 | [diff] [blame] | 101 | struct StoredTask { |
| 102 | StoredTask(TaskId task_id, std::unique_ptr<QueuedTask> task); |
| 103 | ~StoredTask(); |
eladalon | 413ee9a | 2017-08-22 04:02:52 -0700 | [diff] [blame] | 104 | |
| 105 | TaskId task_id; |
Danil Chapovalov | 71037a8 | 2019-09-25 17:21:52 +0200 | [diff] [blame] | 106 | std::unique_ptr<QueuedTask> task; |
eladalon | 413ee9a | 2017-08-22 04:02:52 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
Danil Chapovalov | 71037a8 | 2019-09-25 17:21:52 +0200 | [diff] [blame] | 109 | TaskId PostDelayed(std::unique_ptr<QueuedTask> task, int64_t delay_ms); |
| 110 | |
eladalon | 413ee9a | 2017-08-22 04:02:52 -0700 | [diff] [blame] | 111 | static void Run(void* obj); |
| 112 | |
| 113 | void RunLoop(); |
| 114 | |
| 115 | rtc::CriticalSection cs_; |
Danil Chapovalov | 71037a8 | 2019-09-25 17:21:52 +0200 | [diff] [blame] | 116 | // Tasks are ordered by earliest execution time. |
| 117 | std::multimap<int64_t, StoredTask> tasks_ RTC_GUARDED_BY(cs_); |
eladalon | 413ee9a | 2017-08-22 04:02:52 -0700 | [diff] [blame] | 118 | rtc::ThreadChecker owner_thread_checker_; |
| 119 | rtc::PlatformThread thread_; |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 120 | bool running_ RTC_GUARDED_BY(cs_); |
eladalon | 413ee9a | 2017-08-22 04:02:52 -0700 | [diff] [blame] | 121 | |
| 122 | TaskId next_task_id_; |
| 123 | |
| 124 | // The task-queue will sleep when not executing a task. Wake up occurs when: |
| 125 | // * Upon destruction, to make sure that the |thead_| terminates, so that it |
| 126 | // may be joined. [Event will be set.] |
| 127 | // * New task added. Because we optimize for simplicity rahter than for |
| 128 | // performance (this class is a testing facility only), waking up occurs |
| 129 | // when we get a new task even if it is scheduled with a delay. The RunLoop |
| 130 | // is in charge of sending itself back to sleep if the next task is only |
| 131 | // to be executed at a later time. [Event will be set.] |
| 132 | // * When the next task in the queue is a delayed-task, and the time for |
| 133 | // its execution has come. [Event will time-out.] |
| 134 | rtc::Event wake_up_; |
| 135 | }; |
| 136 | |
Yves Gerey | 6516f76 | 2019-08-29 11:50:23 +0200 | [diff] [blame] | 137 | // Warn if new usage. |
| 138 | typedef DEPRECATED_SingleThreadedTaskQueueForTesting RTC_DEPRECATED |
| 139 | SingleThreadedTaskQueueForTesting; |
| 140 | |
eladalon | 413ee9a | 2017-08-22 04:02:52 -0700 | [diff] [blame] | 141 | } // namespace test |
| 142 | } // namespace webrtc |
| 143 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 144 | #endif // TEST_SINGLE_THREADED_TASK_QUEUE_H_ |