niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_UTILITY_SOURCE_PROCESS_THREAD_IMPL_H_ |
| 12 | #define MODULES_UTILITY_SOURCE_PROCESS_THREAD_IMPL_H_ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 15 | |
henrike@webrtc.org | 79cf3ac | 2014-01-13 15:21:30 +0000 | [diff] [blame] | 16 | #include <list> |
kwiberg | 22feaa3 | 2016-03-17 09:17:43 -0700 | [diff] [blame] | 17 | #include <memory> |
tommi@webrtc.org | 0305448 | 2015-03-05 13:13:42 +0000 | [diff] [blame] | 18 | #include <queue> |
henrike@webrtc.org | 79cf3ac | 2014-01-13 15:21:30 +0000 | [diff] [blame] | 19 | |
Artem Titov | d15a575 | 2021-02-10 14:31:24 +0100 | [diff] [blame] | 20 | #include "api/sequence_checker.h" |
Danil Chapovalov | 959e9b6 | 2019-01-14 14:29:18 +0100 | [diff] [blame] | 21 | #include "api/task_queue/queued_task.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 22 | #include "modules/include/module.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "modules/utility/include/process_thread.h" |
Niels Möller | 2c16cc6 | 2018-10-29 09:47:51 +0100 | [diff] [blame] | 24 | #include "rtc_base/event.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "rtc_base/location.h" |
| 26 | #include "rtc_base/platform_thread.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 27 | |
| 28 | namespace webrtc { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 29 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 30 | class ProcessThreadImpl : public ProcessThread { |
| 31 | public: |
stefan | 847855b | 2015-09-11 09:52:15 -0700 | [diff] [blame] | 32 | explicit ProcessThreadImpl(const char* thread_name); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 33 | ~ProcessThreadImpl() override; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 34 | |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 35 | void Start() override; |
| 36 | void Stop() override; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 37 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 38 | void WakeUp(Module* module) override; |
Danil Chapovalov | 959e9b6 | 2019-01-14 14:29:18 +0100 | [diff] [blame] | 39 | void PostTask(std::unique_ptr<QueuedTask> task) override; |
Danil Chapovalov | 14273de | 2020-02-27 13:37:43 +0100 | [diff] [blame] | 40 | void PostDelayedTask(std::unique_ptr<QueuedTask> task, |
| 41 | uint32_t milliseconds) override; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 42 | |
tommi | dea489f | 2017-03-03 03:20:24 -0800 | [diff] [blame] | 43 | void RegisterModule(Module* module, const rtc::Location& from) override; |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 44 | void DeRegisterModule(Module* module) override; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 45 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 46 | protected: |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 47 | bool Process(); |
| 48 | |
| 49 | private: |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 50 | struct ModuleCallback { |
tommi | dea489f | 2017-03-03 03:20:24 -0800 | [diff] [blame] | 51 | ModuleCallback() = delete; |
| 52 | ModuleCallback(ModuleCallback&& cb) = default; |
| 53 | ModuleCallback(const ModuleCallback& cb) = default; |
| 54 | ModuleCallback(Module* module, const rtc::Location& location) |
| 55 | : module(module), location(location) {} |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 56 | bool operator==(const ModuleCallback& cb) const { |
| 57 | return cb.module == module; |
| 58 | } |
tommi@webrtc.org | 103f328 | 2015-02-08 00:48:10 +0000 | [diff] [blame] | 59 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 60 | Module* const module; |
tommi | dea489f | 2017-03-03 03:20:24 -0800 | [diff] [blame] | 61 | int64_t next_callback = 0; // Absolute timestamp. |
| 62 | const rtc::Location location; |
tommi@webrtc.org | 103f328 | 2015-02-08 00:48:10 +0000 | [diff] [blame] | 63 | |
| 64 | private: |
| 65 | ModuleCallback& operator=(ModuleCallback&); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 66 | }; |
Danil Chapovalov | 14273de | 2020-02-27 13:37:43 +0100 | [diff] [blame] | 67 | struct DelayedTask { |
| 68 | DelayedTask(int64_t run_at_ms, std::unique_ptr<QueuedTask> task) |
| 69 | : run_at_ms(run_at_ms), task(task.release()) {} |
| 70 | friend bool operator<(const DelayedTask& lhs, const DelayedTask& rhs) { |
| 71 | // Earliest DelayedTask should be at the top of the priority queue. |
| 72 | return lhs.run_at_ms > rhs.run_at_ms; |
| 73 | } |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 74 | |
Danil Chapovalov | 14273de | 2020-02-27 13:37:43 +0100 | [diff] [blame] | 75 | int64_t run_at_ms; |
| 76 | // DelayedTask owns the |task|, but some delayed tasks must be removed from |
| 77 | // the std::priority_queue, but mustn't be deleted. std::priority_queue does |
| 78 | // not give non-const access to the values, so storing unique_ptr would |
| 79 | // delete the task as soon as it is remove from the priority queue. |
| 80 | // Thus lifetime of the |task| is managed manually. |
| 81 | QueuedTask* task; |
| 82 | }; |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 83 | typedef std::list<ModuleCallback> ModuleList; |
tommi@webrtc.org | 103f328 | 2015-02-08 00:48:10 +0000 | [diff] [blame] | 84 | |
Danil Chapovalov | 14273de | 2020-02-27 13:37:43 +0100 | [diff] [blame] | 85 | void Delete() override; |
Niels Möller | 2bfddf7 | 2021-02-22 10:36:29 +0100 | [diff] [blame] | 86 | // The part of Stop processing that doesn't need any locking. |
| 87 | void StopNoLocks(); |
Niels Möller | 4785402 | 2021-03-01 11:31:33 +0100 | [diff] [blame] | 88 | void WakeUpNoLocks(Module* module); |
| 89 | void WakeUpInternal(Module* module) RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
Danil Chapovalov | 14273de | 2020-02-27 13:37:43 +0100 | [diff] [blame] | 90 | |
Niels Möller | 2bfddf7 | 2021-02-22 10:36:29 +0100 | [diff] [blame] | 91 | // Members protected by this mutex are accessed on the constructor thread and |
| 92 | // on the spawned process thread, and locking is needed only while the process |
| 93 | // thread is running. |
Niels Möller | 4785402 | 2021-03-01 11:31:33 +0100 | [diff] [blame] | 94 | Mutex mutex_; |
tommi@webrtc.org | 103f328 | 2015-02-08 00:48:10 +0000 | [diff] [blame] | 95 | |
Artem Titov | c8421c4 | 2021-02-02 10:57:19 +0100 | [diff] [blame] | 96 | SequenceChecker thread_checker_; |
Niels Möller | 2c16cc6 | 2018-10-29 09:47:51 +0100 | [diff] [blame] | 97 | rtc::Event wake_up_; |
Markus Handell | ad5037b | 2021-05-07 15:02:36 +0200 | [diff] [blame] | 98 | rtc::PlatformThread thread_; |
tommi@webrtc.org | 103f328 | 2015-02-08 00:48:10 +0000 | [diff] [blame] | 99 | |
Niels Möller | 4785402 | 2021-03-01 11:31:33 +0100 | [diff] [blame] | 100 | ModuleList modules_ RTC_GUARDED_BY(mutex_); |
| 101 | // Set to true when calling Process, to allow reentrant calls to WakeUp. |
| 102 | bool holds_mutex_ RTC_GUARDED_BY(this) = false; |
Danil Chapovalov | 959e9b6 | 2019-01-14 14:29:18 +0100 | [diff] [blame] | 103 | std::queue<QueuedTask*> queue_; |
Niels Möller | 4785402 | 2021-03-01 11:31:33 +0100 | [diff] [blame] | 104 | std::priority_queue<DelayedTask> delayed_tasks_ RTC_GUARDED_BY(mutex_); |
Niels Möller | 2bfddf7 | 2021-02-22 10:36:29 +0100 | [diff] [blame] | 105 | // The `stop_` flag is modified only by the construction thread, protected by |
| 106 | // `thread_checker_`. It is read also by the spawned `thread_`. The latter |
Niels Möller | 4785402 | 2021-03-01 11:31:33 +0100 | [diff] [blame] | 107 | // thread must take `mutex_` before access, and for thread safety, the |
| 108 | // constructor thread needs to take `mutex_` when it modifies `stop_` and |
Niels Möller | 2bfddf7 | 2021-02-22 10:36:29 +0100 | [diff] [blame] | 109 | // `thread_` is running. Annotations like RTC_GUARDED_BY doesn't support this |
| 110 | // usage pattern. |
Niels Möller | 4785402 | 2021-03-01 11:31:33 +0100 | [diff] [blame] | 111 | bool stop_ RTC_GUARDED_BY(mutex_); |
stefan | 2b18084 | 2015-09-14 07:53:38 -0700 | [diff] [blame] | 112 | const char* thread_name_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 113 | }; |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 114 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 115 | } // namespace webrtc |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 116 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 117 | #endif // MODULES_UTILITY_SOURCE_PROCESS_THREAD_IMPL_H_ |