diff options
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); }; |