Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(382)

Side by Side Diff: webrtc/test/single_threaded_task_queue.h

Issue 2998923002: Use SingleThreadedTaskQueue in DirectTransport (Closed)
Patch Set: Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 <utility>
16
17 #include "webrtc/rtc_base/criticalsection.h"
18 #include "webrtc/rtc_base/event.h"
19 #include "webrtc/rtc_base/optional.h"
20 #include "webrtc/rtc_base/platform_thread.h"
21 #include "webrtc/rtc_base/thread_checker.h"
22
23 namespace webrtc {
24 namespace test {
25
26 // TODO(eladalon): !!! Discuss with reviewers - do we want a small unit-test
27 // for this class, which is itself only used for unit-tests?
28
29 // This class gives capabilities similar to rtc::TaskQueue, but insures
30 // everything happens on the same thread. This is intended to make the
31 // threading model of unit-tests (specifically end-to-end tests) more closely
32 // resemble that of real WebRTC, thereby allowing us to replace some critical
33 // sections by thread-checkers.
34 // This task is NOT tuned for performance, but rather for simplicity.
35 class SingleThreadedTaskQueue {
36 public:
37 using Task = std::function<void()>;
38
39 // TODO(eladalon): !!! Is there a way to make this opaque?
40 using TaskIdentifier = size_t;
41
42 explicit SingleThreadedTaskQueue(const char* name);
43 ~SingleThreadedTaskQueue();
44
45 // Sends one task to the task-queue, and returns a handle by which the
46 // task can be cancelled.
47 TaskIdentifier PostTask(Task task);
48
49 // Send one task to the queue. The function does not return until the task
50 // has finished executing. No support for canceling the task.
51 void SendTask(Task task);
52
53 // Given an identifier to the task, attempts to eject it from the queue.
54 // Returns true if the task was found and cancelled. Failure possible
55 // only for invalid task IDs, or for tasks which have already been executed.
56 bool CancelTask(TaskIdentifier task_id);
57
58 private:
59 static void Run(void* obj);
60
61 void RunLoop();
62
63 rtc::CriticalSection cs_;
64 std::list<std::pair<TaskIdentifier, Task>> tasks_ GUARDED_BY(cs_);
65 rtc::ThreadChecker owner_thread_checker_;
66 rtc::PlatformThread thread_;
67 bool running_ GUARDED_BY(cs_);
68
69 TaskIdentifier next_identifier_;
70
71 // By only Set()-ing when going from 0 pending tasks to 1 (or stopping), and
72 // only Wait()-ing when going from 1 pending tasks to 0, we ensure we never
73 // handle two calls to Set() with one call to Wait().
74 rtc::Event event_;
75 };
76
77 } // namespace test
78 } // namespace webrtc
79
80 #endif // WEBRTC_TEST_SINGLE_THREADED_TASK_QUEUE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698