1 /*
2 * Copyright 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "common/message_loop_thread.h"
18
19 #include <sys/syscall.h>
20 #include <unistd.h>
21 #include <thread>
22
23 #include <base/strings/stringprintf.h>
24
25 #include "gd/common/init_flags.h"
26 #include "osi/include/log.h"
27
28 namespace bluetooth {
29
30 namespace common {
31
32 static constexpr int kRealTimeFifoSchedulingPriority = 1;
33
MessageLoopThread(const std::string & thread_name)34 MessageLoopThread::MessageLoopThread(const std::string& thread_name)
35 : MessageLoopThread(thread_name, false) {}
36
MessageLoopThread(const std::string & thread_name,bool is_main)37 MessageLoopThread::MessageLoopThread(const std::string& thread_name,
38 bool is_main)
39 : thread_name_(thread_name),
40 message_loop_(nullptr),
41 run_loop_(nullptr),
42 thread_(nullptr),
43 thread_id_(-1),
44 linux_tid_(-1),
45 weak_ptr_factory_(this),
46 shutting_down_(false),
47 is_main_(is_main) {}
48
~MessageLoopThread()49 MessageLoopThread::~MessageLoopThread() { ShutDown(); }
50
StartUp()51 void MessageLoopThread::StartUp() {
52 std::promise<void> start_up_promise;
53 std::future<void> start_up_future = start_up_promise.get_future();
54 {
55 std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
56 if (thread_ != nullptr) {
57 LOG(WARNING) << __func__ << ": thread " << *this << " is already started";
58
59 return;
60 }
61 thread_ = new std::thread(&MessageLoopThread::RunThread, this,
62 std::move(start_up_promise));
63 }
64 start_up_future.wait();
65 }
66
DoInThread(const base::Location & from_here,base::OnceClosure task)67 bool MessageLoopThread::DoInThread(const base::Location& from_here,
68 base::OnceClosure task) {
69 return DoInThreadDelayed(from_here, std::move(task), base::TimeDelta());
70 }
71
DoInThreadDelayed(const base::Location & from_here,base::OnceClosure task,const base::TimeDelta & delay)72 bool MessageLoopThread::DoInThreadDelayed(const base::Location& from_here,
73 base::OnceClosure task,
74 const base::TimeDelta& delay) {
75 std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
76 if (message_loop_ == nullptr) {
77 LOG(ERROR) << __func__ << ": message loop is null for thread " << *this
78 << ", from " << from_here.ToString();
79 return false;
80 }
81 if (!message_loop_->task_runner()->PostDelayedTask(from_here, std::move(task),
82 delay)) {
83 LOG(ERROR) << __func__
84 << ": failed to post task to message loop for thread " << *this
85 << ", from " << from_here.ToString();
86 return false;
87 }
88 return true;
89 }
90
ShutDown()91 void MessageLoopThread::ShutDown() {
92 {
93 std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
94 if (thread_ == nullptr) {
95 LOG(INFO) << __func__ << ": thread " << *this << " is already stopped";
96 return;
97 }
98 if (message_loop_ == nullptr) {
99 LOG(INFO) << __func__ << ": message_loop_ is null. Already stopping";
100 return;
101 }
102 if (shutting_down_) {
103 LOG(INFO) << __func__ << ": waiting for thread to join";
104 return;
105 }
106 shutting_down_ = true;
107 CHECK_NE(thread_id_, base::PlatformThread::CurrentId())
108 << __func__ << " should not be called on the thread itself. "
109 << "Otherwise, deadlock may happen.";
110 if (!message_loop_->task_runner()->PostTask(
111 FROM_HERE, run_loop_->QuitWhenIdleClosure())) {
112 LOG(FATAL) << __func__
113 << ": failed to post task to message loop for thread "
114 << *this;
115 }
116 }
117 thread_->join();
118 {
119 std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
120 delete thread_;
121 thread_ = nullptr;
122 shutting_down_ = false;
123 }
124 }
125
GetThreadId() const126 base::PlatformThreadId MessageLoopThread::GetThreadId() const {
127 std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
128 return thread_id_;
129 }
130
GetName() const131 std::string MessageLoopThread::GetName() const { return thread_name_; }
132
ToString() const133 std::string MessageLoopThread::ToString() const {
134 std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
135 return base::StringPrintf("%s(%d)", thread_name_.c_str(), thread_id_);
136 }
137
IsRunning() const138 bool MessageLoopThread::IsRunning() const {
139 std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
140 return thread_id_ != -1;
141 }
142
143 // Non API method, should not be protected by API mutex
RunThread(MessageLoopThread * thread,std::promise<void> start_up_promise)144 void MessageLoopThread::RunThread(MessageLoopThread* thread,
145 std::promise<void> start_up_promise) {
146 thread->Run(std::move(start_up_promise));
147 }
148
message_loop() const149 btbase::AbstractMessageLoop* MessageLoopThread::message_loop() const {
150 ASSERT_LOG(!is_main_,
151 "you are not allowed to get the main thread's message loop");
152
153 std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
154 return message_loop_;
155 }
156
EnableRealTimeScheduling()157 bool MessageLoopThread::EnableRealTimeScheduling() {
158 std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
159
160 if (!IsRunning()) {
161 LOG(ERROR) << __func__ << ": thread " << *this << " is not running";
162 return false;
163 }
164
165 struct sched_param rt_params = {.sched_priority =
166 kRealTimeFifoSchedulingPriority};
167 int rc = sched_setscheduler(linux_tid_, SCHED_FIFO, &rt_params);
168 if (rc != 0) {
169 LOG(ERROR) << __func__ << ": unable to set SCHED_FIFO priority "
170 << kRealTimeFifoSchedulingPriority << " for linux_tid "
171 << std::to_string(linux_tid_) << ", thread " << *this
172 << ", error: " << strerror(errno);
173 return false;
174 }
175 return true;
176 }
177
GetWeakPtr()178 base::WeakPtr<MessageLoopThread> MessageLoopThread::GetWeakPtr() {
179 std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
180 return weak_ptr_factory_.GetWeakPtr();
181 }
182
Run(std::promise<void> start_up_promise)183 void MessageLoopThread::Run(std::promise<void> start_up_promise) {
184 {
185 std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
186 LOG(INFO) << __func__ << ": message loop starting for thread "
187 << thread_name_;
188 base::PlatformThread::SetName(thread_name_);
189 message_loop_ = new btbase::AbstractMessageLoop();
190 run_loop_ = new base::RunLoop();
191 thread_id_ = base::PlatformThread::CurrentId();
192 linux_tid_ = static_cast<pid_t>(syscall(SYS_gettid));
193 start_up_promise.set_value();
194 }
195
196 // Blocking until ShutDown() is called
197 run_loop_->Run();
198
199 {
200 std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
201 thread_id_ = -1;
202 linux_tid_ = -1;
203 delete message_loop_;
204 message_loop_ = nullptr;
205 delete run_loop_;
206 run_loop_ = nullptr;
207 LOG(INFO) << __func__ << ": message loop finished for thread "
208 << thread_name_;
209 }
210 }
211
212 } // namespace common
213
214 } // namespace bluetooth
215