2 * Copyright 2018 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #include <base/bind.h>
26 #include <base/location.h>
27 #include <base/message_loop/message_loop.h>
28 #include <base/run_loop.h>
29 #include <base/threading/platform_thread.h>
36 * An interface to various thread related functionality
38 class MessageLoopThread final {
41 * Create a message loop thread with name. Thread won't be running until
44 * @param thread_name name of this worker thread
46 explicit MessageLoopThread(const std::string& thread_name);
49 * Destroys the message loop thread automatically when it goes out of scope
54 * Start the underlying thread. Blocks until all thread infrastructure is
55 * setup. IsRunning() and DoInThread() should return true after this call.
56 * Blocks until the thread is successfully started.
58 * Repeated call to this method will only start this thread once
63 * Post a task to run on this thread
65 * @param from_here location where this task is originated
66 * @param task task created through base::Bind()
67 * @return true if task is successfully scheduled, false if task cannot be
70 bool DoInThread(const base::Location& from_here, base::OnceClosure task);
73 * Shutdown the current thread as if it is never started. IsRunning() and
74 * DoInThread() will return false after this call. Blocks until the thread is
75 * joined and freed. This thread can be re-started again using StartUp()
77 * Repeated call to this method will only stop this thread once
79 * NOTE: Should never be called on the thread itself to avoid deadlock
84 * Get the current thread ID returned by PlatformThread::CurrentId()
86 * On Android platform, this value should be the same as the tid logged by
87 * logcat, which is returned by gettid(). On other platform, this thread id
88 * may have different meanings. Therefore, this ID is only good for logging
89 * and thread comparison purpose
91 * @return this thread's ID
93 base::PlatformThreadId GetThreadId() const;
96 * Get this thread's name set in constructor
98 * @return this thread's name set in constructor
100 std::string GetName() const;
103 * Get a string representation of this thread
105 * @return a string representation of this thread
107 std::string ToString() const;
110 * Check if this thread is running
112 * @return true iff this thread is running and is able to do task
114 bool IsRunning() const;
117 * Attempt to make scheduling for this thread real time
119 * @return true on success, false otherwise
121 bool EnableRealTimeScheduling();
124 * Return the weak pointer to this object. This can be useful when posting
125 * delayed tasks to this MessageLoopThread using Timer.
127 base::WeakPtr<MessageLoopThread> GetWeakPtr();
130 * Return the message loop for this thread. Accessing raw message loop is not
131 * recommended as message loop can be freed internally.
133 * @return message loop associated with this thread, nullptr if thread is not
136 base::MessageLoop* message_loop() const;
140 * Static method to run the thread
142 * This is used instead of a C++ lambda because of the use of std::shared_ptr
144 * @param context needs to be a pointer to an instance of MessageLoopThread
145 * @param start_up_promise a std::promise that is used to notify calling
146 * thread the completion of message loop start-up
148 static void RunThread(MessageLoopThread* context,
149 std::promise<void> start_up_promise);
152 * Post a task to run on this thread after a specified delay. If the task
153 * needs to be cancelable before it's run, use base::CancelableClosure type
154 * for task closure. For example:
156 * base::CancelableClosure cancelable_task;
157 * cancelable_task.Reset(base::Bind(...)); // bind the task
158 * same_thread->DoInThreadDelayed(FROM_HERE,
159 * cancelable_task.callback(), delay);
161 * // Cancel the task closure
162 * same_thread->DoInThread(FROM_HERE,
163 * base::Bind(&base::CancelableClosure::Cancel,
164 * base::Unretained(&cancelable_task)));
167 * Warning: base::CancelableClosure objects must be created on, posted to,
168 * cancelled on, and destroyed on the same thread.
170 * @param from_here location where this task is originated
171 * @param task task created through base::Bind()
172 * @param delay delay for the task to be executed
173 * @return true if task is successfully scheduled, false if task cannot be
176 bool DoInThreadDelayed(const base::Location& from_here,
177 base::OnceClosure task, const base::TimeDelta& delay);
179 friend class RepeatingTimer; // allow Timer to use DoInThreadDelayed()
180 friend class OnceTimer; // allow OnceTimer to use DoInThreadDelayed()
183 * Actual method to run the thread, blocking until ShutDown() is called
185 * @param start_up_promise a std::promise that is used to notify calling
186 * thread the completion of message loop start-up
188 void Run(std::promise<void> start_up_promise);
190 mutable std::recursive_mutex api_mutex_;
191 const std::string thread_name_;
192 base::MessageLoop* message_loop_;
193 base::RunLoop* run_loop_;
194 std::thread* thread_;
195 base::PlatformThreadId thread_id_;
196 // Linux specific abstractions
198 base::WeakPtrFactory<MessageLoopThread> weak_ptr_factory_;
201 DISALLOW_COPY_AND_ASSIGN(MessageLoopThread);
204 inline std::ostream& operator<<(std::ostream& os,
205 const bluetooth::common::MessageLoopThread& a) {
210 } // namespace common
212 } // namespace bluetooth