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