simulated_process_thread.h 2.5 KB

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