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 | |
| 11 | #ifndef WEBRTC_MODULES_UTILITY_SOURCE_PROCESS_THREAD_IMPL_H_ |
| 12 | #define WEBRTC_MODULES_UTILITY_SOURCE_PROCESS_THREAD_IMPL_H_ |
| 13 | |
henrike@webrtc.org | 79cf3ac | 2014-01-13 15:21:30 +0000 | [diff] [blame] | 14 | #include <list> |
| 15 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 16 | #include "webrtc/base/criticalsection.h" |
| 17 | #include "webrtc/base/thread_checker.h" |
pbos@webrtc.org | 8b06200 | 2013-07-12 08:28:10 +0000 | [diff] [blame] | 18 | #include "webrtc/modules/utility/interface/process_thread.h" |
pbos@webrtc.org | 8b06200 | 2013-07-12 08:28:10 +0000 | [diff] [blame] | 19 | #include "webrtc/system_wrappers/interface/event_wrapper.h" |
pbos@webrtc.org | 8b06200 | 2013-07-12 08:28:10 +0000 | [diff] [blame] | 20 | #include "webrtc/system_wrappers/interface/thread_wrapper.h" |
| 21 | #include "webrtc/typedefs.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 24 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 25 | class ProcessThreadImpl : public ProcessThread { |
| 26 | public: |
| 27 | ProcessThreadImpl(); |
| 28 | ~ProcessThreadImpl() override; |
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 | int32_t Start() override; |
| 31 | int32_t Stop() override; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 32 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 33 | void WakeUp(Module* module) override; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 34 | |
tommi@webrtc.org | 103f328 | 2015-02-08 00:48:10 +0000 | [diff] [blame^] | 35 | int32_t RegisterModule(Module* module) override; |
| 36 | int32_t DeRegisterModule(const Module* module) 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 | protected: |
| 39 | static bool Run(void* obj); |
| 40 | bool Process(); |
| 41 | |
| 42 | private: |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 43 | struct ModuleCallback { |
tommi@webrtc.org | 103f328 | 2015-02-08 00:48:10 +0000 | [diff] [blame^] | 44 | ModuleCallback() : module(nullptr), next_callback(0) {} |
| 45 | ModuleCallback(const ModuleCallback& cb) |
| 46 | : module(cb.module), next_callback(cb.next_callback) {} |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 47 | ModuleCallback(Module* module) : module(module), next_callback(0) {} |
| 48 | bool operator==(const ModuleCallback& cb) const { |
| 49 | return cb.module == module; |
| 50 | } |
tommi@webrtc.org | 103f328 | 2015-02-08 00:48:10 +0000 | [diff] [blame^] | 51 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 52 | Module* const module; |
| 53 | int64_t next_callback; // Absolute timestamp. |
tommi@webrtc.org | 103f328 | 2015-02-08 00:48:10 +0000 | [diff] [blame^] | 54 | |
| 55 | private: |
| 56 | ModuleCallback& operator=(ModuleCallback&); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 59 | typedef std::list<ModuleCallback> ModuleList; |
tommi@webrtc.org | 103f328 | 2015-02-08 00:48:10 +0000 | [diff] [blame^] | 60 | |
| 61 | // Warning: For some reason, if |lock_| comes immediately before |modules_| |
| 62 | // with the current class layout, we will start to have mysterious crashes |
| 63 | // on Mac 10.9 debug. I (Tommi) suspect we're hitting some obscure alignemnt |
| 64 | // issues, but I haven't figured out what they are, if there are alignment |
| 65 | // requirements for mutexes on Mac or if there's something else to it. |
| 66 | // So be careful with changing the layout. |
| 67 | rtc::CriticalSection lock_; // Used to guard modules_ and stop_. |
| 68 | |
| 69 | rtc::ThreadChecker thread_checker_; |
| 70 | const rtc::scoped_ptr<EventWrapper> wake_up_; |
| 71 | rtc::scoped_ptr<ThreadWrapper> thread_; |
| 72 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 73 | ModuleList modules_; |
| 74 | bool stop_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 75 | }; |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 76 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 77 | } // namespace webrtc |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 78 | |
| 79 | #endif // WEBRTC_MODULES_UTILITY_SOURCE_PROCESS_THREAD_IMPL_H_ |