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> |
henrike@webrtc.org | 79cf3ac | 2014-01-13 15:21:30 +0000 | [diff] [blame] | 15 | #include <list> |
kwiberg | 22feaa3 | 2016-03-17 09:17:43 -0700 | [diff] [blame] | 16 | #include <memory> |
tommi@webrtc.org | 0305448 | 2015-03-05 13:13:42 +0000 | [diff] [blame] | 17 | #include <queue> |
henrike@webrtc.org | 79cf3ac | 2014-01-13 15:21:30 +0000 | [diff] [blame] | 18 | |
Danil Chapovalov | 959e9b6 | 2019-01-14 14:29:18 +0100 | [diff] [blame^] | 19 | #include "api/task_queue/queued_task.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 20 | #include "modules/include/module.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "modules/utility/include/process_thread.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 22 | #include "rtc_base/critical_section.h" |
Niels Möller | 2c16cc6 | 2018-10-29 09:47:51 +0100 | [diff] [blame] | 23 | #include "rtc_base/event.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 24 | #include "rtc_base/location.h" |
| 25 | #include "rtc_base/platform_thread.h" |
| 26 | #include "rtc_base/thread_checker.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; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 40 | |
tommi | dea489f | 2017-03-03 03:20:24 -0800 | [diff] [blame] | 41 | void RegisterModule(Module* module, const rtc::Location& from) override; |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 42 | void DeRegisterModule(Module* module) override; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 43 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 44 | protected: |
| 45 | static bool Run(void* obj); |
| 46 | bool Process(); |
| 47 | |
| 48 | private: |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 49 | struct ModuleCallback { |
tommi | dea489f | 2017-03-03 03:20:24 -0800 | [diff] [blame] | 50 | ModuleCallback() = delete; |
| 51 | ModuleCallback(ModuleCallback&& cb) = default; |
| 52 | ModuleCallback(const ModuleCallback& cb) = default; |
| 53 | ModuleCallback(Module* module, const rtc::Location& location) |
| 54 | : module(module), location(location) {} |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 55 | bool operator==(const ModuleCallback& cb) const { |
| 56 | return cb.module == module; |
| 57 | } |
tommi@webrtc.org | 103f328 | 2015-02-08 00:48:10 +0000 | [diff] [blame] | 58 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 59 | Module* const module; |
tommi | dea489f | 2017-03-03 03:20:24 -0800 | [diff] [blame] | 60 | int64_t next_callback = 0; // Absolute timestamp. |
| 61 | const rtc::Location location; |
tommi@webrtc.org | 103f328 | 2015-02-08 00:48:10 +0000 | [diff] [blame] | 62 | |
| 63 | private: |
| 64 | ModuleCallback& operator=(ModuleCallback&); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 65 | }; |
| 66 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 67 | typedef std::list<ModuleCallback> ModuleList; |
tommi@webrtc.org | 103f328 | 2015-02-08 00:48:10 +0000 | [diff] [blame] | 68 | |
| 69 | // Warning: For some reason, if |lock_| comes immediately before |modules_| |
| 70 | // with the current class layout, we will start to have mysterious crashes |
| 71 | // on Mac 10.9 debug. I (Tommi) suspect we're hitting some obscure alignemnt |
| 72 | // issues, but I haven't figured out what they are, if there are alignment |
| 73 | // requirements for mutexes on Mac or if there's something else to it. |
| 74 | // So be careful with changing the layout. |
tommi@webrtc.org | 0305448 | 2015-03-05 13:13:42 +0000 | [diff] [blame] | 75 | rtc::CriticalSection lock_; // Used to guard modules_, tasks_ and stop_. |
tommi@webrtc.org | 103f328 | 2015-02-08 00:48:10 +0000 | [diff] [blame] | 76 | |
| 77 | rtc::ThreadChecker thread_checker_; |
Niels Möller | 2c16cc6 | 2018-10-29 09:47:51 +0100 | [diff] [blame] | 78 | rtc::Event wake_up_; |
kwiberg | 22feaa3 | 2016-03-17 09:17:43 -0700 | [diff] [blame] | 79 | // TODO(pbos): Remove unique_ptr and stop recreating the thread. |
| 80 | std::unique_ptr<rtc::PlatformThread> thread_; |
tommi@webrtc.org | 103f328 | 2015-02-08 00:48:10 +0000 | [diff] [blame] | 81 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 82 | ModuleList modules_; |
Danil Chapovalov | 959e9b6 | 2019-01-14 14:29:18 +0100 | [diff] [blame^] | 83 | std::queue<QueuedTask*> queue_; |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 84 | bool stop_; |
stefan | 2b18084 | 2015-09-14 07:53:38 -0700 | [diff] [blame] | 85 | const char* thread_name_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 86 | }; |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 87 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 88 | } // namespace webrtc |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 89 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 90 | #endif // MODULES_UTILITY_SOURCE_PROCESS_THREAD_IMPL_H_ |