blob: 433d9ea315c4ce2bce4d2504e1b116082535c152 [file] [log] [blame]
eladalon413ee9a2017-08-22 04:02:52 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020010#ifndef TEST_SINGLE_THREADED_TASK_QUEUE_H_
11#define TEST_SINGLE_THREADED_TASK_QUEUE_H_
eladalon413ee9a2017-08-22 04:02:52 -070012
13#include <functional>
Danil Chapovalov71037a82019-09-25 17:21:52 +020014#include <map>
eladalon413ee9a2017-08-22 04:02:52 -070015#include <memory>
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +020016#include <utility>
eladalon413ee9a2017-08-22 04:02:52 -070017
Danil Chapovalov71037a82019-09-25 17:21:52 +020018#include "api/task_queue/task_queue_base.h"
Steve Anton10542f22019-01-11 09:11:00 -080019#include "rtc_base/critical_section.h"
Yves Gerey6516f762019-08-29 11:50:23 +020020#include "rtc_base/deprecation.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/event.h"
22#include "rtc_base/platform_thread.h"
Danil Chapovaloveb90e6f2019-10-15 10:04:57 +020023#include "rtc_base/task_queue_for_test.h"
Danil Chapovalov71037a82019-09-25 17:21:52 +020024#include "rtc_base/task_utils/to_queued_task.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "rtc_base/thread_checker.h"
eladalon413ee9a2017-08-22 04:02:52 -070026
27namespace webrtc {
28namespace test {
29
Yves Gerey6516f762019-08-29 11:50:23 +020030// 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
eladalon413ee9a2017-08-22 04:02:52 -070034// 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 Chapovalov71037a82019-09-25 17:21:52 +020040class DEPRECATED_SingleThreadedTaskQueueForTesting : public TaskQueueBase {
eladalon413ee9a2017-08-22 04:02:52 -070041 public:
42 using Task = std::function<void()>;
43 using TaskId = size_t;
Tommi31d1bce2019-08-27 11:34:20 +020044 constexpr static TaskId kInvalidTaskId = static_cast<TaskId>(-1);
eladalon413ee9a2017-08-22 04:02:52 -070045
Yves Gerey6516f762019-08-29 11:50:23 +020046 explicit DEPRECATED_SingleThreadedTaskQueueForTesting(const char* name);
Danil Chapovalov71037a82019-09-25 17:21:52 +020047 ~DEPRECATED_SingleThreadedTaskQueueForTesting() override;
eladalon413ee9a2017-08-22 04:02:52 -070048
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 Chapovalov71037a82019-09-25 17:21:52 +020053 TaskId PostTask(Task task) {
54 return PostDelayed(ToQueuedTask(std::move(task)), /*delay_ms=*/0);
55 }
eladalon413ee9a2017-08-22 04:02:52 -070056
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 Chapovalov71037a82019-09-25 17:21:52 +020060 TaskId PostDelayedTask(Task task, int64_t delay_ms) {
61 return PostDelayed(ToQueuedTask(std::move(task)), delay_ms);
62 }
eladalon413ee9a2017-08-22 04:02:52 -070063
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 Chapovaloveb90e6f2019-10-15 10:04:57 +020066 // 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 }
eladalon413ee9a2017-08-22 04:02:52 -070072
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
Tommi6e4791f2019-08-14 23:05:44 +020078 // Returns true iff called on the thread associated with the task queue.
79 bool IsCurrent();
80
Tommi31d1bce2019-08-27 11:34:20 +020081 // Returns true iff the task queue is actively being serviced.
82 bool IsRunning();
83
84 bool HasPendingTasks() const;
85
86 void Stop();
87
Danil Chapovalov71037a82019-09-25 17:21:52 +020088 // 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
eladalon413ee9a2017-08-22 04:02:52 -0700100 private:
Danil Chapovalov71037a82019-09-25 17:21:52 +0200101 struct StoredTask {
102 StoredTask(TaskId task_id, std::unique_ptr<QueuedTask> task);
103 ~StoredTask();
eladalon413ee9a2017-08-22 04:02:52 -0700104
105 TaskId task_id;
Danil Chapovalov71037a82019-09-25 17:21:52 +0200106 std::unique_ptr<QueuedTask> task;
eladalon413ee9a2017-08-22 04:02:52 -0700107 };
108
Danil Chapovalov71037a82019-09-25 17:21:52 +0200109 TaskId PostDelayed(std::unique_ptr<QueuedTask> task, int64_t delay_ms);
110
eladalon413ee9a2017-08-22 04:02:52 -0700111 static void Run(void* obj);
112
113 void RunLoop();
114
115 rtc::CriticalSection cs_;
Danil Chapovalov71037a82019-09-25 17:21:52 +0200116 // Tasks are ordered by earliest execution time.
117 std::multimap<int64_t, StoredTask> tasks_ RTC_GUARDED_BY(cs_);
eladalon413ee9a2017-08-22 04:02:52 -0700118 rtc::ThreadChecker owner_thread_checker_;
119 rtc::PlatformThread thread_;
danilchapa37de392017-09-09 04:17:22 -0700120 bool running_ RTC_GUARDED_BY(cs_);
eladalon413ee9a2017-08-22 04:02:52 -0700121
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 Gerey6516f762019-08-29 11:50:23 +0200137// Warn if new usage.
138typedef DEPRECATED_SingleThreadedTaskQueueForTesting RTC_DEPRECATED
139 SingleThreadedTaskQueueForTesting;
140
eladalon413ee9a2017-08-22 04:02:52 -0700141} // namespace test
142} // namespace webrtc
143
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200144#endif // TEST_SINGLE_THREADED_TASK_QUEUE_H_