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