Index: webrtc/test/single_threaded_task_queue.h |
diff --git a/webrtc/test/single_threaded_task_queue.h b/webrtc/test/single_threaded_task_queue.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..8ac55c0f1ed799eb47fdf06983e81beb5575da0f |
--- /dev/null |
+++ b/webrtc/test/single_threaded_task_queue.h |
@@ -0,0 +1,80 @@ |
+/* |
+ * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
+ * |
+ * Use of this source code is governed by a BSD-style license |
+ * that can be found in the LICENSE file in the root of the source |
+ * tree. An additional intellectual property rights grant can be found |
+ * in the file PATENTS. All contributing project authors may |
+ * be found in the AUTHORS file in the root of the source tree. |
+ */ |
+#ifndef WEBRTC_TEST_SINGLE_THREADED_TASK_QUEUE_H_ |
+#define WEBRTC_TEST_SINGLE_THREADED_TASK_QUEUE_H_ |
+ |
+#include <functional> |
+#include <list> |
+#include <utility> |
+ |
+#include "webrtc/rtc_base/criticalsection.h" |
+#include "webrtc/rtc_base/event.h" |
+#include "webrtc/rtc_base/optional.h" |
+#include "webrtc/rtc_base/platform_thread.h" |
+#include "webrtc/rtc_base/thread_checker.h" |
+ |
+namespace webrtc { |
+namespace test { |
+ |
+// TODO(eladalon): !!! Discuss with reviewers - do we want a small unit-test |
+// for this class, which is itself only used for unit-tests? |
+ |
+// This class gives capabilities similar to rtc::TaskQueue, but insures |
+// everything happens on the same thread. This is intended to make the |
+// threading model of unit-tests (specifically end-to-end tests) more closely |
+// resemble that of real WebRTC, thereby allowing us to replace some critical |
+// sections by thread-checkers. |
+// This task is NOT tuned for performance, but rather for simplicity. |
+class SingleThreadedTaskQueue { |
+ public: |
+ using Task = std::function<void()>; |
+ |
+ // TODO(eladalon): !!! Is there a way to make this opaque? |
+ using TaskIdentifier = size_t; |
+ |
+ explicit SingleThreadedTaskQueue(const char* name); |
+ ~SingleThreadedTaskQueue(); |
+ |
+ // Sends one task to the task-queue, and returns a handle by which the |
+ // task can be cancelled. |
+ TaskIdentifier PostTask(Task task); |
+ |
+ // Send one task to the queue. The function does not return until the task |
+ // has finished executing. No support for canceling the task. |
+ void SendTask(Task task); |
+ |
+ // Given an identifier to the task, attempts to eject it from the queue. |
+ // Returns true if the task was found and cancelled. Failure possible |
+ // only for invalid task IDs, or for tasks which have already been executed. |
+ bool CancelTask(TaskIdentifier task_id); |
+ |
+ private: |
+ static void Run(void* obj); |
+ |
+ void RunLoop(); |
+ |
+ rtc::CriticalSection cs_; |
+ std::list<std::pair<TaskIdentifier, Task>> tasks_ GUARDED_BY(cs_); |
+ rtc::ThreadChecker owner_thread_checker_; |
+ rtc::PlatformThread thread_; |
+ bool running_ GUARDED_BY(cs_); |
+ |
+ TaskIdentifier next_identifier_; |
+ |
+ // By only Set()-ing when going from 0 pending tasks to 1 (or stopping), and |
+ // only Wait()-ing when going from 1 pending tasks to 0, we ensure we never |
+ // handle two calls to Set() with one call to Wait(). |
+ rtc::Event event_; |
+}; |
+ |
+} // namespace test |
+} // namespace webrtc |
+ |
+#endif // WEBRTC_TEST_SINGLE_THREADED_TASK_QUEUE_H_ |