blob: 0b44340a2f03a55c9f841df7fb7d429974387bf0 [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
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stdint.h>
henrike@webrtc.org79cf3ac2014-01-13 15:21:30 +000015#include <list>
kwiberg22feaa32016-03-17 09:17:43 -070016#include <memory>
tommi@webrtc.org03054482015-03-05 13:13:42 +000017#include <queue>
henrike@webrtc.org79cf3ac2014-01-13 15:21:30 +000018
Danil Chapovalov959e9b62019-01-14 14:29:18 +010019#include "api/task_queue/queued_task.h"
Yves Gerey988cc082018-10-23 12:03:01 +020020#include "modules/include/module.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "modules/utility/include/process_thread.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/critical_section.h"
Niels Möller2c16cc62018-10-29 09:47:51 +010023#include "rtc_base/event.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/location.h"
25#include "rtc_base/platform_thread.h"
26#include "rtc_base/thread_checker.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000027
28namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000029
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000030class ProcessThreadImpl : public ProcessThread {
31 public:
stefan847855b2015-09-11 09:52:15 -070032 explicit ProcessThreadImpl(const char* thread_name);
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000033 ~ProcessThreadImpl() override;
niklase@google.com470e71d2011-07-07 08:21:25 +000034
tommi@webrtc.org3985f012015-02-27 13:36:34 +000035 void Start() override;
36 void Stop() override;
niklase@google.com470e71d2011-07-07 08:21:25 +000037
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000038 void WakeUp(Module* module) override;
Danil Chapovalov959e9b62019-01-14 14:29:18 +010039 void PostTask(std::unique_ptr<QueuedTask> task) override;
niklase@google.com470e71d2011-07-07 08:21:25 +000040
tommidea489f2017-03-03 03:20:24 -080041 void RegisterModule(Module* module, const rtc::Location& from) override;
tommi@webrtc.org3985f012015-02-27 13:36:34 +000042 void DeRegisterModule(Module* module) override;
niklase@google.com470e71d2011-07-07 08:21:25 +000043
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000044 protected:
Niels Möller4731f002019-05-03 09:34:24 +020045 static void Run(void* obj);
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000046 bool Process();
47
48 private:
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000049 struct ModuleCallback {
tommidea489f2017-03-03 03:20:24 -080050 ModuleCallback() = delete;
51 ModuleCallback(ModuleCallback&& cb) = default;
52 ModuleCallback(const ModuleCallback& cb) = default;
53 ModuleCallback(Module* module, const rtc::Location& location)
54 : module(module), location(location) {}
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000055 bool operator==(const ModuleCallback& cb) const {
56 return cb.module == module;
57 }
tommi@webrtc.org103f3282015-02-08 00:48:10 +000058
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000059 Module* const module;
tommidea489f2017-03-03 03:20:24 -080060 int64_t next_callback = 0; // Absolute timestamp.
61 const rtc::Location location;
tommi@webrtc.org103f3282015-02-08 00:48:10 +000062
63 private:
64 ModuleCallback& operator=(ModuleCallback&);
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000065 };
66
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000067 typedef std::list<ModuleCallback> ModuleList;
tommi@webrtc.org103f3282015-02-08 00:48:10 +000068
69 // Warning: For some reason, if |lock_| comes immediately before |modules_|
70 // with the current class layout, we will start to have mysterious crashes
71 // on Mac 10.9 debug. I (Tommi) suspect we're hitting some obscure alignemnt
72 // issues, but I haven't figured out what they are, if there are alignment
73 // requirements for mutexes on Mac or if there's something else to it.
74 // So be careful with changing the layout.
tommi@webrtc.org03054482015-03-05 13:13:42 +000075 rtc::CriticalSection lock_; // Used to guard modules_, tasks_ and stop_.
tommi@webrtc.org103f3282015-02-08 00:48:10 +000076
77 rtc::ThreadChecker thread_checker_;
Niels Möller2c16cc62018-10-29 09:47:51 +010078 rtc::Event wake_up_;
kwiberg22feaa32016-03-17 09:17:43 -070079 // TODO(pbos): Remove unique_ptr and stop recreating the thread.
80 std::unique_ptr<rtc::PlatformThread> thread_;
tommi@webrtc.org103f3282015-02-08 00:48:10 +000081
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000082 ModuleList modules_;
Danil Chapovalov959e9b62019-01-14 14:29:18 +010083 std::queue<QueuedTask*> queue_;
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000084 bool stop_;
stefan2b180842015-09-14 07:53:38 -070085 const char* thread_name_;
niklase@google.com470e71d2011-07-07 08:21:25 +000086};
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000087
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +000088} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +000089
Yves Gerey665174f2018-06-19 15:03:05 +020090#endif // MODULES_UTILITY_SOURCE_PROCESS_THREAD_IMPL_H_