blob: 00126730fbf8ec68d0ee5e8045543d07d1e07d94 [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>
14#include <list>
15#include <memory>
16
Steve Anton10542f22019-01-11 09:11:00 -080017#include "rtc_base/critical_section.h"
Yves Gerey6516f762019-08-29 11:50:23 +020018#include "rtc_base/deprecation.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/event.h"
20#include "rtc_base/platform_thread.h"
21#include "rtc_base/thread_checker.h"
eladalon413ee9a2017-08-22 04:02:52 -070022
23namespace webrtc {
24namespace test {
25
Yves Gerey6516f762019-08-29 11:50:23 +020026// DEPRECATED. This class doesn't striclty follow rtc::TaskQueue semantics,
27// which makes it surprising and hard to use correctly.
28// Please use TaskQueueForTest instead.
29
eladalon413ee9a2017-08-22 04:02:52 -070030// This class gives capabilities similar to rtc::TaskQueue, but ensures
31// everything happens on the same thread. This is intended to make the
32// threading model of unit-tests (specifically end-to-end tests) more closely
33// resemble that of real WebRTC, thereby allowing us to replace some critical
34// sections by thread-checkers.
35// This task is NOT tuned for performance, but rather for simplicity.
Yves Gerey6516f762019-08-29 11:50:23 +020036class DEPRECATED_SingleThreadedTaskQueueForTesting {
eladalon413ee9a2017-08-22 04:02:52 -070037 public:
38 using Task = std::function<void()>;
39 using TaskId = size_t;
Tommi31d1bce2019-08-27 11:34:20 +020040 constexpr static TaskId kInvalidTaskId = static_cast<TaskId>(-1);
eladalon413ee9a2017-08-22 04:02:52 -070041
Yves Gerey6516f762019-08-29 11:50:23 +020042 explicit DEPRECATED_SingleThreadedTaskQueueForTesting(const char* name);
43 ~DEPRECATED_SingleThreadedTaskQueueForTesting();
eladalon413ee9a2017-08-22 04:02:52 -070044
45 // Sends one task to the task-queue, and returns a handle by which the
46 // task can be cancelled.
47 // This mimics the behavior of TaskQueue, but only for lambdas, rather than
48 // for both lambdas and QueuedTask objects.
49 TaskId PostTask(Task task);
50
51 // Same as PostTask(), but ensures that the task will not begin execution
52 // less than |delay_ms| milliseconds after being posted; an upper bound
53 // is not provided.
54 TaskId PostDelayedTask(Task task, int64_t delay_ms);
55
56 // Send one task to the queue. The function does not return until the task
57 // has finished executing. No support for canceling the task.
58 void SendTask(Task task);
59
60 // Given an identifier to the task, attempts to eject it from the queue.
61 // Returns true if the task was found and cancelled. Failure possible
62 // only for invalid task IDs, or for tasks which have already been executed.
63 bool CancelTask(TaskId task_id);
64
Tommi6e4791f2019-08-14 23:05:44 +020065 // Returns true iff called on the thread associated with the task queue.
66 bool IsCurrent();
67
Tommi31d1bce2019-08-27 11:34:20 +020068 // Returns true iff the task queue is actively being serviced.
69 bool IsRunning();
70
71 bool HasPendingTasks() const;
72
73 void Stop();
74
eladalon413ee9a2017-08-22 04:02:52 -070075 private:
76 struct QueuedTask {
77 QueuedTask(TaskId task_id, int64_t earliest_execution_time, Task task);
78 ~QueuedTask();
79
80 TaskId task_id;
81 int64_t earliest_execution_time;
82 Task task;
83 };
84
85 static void Run(void* obj);
86
87 void RunLoop();
88
89 rtc::CriticalSection cs_;
danilchapa37de392017-09-09 04:17:22 -070090 std::list<std::unique_ptr<QueuedTask>> tasks_ RTC_GUARDED_BY(cs_);
eladalon413ee9a2017-08-22 04:02:52 -070091 rtc::ThreadChecker owner_thread_checker_;
92 rtc::PlatformThread thread_;
danilchapa37de392017-09-09 04:17:22 -070093 bool running_ RTC_GUARDED_BY(cs_);
eladalon413ee9a2017-08-22 04:02:52 -070094
95 TaskId next_task_id_;
96
97 // The task-queue will sleep when not executing a task. Wake up occurs when:
98 // * Upon destruction, to make sure that the |thead_| terminates, so that it
99 // may be joined. [Event will be set.]
100 // * New task added. Because we optimize for simplicity rahter than for
101 // performance (this class is a testing facility only), waking up occurs
102 // when we get a new task even if it is scheduled with a delay. The RunLoop
103 // is in charge of sending itself back to sleep if the next task is only
104 // to be executed at a later time. [Event will be set.]
105 // * When the next task in the queue is a delayed-task, and the time for
106 // its execution has come. [Event will time-out.]
107 rtc::Event wake_up_;
108};
109
Yves Gerey6516f762019-08-29 11:50:23 +0200110// Warn if new usage.
111typedef DEPRECATED_SingleThreadedTaskQueueForTesting RTC_DEPRECATED
112 SingleThreadedTaskQueueForTesting;
113
eladalon413ee9a2017-08-22 04:02:52 -0700114} // namespace test
115} // namespace webrtc
116
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200117#endif // TEST_SINGLE_THREADED_TASK_QUEUE_H_