1 /*
2 * Copyright 2018 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 "message_loop_thread.h"
18
19 #include <sys/syscall.h>
20 #include <unistd.h>
21 #include <thread>
22
23 #include <base/logging.h>
24 #include <base/strings/stringprintf.h>
25
26 #include "gd/common/init_flags.h"
27 #include "osi/include/log.h"
28
29 namespace bluetooth {
30
31 namespace common {
32
33 static constexpr int kRealTimeFifoSchedulingPriority = 1;
34
MessageLoopThread(const std::string & thread_name)35 MessageLoopThread::MessageLoopThread(const std::string& thread_name)
36 : MessageLoopThread(thread_name, false) {}
37
MessageLoopThread(const std::string & thread_name,bool is_main)38 MessageLoopThread::MessageLoopThread(const std::string& thread_name,
39 bool is_main)
40 : thread_name_(thread_name),
41 message_loop_(nullptr),
42 run_loop_(nullptr),
43 thread_(nullptr),
44 thread_id_(-1),
45 linux_tid_(-1),
46 weak_ptr_factory_(this),
47 shutting_down_(false),
48 is_main_(is_main),
49 rust_thread_(nullptr) {}
50
~MessageLoopThread()51 MessageLoopThread::~MessageLoopThread() { ShutDown(); }
52
StartUp()53 void MessageLoopThread::StartUp() {
54 if (is_main_ && init_flags::gd_rust_is_enabled()) {
55 rust_thread_ = new ::rust::Box<shim::rust::MessageLoopThread>(
56 shim::rust::main_message_loop_thread_create());
57 auto rust_id =
58 bluetooth::shim::rust::main_message_loop_thread_start(**rust_thread_);
59 thread_id_ = rust_id;
60 linux_tid_ = rust_id;
61 return;
62 }
63
64 std::promise<void> start_up_promise;
65 std::future<void> start_up_future = start_up_promise.get_future();
66 {
67 std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
68 if (thread_ != nullptr) {
69 LOG(WARNING) << __func__ << ": thread " << *this << " is already started";
70
71 return;
72 }
73 thread_ = new std::thread(&MessageLoopThread::RunThread, this,
74 std::move(start_up_promise));
75 }
76 start_up_future.wait();
77 }
78
DoInThread(const base::Location & from_here,base::OnceClosure task)79 bool MessageLoopThread::DoInThread(const base::Location& from_here,
80 base::OnceClosure task) {
81 return DoInThreadDelayed(from_here, std::move(task), base::TimeDelta());
82 }
83
DoInThreadDelayed(const base::Location & from_here,base::OnceClosure task,const base::TimeDelta & delay)84 bool MessageLoopThread::DoInThreadDelayed(const base::Location& from_here,
85 base::OnceClosure task,
86 const base::TimeDelta& delay) {
87 std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
88 if (is_main_ && init_flags::gd_rust_is_enabled()) {
89 if (rust_thread_ == nullptr) {
90 LOG(ERROR) << __func__ << ": rust thread is null for thread " << *this
91 << ", from " << from_here.ToString();
92 return false;
93 }
94
95 shim::rust::main_message_loop_thread_do_delayed(
96 **rust_thread_,
97 std::make_unique<shim::rust::OnceClosure>(std::move(task)),
98 delay.InMilliseconds());
99 return true;
100 }
101
102 if (message_loop_ == nullptr) {
103 LOG(ERROR) << __func__ << ": message loop is null for thread " << *this
104 << ", from " << from_here.ToString();
105 return false;
106 }
107 if (!message_loop_->task_runner()->PostDelayedTask(from_here, std::move(task),
108 delay)) {
109 LOG(ERROR) << __func__
110 << ": failed to post task to message loop for thread " << *this
111 << ", from " << from_here.ToString();
112 return false;
113 }
114 return true;
115 }
116
ShutDown()117 void MessageLoopThread::ShutDown() {
118 {
119 if (is_main_ && init_flags::gd_rust_is_enabled()) {
120 delete rust_thread_;
121 rust_thread_ = nullptr;
122 thread_id_ = -1;
123 linux_tid_ = -1;
124 return;
125 }
126
127 std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
128 if (thread_ == nullptr) {
129 LOG(INFO) << __func__ << ": thread " << *this << " is already stopped";
130 return;
131 }
132 if (message_loop_ == nullptr) {
133 LOG(INFO) << __func__ << ": message_loop_ is null. Already stopping";
134 return;
135 }
136 if (shutting_down_) {
137 LOG(INFO) << __func__ << ": waiting for thread to join";
138 return;
139 }
140 shutting_down_ = true;
141 CHECK_NE(thread_id_, base::PlatformThread::CurrentId())
142 << __func__ << " should not be called on the thread itself. "
143 << "Otherwise, deadlock may happen.";
144 if (!message_loop_->task_runner()->PostTask(
145 FROM_HERE, run_loop_->QuitWhenIdleClosure())) {
146 LOG(FATAL) << __func__
147 << ": failed to post task to message loop for thread "
148 << *this;
149 }
150 }
151 thread_->join();
152 {
153 std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
154 delete thread_;
155 thread_ = nullptr;
156 shutting_down_ = false;
157 }
158 }
159
GetThreadId() const160 base::PlatformThreadId MessageLoopThread::GetThreadId() const {
161 std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
162 return thread_id_;
163 }
164
GetName() const165 std::string MessageLoopThread::GetName() const {
166 return thread_name_;
167 }
168
ToString() const169 std::string MessageLoopThread::ToString() const {
170 std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
171 return base::StringPrintf("%s(%d)", thread_name_.c_str(), thread_id_);
172 }
173
IsRunning() const174 bool MessageLoopThread::IsRunning() const {
175 std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
176 return thread_id_ != -1;
177 }
178
179 // Non API method, should not be protected by API mutex
RunThread(MessageLoopThread * thread,std::promise<void> start_up_promise)180 void MessageLoopThread::RunThread(MessageLoopThread* thread,
181 std::promise<void> start_up_promise) {
182 thread->Run(std::move(start_up_promise));
183 }
184
message_loop() const185 btbase::AbstractMessageLoop* MessageLoopThread::message_loop() const {
186 ASSERT_LOG(!is_main_,
187 "you are not allowed to get the main thread's message loop");
188
189 std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
190 return message_loop_;
191 }
192
EnableRealTimeScheduling()193 bool MessageLoopThread::EnableRealTimeScheduling() {
194 std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
195
196 if (!IsRunning()) {
197 LOG(ERROR) << __func__ << ": thread " << *this << " is not running";
198 return false;
199 }
200
201 struct sched_param rt_params = {.sched_priority =
202 kRealTimeFifoSchedulingPriority};
203 int rc = sched_setscheduler(linux_tid_, SCHED_FIFO, &rt_params);
204 if (rc != 0) {
205 LOG(ERROR) << __func__ << ": unable to set SCHED_FIFO priority "
206 << kRealTimeFifoSchedulingPriority << " for linux_tid "
207 << std::to_string(linux_tid_) << ", thread " << *this
208 << ", error: " << strerror(errno);
209 return false;
210 }
211 return true;
212 }
213
GetWeakPtr()214 base::WeakPtr<MessageLoopThread> MessageLoopThread::GetWeakPtr() {
215 std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
216 return weak_ptr_factory_.GetWeakPtr();
217 }
218
Run(std::promise<void> start_up_promise)219 void MessageLoopThread::Run(std::promise<void> start_up_promise) {
220 {
221 std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
222 if (is_main_ && init_flags::gd_rust_is_enabled()) {
223 return;
224 }
225
226 LOG(INFO) << __func__ << ": message loop starting for thread "
227 << thread_name_;
228 base::PlatformThread::SetName(thread_name_);
229 message_loop_ = new btbase::AbstractMessageLoop();
230 run_loop_ = new base::RunLoop();
231 thread_id_ = base::PlatformThread::CurrentId();
232 linux_tid_ = static_cast<pid_t>(syscall(SYS_gettid));
233 start_up_promise.set_value();
234 }
235
236 // Blocking until ShutDown() is called
237 run_loop_->Run();
238
239 {
240 std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
241 thread_id_ = -1;
242 linux_tid_ = -1;
243 delete message_loop_;
244 message_loop_ = nullptr;
245 delete run_loop_;
246 run_loop_ = nullptr;
247 LOG(INFO) << __func__ << ": message loop finished for thread "
248 << thread_name_;
249 }
250 }
251
252 } // namespace common
253
254 } // namespace bluetooth
255