blob: b6c1e6e2659dc9f9006b423fb3eda469b4fa22c2 [file] [log] [blame]
Sebastian Janssonfc8279d2020-01-16 11:45:59 +01001/*
2 * Copyright (c) 2020 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 TEST_TIME_CONTROLLER_SIMULATED_THREAD_H_
11#define TEST_TIME_CONTROLLER_SIMULATED_THREAD_H_
12
13#include <memory>
14
Markus Handelle56976d2020-07-08 17:34:37 +020015#include "rtc_base/synchronization/mutex.h"
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010016#include "test/time_controller/simulated_time_controller.h"
17
18namespace webrtc {
19
20class SimulatedThread : public rtc::Thread,
21 public sim_time_impl::SimulatedSequenceRunner {
22 public:
Sebastian Jansson6ce033a2020-01-22 10:12:56 +010023 using CurrentThreadSetter = CurrentThreadSetter;
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010024 SimulatedThread(sim_time_impl::SimulatedTimeControllerImpl* handler,
25 absl::string_view name,
26 std::unique_ptr<rtc::SocketServer> socket_server);
27 ~SimulatedThread() override;
28
29 void RunReady(Timestamp at_time) override;
30
31 Timestamp GetNextRunTime() const override {
Markus Handelle56976d2020-07-08 17:34:37 +020032 MutexLock lock(&lock_);
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010033 return next_run_time_;
34 }
35
36 TaskQueueBase* GetAsTaskQueue() override { return this; }
37
38 // Thread interface
39 void Send(const rtc::Location& posted_from,
40 rtc::MessageHandler* phandler,
41 uint32_t id,
42 rtc::MessageData* pdata) override;
43 void Post(const rtc::Location& posted_from,
44 rtc::MessageHandler* phandler,
45 uint32_t id,
46 rtc::MessageData* pdata,
47 bool time_sensitive) override;
48 void PostDelayed(const rtc::Location& posted_from,
49 int delay_ms,
50 rtc::MessageHandler* phandler,
51 uint32_t id,
52 rtc::MessageData* pdata) override;
53 void PostAt(const rtc::Location& posted_from,
54 int64_t target_time_ms,
55 rtc::MessageHandler* phandler,
56 uint32_t id,
57 rtc::MessageData* pdata) override;
58
59 void Stop() override;
60
61 private:
62 sim_time_impl::SimulatedTimeControllerImpl* const handler_;
63 // Using char* to be debugger friendly.
64 char* name_;
Markus Handelle56976d2020-07-08 17:34:37 +020065 mutable Mutex lock_;
Sebastian Janssonfc8279d2020-01-16 11:45:59 +010066 Timestamp next_run_time_ RTC_GUARDED_BY(lock_) = Timestamp::PlusInfinity();
67};
68
69class SimulatedMainThread : public SimulatedThread {
70 public:
71 explicit SimulatedMainThread(
72 sim_time_impl::SimulatedTimeControllerImpl* handler);
73 ~SimulatedMainThread();
74
75 private:
76 CurrentThreadSetter current_setter_;
77};
78} // namespace webrtc
79#endif // TEST_TIME_CONTROLLER_SIMULATED_THREAD_H_