blob: 53b5d591a383e7b4f11848ef3057b13687540824 [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>
tommi@webrtc.org03054482015-03-05 13:13:42 +000015#include <queue>
henrike@webrtc.org79cf3ac2014-01-13 15:21:30 +000016
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000017#include "webrtc/base/criticalsection.h"
18#include "webrtc/base/thread_checker.h"
pbos@webrtc.org8b062002013-07-12 08:28:10 +000019#include "webrtc/modules/utility/interface/process_thread.h"
pbos@webrtc.org8b062002013-07-12 08:28:10 +000020#include "webrtc/system_wrappers/interface/event_wrapper.h"
pbos@webrtc.org8b062002013-07-12 08:28:10 +000021#include "webrtc/system_wrappers/interface/thread_wrapper.h"
22#include "webrtc/typedefs.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000023
24namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000025
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000026class ProcessThreadImpl : public ProcessThread {
27 public:
stefan847855b2015-09-11 09:52:15 -070028 explicit ProcessThreadImpl(const char* thread_name);
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000029 ~ProcessThreadImpl() override;
niklase@google.com470e71d2011-07-07 08:21:25 +000030
tommi@webrtc.org3985f012015-02-27 13:36:34 +000031 void Start() override;
32 void Stop() override;
niklase@google.com470e71d2011-07-07 08:21:25 +000033
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000034 void WakeUp(Module* module) override;
tommi@webrtc.org03054482015-03-05 13:13:42 +000035 void PostTask(rtc::scoped_ptr<ProcessTask> task) override;
niklase@google.com470e71d2011-07-07 08:21:25 +000036
tommi@webrtc.org3985f012015-02-27 13:36:34 +000037 void RegisterModule(Module* module) override;
38 void DeRegisterModule(Module* module) override;
niklase@google.com470e71d2011-07-07 08:21:25 +000039
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000040 protected:
41 static bool Run(void* obj);
42 bool Process();
43
44 private:
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000045 struct ModuleCallback {
tommi@webrtc.org103f3282015-02-08 00:48:10 +000046 ModuleCallback() : module(nullptr), next_callback(0) {}
47 ModuleCallback(const ModuleCallback& cb)
48 : module(cb.module), next_callback(cb.next_callback) {}
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000049 ModuleCallback(Module* module) : module(module), next_callback(0) {}
50 bool operator==(const ModuleCallback& cb) const {
51 return cb.module == module;
52 }
tommi@webrtc.org103f3282015-02-08 00:48:10 +000053
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000054 Module* const module;
55 int64_t next_callback; // Absolute timestamp.
tommi@webrtc.org103f3282015-02-08 00:48:10 +000056
57 private:
58 ModuleCallback& operator=(ModuleCallback&);
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000059 };
60
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000061 typedef std::list<ModuleCallback> ModuleList;
tommi@webrtc.org103f3282015-02-08 00:48:10 +000062
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.org03054482015-03-05 13:13:42 +000069 rtc::CriticalSection lock_; // Used to guard modules_, tasks_ and stop_.
tommi@webrtc.org103f3282015-02-08 00:48:10 +000070
71 rtc::ThreadChecker thread_checker_;
72 const rtc::scoped_ptr<EventWrapper> wake_up_;
73 rtc::scoped_ptr<ThreadWrapper> thread_;
74
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000075 ModuleList modules_;
tommi@webrtc.org03054482015-03-05 13:13:42 +000076 // TODO(tommi): Support delayed tasks.
77 std::queue<ProcessTask*> queue_;
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000078 bool stop_;
stefan847855b2015-09-11 09:52:15 -070079 std::string thread_name_;
niklase@google.com470e71d2011-07-07 08:21:25 +000080};
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000081
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +000082} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +000083
84#endif // WEBRTC_MODULES_UTILITY_SOURCE_PROCESS_THREAD_IMPL_H_