blob: 7e01ae5c67df7d16750d9e62983a1a12ecf1e58a [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
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.org79cf3ac2014-01-13 15:21:30 +000014#include <list>
15
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000016#include "webrtc/base/criticalsection.h"
17#include "webrtc/base/thread_checker.h"
pbos@webrtc.org8b062002013-07-12 08:28:10 +000018#include "webrtc/modules/utility/interface/process_thread.h"
pbos@webrtc.org8b062002013-07-12 08:28:10 +000019#include "webrtc/system_wrappers/interface/event_wrapper.h"
pbos@webrtc.org8b062002013-07-12 08:28:10 +000020#include "webrtc/system_wrappers/interface/thread_wrapper.h"
21#include "webrtc/typedefs.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000022
23namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000024
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000025class ProcessThreadImpl : public ProcessThread {
26 public:
27 ProcessThreadImpl();
28 ~ProcessThreadImpl() override;
niklase@google.com470e71d2011-07-07 08:21:25 +000029
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000030 int32_t Start() override;
31 int32_t Stop() override;
niklase@google.com470e71d2011-07-07 08:21:25 +000032
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000033 void WakeUp(Module* module) override;
niklase@google.com470e71d2011-07-07 08:21:25 +000034
tommi@webrtc.org103f3282015-02-08 00:48:10 +000035 int32_t RegisterModule(Module* module) override;
36 int32_t DeRegisterModule(const Module* module) override;
niklase@google.com470e71d2011-07-07 08:21:25 +000037
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000038 protected:
39 static bool Run(void* obj);
40 bool Process();
41
42 private:
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000043 struct ModuleCallback {
tommi@webrtc.org103f3282015-02-08 00:48:10 +000044 ModuleCallback() : module(nullptr), next_callback(0) {}
45 ModuleCallback(const ModuleCallback& cb)
46 : module(cb.module), next_callback(cb.next_callback) {}
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000047 ModuleCallback(Module* module) : module(module), next_callback(0) {}
48 bool operator==(const ModuleCallback& cb) const {
49 return cb.module == module;
50 }
tommi@webrtc.org103f3282015-02-08 00:48:10 +000051
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000052 Module* const module;
53 int64_t next_callback; // Absolute timestamp.
tommi@webrtc.org103f3282015-02-08 00:48:10 +000054
55 private:
56 ModuleCallback& operator=(ModuleCallback&);
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000057 };
58
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000059 typedef std::list<ModuleCallback> ModuleList;
tommi@webrtc.org103f3282015-02-08 00:48:10 +000060
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.org0c3e12b2015-02-06 09:44:12 +000073 ModuleList modules_;
74 bool stop_;
niklase@google.com470e71d2011-07-07 08:21:25 +000075};
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000076
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +000077} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +000078
79#endif // WEBRTC_MODULES_UTILITY_SOURCE_PROCESS_THREAD_IMPL_H_