diff options
author | Hansong Zhang <hsz@google.com> | 2018-08-14 14:29:23 -0700 |
---|---|---|
committer | Hansong Zhang <hsz@google.com> | 2018-09-13 13:37:42 -0700 |
commit | f05a8c49a889b5a83841e4bf55dd3cf5a1afa8d0 (patch) | |
tree | 5bc7d1343c049f61e0bafec0158a3f336dc2b137 /system/common/message_loop_thread.h | |
parent | 76250727b2fa2a5a6364edcb20e4d379c82090a4 (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.h')
-rw-r--r-- | system/common/message_loop_thread.h | 39 |
1 files changed, 38 insertions, 1 deletions
diff --git a/system/common/message_loop_thread.h b/system/common/message_loop_thread.h index c342be4014..4db11bb709 100644 --- a/system/common/message_loop_thread.h +++ b/system/common/message_loop_thread.h @@ -122,7 +122,13 @@ class MessageLoopThread final { bool EnableRealTimeScheduling(); /** - * Return the mssage loop for this thread. Accessing raw message loop is not + * Return the weak pointer to this object. This can be useful when posting + * delayed tasks to this MessageLoopThread using Timer. + */ + base::WeakPtr<MessageLoopThread> GetWeakPtr(); + + /** + * Return the message loop for this thread. Accessing raw message loop is not * recommended as message loop can be freed internally. * * @return message loop associated with this thread, nullptr if thread is not @@ -144,6 +150,36 @@ class MessageLoopThread final { std::promise<void> start_up_promise); /** + * Post a task to run on this thread after a specified delay. If the task + * needs to be cancelable before it's run, use base::CancelableClosure type + * for task closure. For example: + * <code> + * base::CancelableClosure cancelable_task; + * cancelable_task.Reset(base::Bind(...)); // bind the task + * same_thread->DoInThreadDelayed(FROM_HERE, + * cancelable_task.callback(), delay); + * ... + * // Cancel the task closure + * same_thread->DoInThread(FROM_HERE, + * base::Bind(&base::CancelableClosure::Cancel, + * base::Unretained(&cancelable_task))); + * </code> + * + * Warning: base::CancelableClosure objects must be created on, posted to, + * cancelled on, and destroyed on the same thread. + * + * @param from_here location where this task is originated + * @param task task created through base::Bind() + * @param delay delay for the task to be executed + * @return true if task is successfully scheduled, false if task cannot be + * scheduled + */ + bool DoInThreadDelayed(const tracked_objects::Location& from_here, + base::OnceClosure task, const base::TimeDelta& delay); + + friend class Timer; // allow Timer to use DoInThreadDelayed() + + /** * Actual method to run the thread, blocking until ShutDown() is called * * @param start_up_promise a std::promise that is used to notify calling @@ -159,6 +195,7 @@ class MessageLoopThread final { base::PlatformThreadId thread_id_; // Linux specific abstractions pid_t linux_tid_; + base::WeakPtrFactory<MessageLoopThread> weak_ptr_factory_; DISALLOW_COPY_AND_ASSIGN(MessageLoopThread); }; |