xref: /aosp_15_r20/external/webrtc/test/time_controller/simulated_thread.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
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 
15 #include "rtc_base/synchronization/mutex.h"
16 #include "test/time_controller/simulated_time_controller.h"
17 
18 namespace webrtc {
19 
20 class SimulatedThread : public rtc::Thread,
21                         public sim_time_impl::SimulatedSequenceRunner {
22  public:
23   using CurrentThreadSetter = CurrentThreadSetter;
24   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 
GetNextRunTime()31   Timestamp GetNextRunTime() const override {
32     MutexLock lock(&lock_);
33     return next_run_time_;
34   }
35 
GetAsTaskQueue()36   TaskQueueBase* GetAsTaskQueue() override { return this; }
37 
38   // Thread interface
39   void BlockingCall(rtc::FunctionView<void()> functor) override;
40   void PostTask(absl::AnyInvocable<void() &&> task) override;
41   void PostDelayedTask(absl::AnyInvocable<void() &&> task,
42                        TimeDelta delay) override;
43   void PostDelayedHighPrecisionTask(absl::AnyInvocable<void() &&> task,
44                                     TimeDelta delay) override;
45 
46   void Stop() override;
47 
48  private:
49   sim_time_impl::SimulatedTimeControllerImpl* const handler_;
50   // Using char* to be debugger friendly.
51   char* name_;
52   mutable Mutex lock_;
53   Timestamp next_run_time_ RTC_GUARDED_BY(lock_) = Timestamp::PlusInfinity();
54 };
55 
56 class SimulatedMainThread : public SimulatedThread {
57  public:
58   explicit SimulatedMainThread(
59       sim_time_impl::SimulatedTimeControllerImpl* handler);
60   ~SimulatedMainThread();
61 
62  private:
63   CurrentThreadSetter current_setter_;
64 };
65 }  // namespace webrtc
66 #endif  // TEST_TIME_CONTROLLER_SIMULATED_THREAD_H_
67