OLD | NEW |
(Empty) | |
| 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 */ |
| 10 #ifndef WEBRTC_TEST_SINGLE_THREADED_TASK_QUEUE_H_ |
| 11 #define WEBRTC_TEST_SINGLE_THREADED_TASK_QUEUE_H_ |
| 12 |
| 13 #include <functional> |
| 14 #include <list> |
| 15 #include <memory> |
| 16 |
| 17 #include "webrtc/rtc_base/criticalsection.h" |
| 18 #include "webrtc/rtc_base/event.h" |
| 19 #include "webrtc/rtc_base/platform_thread.h" |
| 20 #include "webrtc/rtc_base/thread_checker.h" |
| 21 |
| 22 namespace webrtc { |
| 23 namespace test { |
| 24 |
| 25 // This class gives capabilities similar to rtc::TaskQueue, but ensures |
| 26 // everything happens on the same thread. This is intended to make the |
| 27 // threading model of unit-tests (specifically end-to-end tests) more closely |
| 28 // resemble that of real WebRTC, thereby allowing us to replace some critical |
| 29 // sections by thread-checkers. |
| 30 // This task is NOT tuned for performance, but rather for simplicity. |
| 31 class SingleThreadedTaskQueueForTesting { |
| 32 public: |
| 33 using Task = std::function<void()>; |
| 34 using TaskId = size_t; |
| 35 |
| 36 explicit SingleThreadedTaskQueueForTesting(const char* name); |
| 37 ~SingleThreadedTaskQueueForTesting(); |
| 38 |
| 39 // Sends one task to the task-queue, and returns a handle by which the |
| 40 // task can be cancelled. |
| 41 // This mimics the behavior of TaskQueue, but only for lambdas, rather than |
| 42 // for both lambdas and QueuedTask objects. |
| 43 TaskId PostTask(Task task); |
| 44 |
| 45 // Same as PostTask(), but ensures that the task will not begin execution |
| 46 // less than |delay_ms| milliseconds after being posted; an upper bound |
| 47 // is not provided. |
| 48 TaskId PostDelayedTask(Task task, int64_t delay_ms); |
| 49 |
| 50 // Send one task to the queue. The function does not return until the task |
| 51 // has finished executing. No support for canceling the task. |
| 52 void SendTask(Task task); |
| 53 |
| 54 // Given an identifier to the task, attempts to eject it from the queue. |
| 55 // Returns true if the task was found and cancelled. Failure possible |
| 56 // only for invalid task IDs, or for tasks which have already been executed. |
| 57 bool CancelTask(TaskId task_id); |
| 58 |
| 59 private: |
| 60 struct QueuedTask { |
| 61 QueuedTask(TaskId task_id, int64_t earliest_execution_time, Task task); |
| 62 ~QueuedTask(); |
| 63 |
| 64 TaskId task_id; |
| 65 int64_t earliest_execution_time; |
| 66 Task task; |
| 67 }; |
| 68 |
| 69 static void Run(void* obj); |
| 70 |
| 71 void RunLoop(); |
| 72 |
| 73 rtc::CriticalSection cs_; |
| 74 std::list<std::unique_ptr<QueuedTask>> tasks_ GUARDED_BY(cs_); |
| 75 rtc::ThreadChecker owner_thread_checker_; |
| 76 rtc::PlatformThread thread_; |
| 77 bool running_ GUARDED_BY(cs_); |
| 78 |
| 79 TaskId next_task_id_; |
| 80 |
| 81 // The task-queue will sleep when not executing a task. Wake up occurs when: |
| 82 // * Upon destruction, to make sure that the |thead_| terminates, so that it |
| 83 // may be joined. [Event will be set.] |
| 84 // * New task added. Because we optimize for simplicity rahter than for |
| 85 // performance (this class is a testing facility only), waking up occurs |
| 86 // when we get a new task even if it is scheduled with a delay. The RunLoop |
| 87 // is in charge of sending itself back to sleep if the next task is only |
| 88 // to be executed at a later time. [Event will be set.] |
| 89 // * When the next task in the queue is a delayed-task, and the time for |
| 90 // its execution has come. [Event will time-out.] |
| 91 rtc::Event wake_up_; |
| 92 }; |
| 93 |
| 94 } // namespace test |
| 95 } // namespace webrtc |
| 96 |
| 97 #endif // WEBRTC_TEST_SINGLE_THREADED_TASK_QUEUE_H_ |
OLD | NEW |