blob: 38458294dea16ab895ec349aab865b350fe21222 [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
eladalon413ee9a2017-08-22 04:02:52 -070064 // Given an identifier to the task, attempts to eject it from the queue.
65 // Returns true if the task was found and cancelled. Failure possible
66 // only for invalid task IDs, or for tasks which have already been executed.
67 bool CancelTask(TaskId task_id);
68
Tommi6e4791f2019-08-14 23:05:44 +020069 // Returns true iff called on the thread associated with the task queue.
70 bool IsCurrent();
71
Tommi31d1bce2019-08-27 11:34:20 +020072 // Returns true iff the task queue is actively being serviced.
73 bool IsRunning();
74
75 bool HasPendingTasks() const;
76
77 void Stop();
78
Danil Chapovalov71037a82019-09-25 17:21:52 +020079 // Implements TaskQueueBase.
80 void Delete() override;
81
82 void PostTask(std::unique_ptr<QueuedTask> task) override {
83 PostDelayed(std::move(task), /*delay_ms=*/0);
84 }
85
86 void PostDelayedTask(std::unique_ptr<QueuedTask> task,
87 uint32_t delay_ms) override {
88 PostDelayed(std::move(task), delay_ms);
89 }
90
eladalon413ee9a2017-08-22 04:02:52 -070091 private:
Danil Chapovalov71037a82019-09-25 17:21:52 +020092 struct StoredTask {
93 StoredTask(TaskId task_id, std::unique_ptr<QueuedTask> task);
94 ~StoredTask();
eladalon413ee9a2017-08-22 04:02:52 -070095
96 TaskId task_id;
Danil Chapovalov71037a82019-09-25 17:21:52 +020097 std::unique_ptr<QueuedTask> task;
eladalon413ee9a2017-08-22 04:02:52 -070098 };
99
Danil Chapovalov71037a82019-09-25 17:21:52 +0200100 TaskId PostDelayed(std::unique_ptr<QueuedTask> task, int64_t delay_ms);
101
eladalon413ee9a2017-08-22 04:02:52 -0700102 static void Run(void* obj);
103
104 void RunLoop();
105
106 rtc::CriticalSection cs_;
Danil Chapovalov71037a82019-09-25 17:21:52 +0200107 // Tasks are ordered by earliest execution time.
108 std::multimap<int64_t, StoredTask> tasks_ RTC_GUARDED_BY(cs_);
eladalon413ee9a2017-08-22 04:02:52 -0700109 rtc::ThreadChecker owner_thread_checker_;
110 rtc::PlatformThread thread_;
danilchapa37de392017-09-09 04:17:22 -0700111 bool running_ RTC_GUARDED_BY(cs_);
eladalon413ee9a2017-08-22 04:02:52 -0700112
113 TaskId next_task_id_;
114
115 // The task-queue will sleep when not executing a task. Wake up occurs when:
116 // * Upon destruction, to make sure that the |thead_| terminates, so that it
117 // may be joined. [Event will be set.]
118 // * New task added. Because we optimize for simplicity rahter than for
119 // performance (this class is a testing facility only), waking up occurs
120 // when we get a new task even if it is scheduled with a delay. The RunLoop
121 // is in charge of sending itself back to sleep if the next task is only
122 // to be executed at a later time. [Event will be set.]
123 // * When the next task in the queue is a delayed-task, and the time for
124 // its execution has come. [Event will time-out.]
125 rtc::Event wake_up_;
126};
127
Yves Gerey6516f762019-08-29 11:50:23 +0200128// Warn if new usage.
129typedef DEPRECATED_SingleThreadedTaskQueueForTesting RTC_DEPRECATED
130 SingleThreadedTaskQueueForTesting;
131
eladalon413ee9a2017-08-22 04:02:52 -0700132} // namespace test
133} // namespace webrtc
134
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200135#endif // TEST_SINGLE_THREADED_TASK_QUEUE_H_