simulated_task_queue.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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_TASK_QUEUE_H_
  11. #define TEST_TIME_CONTROLLER_SIMULATED_TASK_QUEUE_H_
  12. #include <deque>
  13. #include <map>
  14. #include <memory>
  15. #include <vector>
  16. #include "rtc_base/synchronization/mutex.h"
  17. #include "test/time_controller/simulated_time_controller.h"
  18. namespace webrtc {
  19. class SimulatedTaskQueue : public TaskQueueBase,
  20. public sim_time_impl::SimulatedSequenceRunner {
  21. public:
  22. SimulatedTaskQueue(sim_time_impl::SimulatedTimeControllerImpl* handler,
  23. absl::string_view name);
  24. ~SimulatedTaskQueue();
  25. void RunReady(Timestamp at_time) override;
  26. Timestamp GetNextRunTime() const override {
  27. MutexLock lock(&lock_);
  28. return next_run_time_;
  29. }
  30. TaskQueueBase* GetAsTaskQueue() override { return this; }
  31. // TaskQueueBase interface
  32. void Delete() override;
  33. void PostTask(std::unique_ptr<QueuedTask> task) override;
  34. void PostDelayedTask(std::unique_ptr<QueuedTask> task,
  35. uint32_t milliseconds) override;
  36. private:
  37. sim_time_impl::SimulatedTimeControllerImpl* const handler_;
  38. // Using char* to be debugger friendly.
  39. char* name_;
  40. mutable Mutex lock_;
  41. std::deque<std::unique_ptr<QueuedTask>> ready_tasks_ RTC_GUARDED_BY(lock_);
  42. std::map<Timestamp, std::vector<std::unique_ptr<QueuedTask>>> delayed_tasks_
  43. RTC_GUARDED_BY(lock_);
  44. Timestamp next_run_time_ RTC_GUARDED_BY(lock_) = Timestamp::PlusInfinity();
  45. };
  46. } // namespace webrtc
  47. #endif // TEST_TIME_CONTROLLER_SIMULATED_TASK_QUEUE_H_