blob: 52316c66e95b3072efaa66f3b9735fc3ffd2b7df [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>
16
Danil Chapovalov71037a82019-09-25 17:21:52 +020017#include "api/task_queue/task_queue_base.h"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "rtc_base/critical_section.h"
Yves Gerey6516f762019-08-29 11:50:23 +020019#include "rtc_base/deprecation.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/event.h"
21#include "rtc_base/platform_thread.h"
Danil Chapovalov71037a82019-09-25 17:21:52 +020022#include "rtc_base/task_utils/to_queued_task.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "rtc_base/thread_checker.h"
eladalon413ee9a2017-08-22 04:02:52 -070024
25namespace webrtc {
26namespace test {
27
Yves Gerey6516f762019-08-29 11:50:23 +020028// 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
eladalon413ee9a2017-08-22 04:02:52 -070032// 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 Chapovalov71037a82019-09-25 17:21:52 +020038class DEPRECATED_SingleThreadedTaskQueueForTesting : public TaskQueueBase {
eladalon413ee9a2017-08-22 04:02:52 -070039 public:
40 using Task = std::function<void()>;
41 using TaskId = size_t;
Tommi31d1bce2019-08-27 11:34:20 +020042 constexpr static TaskId kInvalidTaskId = static_cast<TaskId>(-1);
eladalon413ee9a2017-08-22 04:02:52 -070043
Yves Gerey6516f762019-08-29 11:50:23 +020044 explicit DEPRECATED_SingleThreadedTaskQueueForTesting(const char* name);
Danil Chapovalov71037a82019-09-25 17:21:52 +020045 ~DEPRECATED_SingleThreadedTaskQueueForTesting() override;
eladalon413ee9a2017-08-22 04:02:52 -070046
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 Chapovalov71037a82019-09-25 17:21:52 +020051 TaskId PostTask(Task task) {
52 return PostDelayed(ToQueuedTask(std::move(task)), /*delay_ms=*/0);
53 }
eladalon413ee9a2017-08-22 04:02:52 -070054
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 Chapovalov71037a82019-09-25 17:21:52 +020058 TaskId PostDelayedTask(Task task, int64_t delay_ms) {
59 return PostDelayed(ToQueuedTask(std::move(task)), delay_ms);
60 }
eladalon413ee9a2017-08-22 04:02:52 -070061
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
Tommi6e4791f2019-08-14 23:05:44 +020071 // Returns true iff called on the thread associated with the task queue.
72 bool IsCurrent();
73
Tommi31d1bce2019-08-27 11:34:20 +020074 // Returns true iff the task queue is actively being serviced.
75 bool IsRunning();
76
77 bool HasPendingTasks() const;
78
79 void Stop();
80
Danil Chapovalov71037a82019-09-25 17:21:52 +020081 // 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
eladalon413ee9a2017-08-22 04:02:52 -070093 private:
Danil Chapovalov71037a82019-09-25 17:21:52 +020094 struct StoredTask {
95 StoredTask(TaskId task_id, std::unique_ptr<QueuedTask> task);
96 ~StoredTask();
eladalon413ee9a2017-08-22 04:02:52 -070097
98 TaskId task_id;
Danil Chapovalov71037a82019-09-25 17:21:52 +020099 std::unique_ptr<QueuedTask> task;
eladalon413ee9a2017-08-22 04:02:52 -0700100 };
101
Danil Chapovalov71037a82019-09-25 17:21:52 +0200102 TaskId PostDelayed(std::unique_ptr<QueuedTask> task, int64_t delay_ms);
103
eladalon413ee9a2017-08-22 04:02:52 -0700104 static void Run(void* obj);
105
106 void RunLoop();
107
108 rtc::CriticalSection cs_;
Danil Chapovalov71037a82019-09-25 17:21:52 +0200109 // Tasks are ordered by earliest execution time.
110 std::multimap<int64_t, StoredTask> tasks_ RTC_GUARDED_BY(cs_);
eladalon413ee9a2017-08-22 04:02:52 -0700111 rtc::ThreadChecker owner_thread_checker_;
112 rtc::PlatformThread thread_;
danilchapa37de392017-09-09 04:17:22 -0700113 bool running_ RTC_GUARDED_BY(cs_);
eladalon413ee9a2017-08-22 04:02:52 -0700114
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 Gerey6516f762019-08-29 11:50:23 +0200130// Warn if new usage.
131typedef DEPRECATED_SingleThreadedTaskQueueForTesting RTC_DEPRECATED
132 SingleThreadedTaskQueueForTesting;
133
eladalon413ee9a2017-08-22 04:02:52 -0700134} // namespace test
135} // namespace webrtc
136
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200137#endif // TEST_SINGLE_THREADED_TASK_QUEUE_H_