process_thread_impl.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) 2011 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 MODULES_UTILITY_SOURCE_PROCESS_THREAD_IMPL_H_
  11. #define MODULES_UTILITY_SOURCE_PROCESS_THREAD_IMPL_H_
  12. #include <stdint.h>
  13. #include <list>
  14. #include <memory>
  15. #include <queue>
  16. #include "api/task_queue/queued_task.h"
  17. #include "modules/include/module.h"
  18. #include "modules/utility/include/process_thread.h"
  19. #include "rtc_base/deprecated/recursive_critical_section.h"
  20. #include "rtc_base/event.h"
  21. #include "rtc_base/location.h"
  22. #include "rtc_base/platform_thread.h"
  23. #include "rtc_base/thread_checker.h"
  24. namespace webrtc {
  25. class ProcessThreadImpl : public ProcessThread {
  26. public:
  27. explicit ProcessThreadImpl(const char* thread_name);
  28. ~ProcessThreadImpl() override;
  29. void Start() override;
  30. void Stop() override;
  31. void WakeUp(Module* module) override;
  32. void PostTask(std::unique_ptr<QueuedTask> task) override;
  33. void PostDelayedTask(std::unique_ptr<QueuedTask> task,
  34. uint32_t milliseconds) override;
  35. void RegisterModule(Module* module, const rtc::Location& from) override;
  36. void DeRegisterModule(Module* module) override;
  37. protected:
  38. static void Run(void* obj);
  39. bool Process();
  40. private:
  41. struct ModuleCallback {
  42. ModuleCallback() = delete;
  43. ModuleCallback(ModuleCallback&& cb) = default;
  44. ModuleCallback(const ModuleCallback& cb) = default;
  45. ModuleCallback(Module* module, const rtc::Location& location)
  46. : module(module), location(location) {}
  47. bool operator==(const ModuleCallback& cb) const {
  48. return cb.module == module;
  49. }
  50. Module* const module;
  51. int64_t next_callback = 0; // Absolute timestamp.
  52. const rtc::Location location;
  53. private:
  54. ModuleCallback& operator=(ModuleCallback&);
  55. };
  56. struct DelayedTask {
  57. DelayedTask(int64_t run_at_ms, std::unique_ptr<QueuedTask> task)
  58. : run_at_ms(run_at_ms), task(task.release()) {}
  59. friend bool operator<(const DelayedTask& lhs, const DelayedTask& rhs) {
  60. // Earliest DelayedTask should be at the top of the priority queue.
  61. return lhs.run_at_ms > rhs.run_at_ms;
  62. }
  63. int64_t run_at_ms;
  64. // DelayedTask owns the |task|, but some delayed tasks must be removed from
  65. // the std::priority_queue, but mustn't be deleted. std::priority_queue does
  66. // not give non-const access to the values, so storing unique_ptr would
  67. // delete the task as soon as it is remove from the priority queue.
  68. // Thus lifetime of the |task| is managed manually.
  69. QueuedTask* task;
  70. };
  71. typedef std::list<ModuleCallback> ModuleList;
  72. void Delete() override;
  73. // Warning: For some reason, if |lock_| comes immediately before |modules_|
  74. // with the current class layout, we will start to have mysterious crashes
  75. // on Mac 10.9 debug. I (Tommi) suspect we're hitting some obscure alignemnt
  76. // issues, but I haven't figured out what they are, if there are alignment
  77. // requirements for mutexes on Mac or if there's something else to it.
  78. // So be careful with changing the layout.
  79. rtc::RecursiveCriticalSection
  80. lock_; // Used to guard modules_, tasks_ and stop_.
  81. rtc::ThreadChecker thread_checker_;
  82. rtc::Event wake_up_;
  83. // TODO(pbos): Remove unique_ptr and stop recreating the thread.
  84. std::unique_ptr<rtc::PlatformThread> thread_;
  85. ModuleList modules_;
  86. std::queue<QueuedTask*> queue_;
  87. std::priority_queue<DelayedTask> delayed_tasks_ RTC_GUARDED_BY(lock_);
  88. bool stop_;
  89. const char* thread_name_;
  90. };
  91. } // namespace webrtc
  92. #endif // MODULES_UTILITY_SOURCE_PROCESS_THREAD_IMPL_H_