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