summaryrefslogtreecommitdiff
path: root/system/common/message_loop_thread.cc
diff options
context:
space:
mode:
authorHansong Zhang <hsz@google.com>2018-08-14 14:29:23 -0700
committerHansong Zhang <hsz@google.com>2018-09-13 13:37:42 -0700
commitf05a8c49a889b5a83841e4bf55dd3cf5a1afa8d0 (patch)
tree5bc7d1343c049f61e0bafec0158a3f336dc2b137 /system/common/message_loop_thread.cc
parent76250727b2fa2a5a6364edcb20e4d379c82090a4 (diff)
Add Timer as an alternative to osi alarm
* Add a private method MessageLoopThread.DoInThreadDelayed to post a delayed task in message loop, as an alternative approach to osi alarm clock * Add a unit test for MessageLoopThread to check ShutDown() waits until current task finishes * Add Timer using MessageLoopThread.DoInThreadDelayed * Timer provides similar API as osi alarm, and uses same OS clock (boot timer) as alarm * Add benchmark and unit tests to ensure the performance is comparable to the existing osi alarm Test: Run unit test and benchmark test ./test/run_unit_tests.sh bluetooth_test_common ./test/run_benchmarks.sh bluetooth_benchmark_timer_performance --benchmark_repetitions=10 --benchmark_report_aggregates_only=true Bug: 110303473 Change-Id: I6f2e7ae2f80f9889fc5fe3c8cd6b9b2670938b46
Diffstat (limited to 'system/common/message_loop_thread.cc')
-rw-r--r--system/common/message_loop_thread.cc17
1 files changed, 15 insertions, 2 deletions
diff --git a/system/common/message_loop_thread.cc b/system/common/message_loop_thread.cc
index 9bf25bef16..f57ce46b7c 100644
--- a/system/common/message_loop_thread.cc
+++ b/system/common/message_loop_thread.cc
@@ -34,7 +34,8 @@ MessageLoopThread::MessageLoopThread(const std::string& thread_name)
run_loop_(nullptr),
thread_(nullptr),
thread_id_(-1),
- linux_tid_(-1) {}
+ linux_tid_(-1),
+ weak_ptr_factory_(this) {}
MessageLoopThread::~MessageLoopThread() {
std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
@@ -58,13 +59,20 @@ void MessageLoopThread::StartUp() {
bool MessageLoopThread::DoInThread(const tracked_objects::Location& from_here,
base::OnceClosure task) {
+ return DoInThreadDelayed(from_here, std::move(task), base::TimeDelta());
+}
+
+bool MessageLoopThread::DoInThreadDelayed(
+ const tracked_objects::Location& from_here, base::OnceClosure task,
+ const base::TimeDelta& delay) {
std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
if (message_loop_ == nullptr) {
LOG(ERROR) << __func__ << ": message loop is null for thread " << *this
<< ", from " << from_here.ToString();
return false;
}
- if (!message_loop_->task_runner()->PostTask(from_here, std::move(task))) {
+ if (!message_loop_->task_runner()->PostDelayedTask(from_here, std::move(task),
+ delay)) {
LOG(ERROR) << __func__
<< ": failed to post task to message loop for thread " << *this
<< ", from " << from_here.ToString();
@@ -145,6 +153,11 @@ bool MessageLoopThread::EnableRealTimeScheduling() {
return true;
}
+base::WeakPtr<MessageLoopThread> MessageLoopThread::GetWeakPtr() {
+ std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
+ return weak_ptr_factory_.GetWeakPtr();
+}
+
// Non API method, should NOT be protected by API mutex to avoid deadlock
void MessageLoopThread::Run(std::promise<void> start_up_promise) {
LOG(INFO) << __func__ << ": message loop starting for thread "