• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_PROCESS_THREAD_H_
11 #define TEST_TIME_CONTROLLER_SIMULATED_PROCESS_THREAD_H_
12 
13 #include <deque>
14 #include <list>
15 #include <map>
16 #include <memory>
17 #include <vector>
18 
19 #include "rtc_base/synchronization/mutex.h"
20 #include "test/time_controller/simulated_time_controller.h"
21 
22 namespace webrtc {
23 
24 class SimulatedProcessThread : public ProcessThread,
25                                public sim_time_impl::SimulatedSequenceRunner {
26  public:
27   SimulatedProcessThread(sim_time_impl::SimulatedTimeControllerImpl* handler,
28                          absl::string_view name);
29   virtual ~SimulatedProcessThread();
30   void RunReady(Timestamp at_time) override;
31 
GetNextRunTime()32   Timestamp GetNextRunTime() const override {
33     MutexLock lock(&lock_);
34     return next_run_time_;
35   }
36 
GetAsTaskQueue()37   TaskQueueBase* GetAsTaskQueue() override { return this; }
38 
39   // ProcessThread interface
40   void Start() override;
41   void Stop() override;
42   void WakeUp(Module* module) override;
43   void RegisterModule(Module* module, const rtc::Location& from) override;
44   void DeRegisterModule(Module* module) override;
45   void PostTask(std::unique_ptr<QueuedTask> task) override;
46   void PostDelayedTask(std::unique_ptr<QueuedTask> task,
47                        uint32_t milliseconds) override;
48 
49  private:
Delete()50   void Delete() override {
51     // ProcessThread shouldn't be deleted as a TaskQueue.
52     RTC_NOTREACHED();
53   }
54   Timestamp GetNextTime(Module* module, Timestamp at_time);
55 
56   sim_time_impl::SimulatedTimeControllerImpl* const handler_;
57   // Using char* to be debugger friendly.
58   char* name_;
59   mutable Mutex lock_;
60   Timestamp next_run_time_ RTC_GUARDED_BY(lock_) = Timestamp::PlusInfinity();
61 
62   std::deque<std::unique_ptr<QueuedTask>> queue_;
63   std::map<Timestamp, std::vector<std::unique_ptr<QueuedTask>>> delayed_tasks_
64       RTC_GUARDED_BY(lock_);
65 
66   bool process_thread_running_ RTC_GUARDED_BY(lock_) = false;
67   std::vector<Module*> stopped_modules_ RTC_GUARDED_BY(lock_);
68   std::map<Timestamp, std::list<Module*>> delayed_modules_
69       RTC_GUARDED_BY(lock_);
70 };
71 }  // namespace webrtc
72 
73 #endif  // TEST_TIME_CONTROLLER_SIMULATED_PROCESS_THREAD_H_
74