blob: 93d093d14bbe5f31b4b001b69fb0bdb5377c1ac1 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_UTILITY_SOURCE_PROCESS_THREAD_IMPL_H_
12#define MODULES_UTILITY_SOURCE_PROCESS_THREAD_IMPL_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
henrike@webrtc.org79cf3ac2014-01-13 15:21:30 +000014#include <list>
kwiberg22feaa32016-03-17 09:17:43 -070015#include <memory>
tommi@webrtc.org03054482015-03-05 13:13:42 +000016#include <queue>
henrike@webrtc.org79cf3ac2014-01-13 15:21:30 +000017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#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.com470e71d2011-07-07 08:21:25 +000025
26namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000027
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000028class ProcessThreadImpl : public ProcessThread {
29 public:
stefan847855b2015-09-11 09:52:15 -070030 explicit ProcessThreadImpl(const char* thread_name);
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000031 ~ProcessThreadImpl() override;
niklase@google.com470e71d2011-07-07 08:21:25 +000032
tommi@webrtc.org3985f012015-02-27 13:36:34 +000033 void Start() override;
34 void Stop() override;
niklase@google.com470e71d2011-07-07 08:21:25 +000035
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000036 void WakeUp(Module* module) override;
tommi435f98b2016-05-28 14:57:15 -070037 void PostTask(std::unique_ptr<rtc::QueuedTask> task) override;
niklase@google.com470e71d2011-07-07 08:21:25 +000038
tommidea489f2017-03-03 03:20:24 -080039 void RegisterModule(Module* module, const rtc::Location& from) override;
tommi@webrtc.org3985f012015-02-27 13:36:34 +000040 void DeRegisterModule(Module* module) override;
niklase@google.com470e71d2011-07-07 08:21:25 +000041
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000042 protected:
43 static bool Run(void* obj);
44 bool Process();
45
46 private:
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000047 struct ModuleCallback {
tommidea489f2017-03-03 03:20:24 -080048 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.org0c3e12b2015-02-06 09:44:12 +000053 bool operator==(const ModuleCallback& cb) const {
54 return cb.module == module;
55 }
tommi@webrtc.org103f3282015-02-08 00:48:10 +000056
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000057 Module* const module;
tommidea489f2017-03-03 03:20:24 -080058 int64_t next_callback = 0; // Absolute timestamp.
59 const rtc::Location location;
tommi@webrtc.org103f3282015-02-08 00:48:10 +000060
61 private:
62 ModuleCallback& operator=(ModuleCallback&);
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000063 };
64
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000065 typedef std::list<ModuleCallback> ModuleList;
tommi@webrtc.org103f3282015-02-08 00:48:10 +000066
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.org03054482015-03-05 13:13:42 +000073 rtc::CriticalSection lock_; // Used to guard modules_, tasks_ and stop_.
tommi@webrtc.org103f3282015-02-08 00:48:10 +000074
75 rtc::ThreadChecker thread_checker_;
kwiberg22feaa32016-03-17 09:17:43 -070076 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.org103f3282015-02-08 00:48:10 +000079
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000080 ModuleList modules_;
tommi435f98b2016-05-28 14:57:15 -070081 std::queue<rtc::QueuedTask*> queue_;
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000082 bool stop_;
stefan2b180842015-09-14 07:53:38 -070083 const char* thread_name_;
niklase@google.com470e71d2011-07-07 08:21:25 +000084};
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000085
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +000086} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +000087
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020088#endif // MODULES_UTILITY_SOURCE_PROCESS_THREAD_IMPL_H_