blob: e2c2e2d92e426cdf24557c04e2cfaf5da3de8044 [file] [log] [blame]
Jack Hec27d2572018-07-25 12:02:22 -07001/*
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
Jakub Pawlowski67f5f372018-07-23 10:00:25 -070017#include "message_loop_thread.h"
18
Jack Hec27d2572018-07-25 12:02:22 -070019#include <sys/syscall.h>
20#include <unistd.h>
21#include <thread>
22
23#include <base/strings/stringprintf.h>
24
Jack Hec27d2572018-07-25 12:02:22 -070025namespace bluetooth {
26
27namespace common {
28
29static constexpr int kRealTimeFifoSchedulingPriority = 1;
30
31MessageLoopThread::MessageLoopThread(const std::string& thread_name)
32 : thread_name_(thread_name),
33 message_loop_(nullptr),
34 run_loop_(nullptr),
35 thread_(nullptr),
36 thread_id_(-1),
Hansong Zhang438b08c2018-08-14 14:29:23 -070037 linux_tid_(-1),
Hansong Zhangd7831402018-09-29 14:23:31 -070038 weak_ptr_factory_(this),
39 shutting_down_(false) {}
Jack Hec27d2572018-07-25 12:02:22 -070040
Hansong Zhangd7831402018-09-29 14:23:31 -070041MessageLoopThread::~MessageLoopThread() { ShutDown(); }
Jack Hec27d2572018-07-25 12:02:22 -070042
43void MessageLoopThread::StartUp() {
Jack Hef607f2d2018-08-24 17:14:10 -070044 std::promise<void> start_up_promise;
45 std::future<void> start_up_future = start_up_promise.get_future();
Hansong Zhangd7831402018-09-29 14:23:31 -070046 {
47 std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
48 if (thread_ != nullptr) {
49 LOG(WARNING) << __func__ << ": thread " << *this << " is already started";
50
51 return;
52 }
53 thread_ = new std::thread(&MessageLoopThread::RunThread, this,
54 std::move(start_up_promise));
55 }
Jack Hef607f2d2018-08-24 17:14:10 -070056 start_up_future.wait();
Jack Hec27d2572018-07-25 12:02:22 -070057}
58
Jakub Pawlowski67f5f372018-07-23 10:00:25 -070059bool MessageLoopThread::DoInThread(const base::Location& from_here,
Jack Hec27d2572018-07-25 12:02:22 -070060 base::OnceClosure task) {
Hansong Zhang438b08c2018-08-14 14:29:23 -070061 return DoInThreadDelayed(from_here, std::move(task), base::TimeDelta());
62}
63
Jakub Pawlowski67f5f372018-07-23 10:00:25 -070064bool MessageLoopThread::DoInThreadDelayed(const base::Location& from_here,
65 base::OnceClosure task,
66 const base::TimeDelta& delay) {
Jack Hec27d2572018-07-25 12:02:22 -070067 std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
68 if (message_loop_ == nullptr) {
69 LOG(ERROR) << __func__ << ": message loop is null for thread " << *this
70 << ", from " << from_here.ToString();
71 return false;
72 }
Hansong Zhang438b08c2018-08-14 14:29:23 -070073 if (!message_loop_->task_runner()->PostDelayedTask(from_here, std::move(task),
74 delay)) {
Jack Hec27d2572018-07-25 12:02:22 -070075 LOG(ERROR) << __func__
76 << ": failed to post task to message loop for thread " << *this
77 << ", from " << from_here.ToString();
78 return false;
79 }
80 return true;
81}
82
83void MessageLoopThread::ShutDown() {
Hansong Zhangd7831402018-09-29 14:23:31 -070084 {
85 std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
86 if (thread_ == nullptr) {
87 LOG(INFO) << __func__ << ": thread " << *this << " is already stopped";
88 return;
89 }
90 if (message_loop_ == nullptr) {
91 LOG(INFO) << __func__ << ": message_loop_ is null. Already stopping";
92 return;
93 }
94 if (shutting_down_) {
95 LOG(INFO) << __func__ << ": waiting for thread to join";
96 return;
97 }
98 shutting_down_ = true;
99 CHECK_NE(thread_id_, base::PlatformThread::CurrentId())
100 << __func__ << " should not be called on the thread itself. "
101 << "Otherwise, deadlock may happen.";
Jack Hec27d2572018-07-25 12:02:22 -0700102 if (!message_loop_->task_runner()->PostTask(
103 FROM_HERE, run_loop_->QuitWhenIdleClosure())) {
104 LOG(FATAL) << __func__
105 << ": failed to post task to message loop for thread "
106 << *this;
107 }
108 }
109 thread_->join();
Hansong Zhangd7831402018-09-29 14:23:31 -0700110 {
111 std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
112 delete thread_;
113 thread_ = nullptr;
114 shutting_down_ = false;
115 }
Jack Hec27d2572018-07-25 12:02:22 -0700116}
117
118base::PlatformThreadId MessageLoopThread::GetThreadId() const {
119 std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
120 return thread_id_;
121}
122
123std::string MessageLoopThread::GetName() const {
Jack Hec27d2572018-07-25 12:02:22 -0700124 return thread_name_;
125}
126
127std::string MessageLoopThread::ToString() const {
128 std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
129 return base::StringPrintf("%s(%d)", thread_name_.c_str(), thread_id_);
130}
131
132bool MessageLoopThread::IsRunning() const {
133 std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
134 return message_loop_ != nullptr;
135}
136
137// Non API method, should not be protected by API mutex
Jack Hef607f2d2018-08-24 17:14:10 -0700138void MessageLoopThread::RunThread(MessageLoopThread* thread,
139 std::promise<void> start_up_promise) {
140 thread->Run(std::move(start_up_promise));
Jack Hec27d2572018-07-25 12:02:22 -0700141}
142
143base::MessageLoop* MessageLoopThread::message_loop() const {
144 std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
145 return message_loop_;
146}
147
148bool MessageLoopThread::EnableRealTimeScheduling() {
149 std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
150 if (!IsRunning()) {
151 LOG(ERROR) << __func__ << ": thread " << *this << " is not running";
152 return false;
153 }
154 struct sched_param rt_params = {.sched_priority =
155 kRealTimeFifoSchedulingPriority};
156 int rc = sched_setscheduler(linux_tid_, SCHED_FIFO, &rt_params);
157 if (rc != 0) {
158 LOG(ERROR) << __func__ << ": unable to set SCHED_FIFO priority "
159 << kRealTimeFifoSchedulingPriority << " for linux_tid "
160 << std::to_string(linux_tid_) << ", thread " << *this
161 << ", error: " << strerror(errno);
162 return false;
163 }
164 return true;
165}
166
Hansong Zhang438b08c2018-08-14 14:29:23 -0700167base::WeakPtr<MessageLoopThread> MessageLoopThread::GetWeakPtr() {
168 std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
169 return weak_ptr_factory_.GetWeakPtr();
170}
171
Jack Hef607f2d2018-08-24 17:14:10 -0700172void MessageLoopThread::Run(std::promise<void> start_up_promise) {
Hansong Zhangd7831402018-09-29 14:23:31 -0700173 {
174 std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
175 LOG(INFO) << __func__ << ": message loop starting for thread "
176 << thread_name_;
177 base::PlatformThread::SetName(thread_name_);
178 message_loop_ = new base::MessageLoop();
179 run_loop_ = new base::RunLoop();
180 thread_id_ = base::PlatformThread::CurrentId();
181 linux_tid_ = static_cast<pid_t>(syscall(SYS_gettid));
182 start_up_promise.set_value();
183 }
184
Jack Hec27d2572018-07-25 12:02:22 -0700185 // Blocking until ShutDown() is called
186 run_loop_->Run();
Hansong Zhangd7831402018-09-29 14:23:31 -0700187
188 {
189 std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
190 thread_id_ = -1;
191 linux_tid_ = -1;
192 delete message_loop_;
193 message_loop_ = nullptr;
194 delete run_loop_;
195 run_loop_ = nullptr;
196 LOG(INFO) << __func__ << ": message loop finished for thread "
197 << thread_name_;
198 }
Jack Hec27d2572018-07-25 12:02:22 -0700199}
200
201} // namespace common
202
203} // namespace bluetooth