Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_RUN_LOOP_H_ |
| 6 | #define BASE_RUN_LOOP_H_ |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 7 | |
Gabriel Charette | 1ef212b | 2017-12-03 12:47:21 | [diff] [blame] | 8 | #include <utility> |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 11 | #include "base/base_export.h" |
Brett Wilson | 1f07f20e | 2017-10-02 18:55:28 | [diff] [blame] | 12 | #include "base/containers/stack.h" |
Gabriel Charette | 2a5335017 | 2021-05-06 20:22:55 | [diff] [blame] | 13 | #include "base/dcheck_is_on.h" |
Avi Drissman | 63e1f99 | 2023-01-13 18:54:43 | [diff] [blame] | 14 | #include "base/functional/callback.h" |
Wez | 9d5dd28 | 2020-02-10 17:21:22 | [diff] [blame] | 15 | #include "base/gtest_prod_util.h" |
danakj | e125e8d6 | 2021-01-21 22:06:31 | [diff] [blame] | 16 | #include "base/location.h" |
Keishi Hattori | e175ac5 | 2022-06-07 06:24:57 | [diff] [blame] | 17 | #include "base/memory/raw_ptr.h" |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 18 | #include "base/memory/weak_ptr.h" |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 19 | #include "base/observer_list.h" |
gab | 980a5271 | 2017-05-18 16:20:16 | [diff] [blame] | 20 | #include "base/sequence_checker.h" |
ahest | 72c1b44 | 2016-12-09 20:40:38 | [diff] [blame] | 21 | #include "base/threading/thread_checker.h" |
Wez | d9e4cb77 | 2019-01-09 03:07:03 | [diff] [blame] | 22 | #include "base/time/time.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 23 | #include "build/build_config.h" |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 24 | |
| 25 | namespace base { |
Wez | 9d5dd28 | 2020-02-10 17:21:22 | [diff] [blame] | 26 | |
| 27 | namespace test { |
| 28 | class ScopedRunLoopTimeout; |
| 29 | class ScopedDisableRunLoopTimeout; |
| 30 | } // namespace test |
| 31 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 32 | #if BUILDFLAG(IS_ANDROID) |
Egor Pasko | 6da5a07c | 2024-03-11 19:56:29 | [diff] [blame] | 33 | class MessagePumpAndroid; |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 34 | #endif |
| 35 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 36 | #if BUILDFLAG(IS_IOS) |
[email protected] | feb727e | 2012-07-13 11:02:57 | [diff] [blame] | 37 | class MessagePumpUIApplication; |
| 38 | #endif |
| 39 | |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 40 | class SingleThreadTaskRunner; |
| 41 | |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 42 | // Helper class to run the RunLoop::Delegate associated with the current thread. |
| 43 | // A RunLoop::Delegate must have been bound to this thread (ref. |
| 44 | // RunLoop::RegisterDelegateForCurrentThread()) prior to using any of RunLoop's |
| 45 | // member and static methods unless explicitly indicated otherwise (e.g. |
| 46 | // IsRunning/IsNestedOnCurrentThread()). RunLoop::Run can only be called once |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 47 | // per RunLoop lifetime. Create a RunLoop on the stack and call Run/Quit to run |
Gabriel Charette | 3a2ce02 | 2020-05-28 19:44:35 | [diff] [blame] | 48 | // a nested RunLoop but please avoid nested loops in production code! |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 49 | class BASE_EXPORT RunLoop { |
| 50 | public: |
Gabriel Charette | 3ff403e | 2017-08-07 04:22:48 | [diff] [blame] | 51 | // The type of RunLoop: a kDefault RunLoop at the top-level (non-nested) will |
| 52 | // process system and application tasks assigned to its Delegate. When nested |
| 53 | // however a kDefault RunLoop will only process system tasks while a |
| 54 | // kNestableTasksAllowed RunLoop will continue to process application tasks |
| 55 | // even if nested. |
| 56 | // |
| 57 | // This is relevant in the case of recursive RunLoops. Some unwanted run loops |
| 58 | // may occur when using common controls or printer functions. By default, |
| 59 | // recursive task processing is disabled. |
| 60 | // |
| 61 | // In general, nestable RunLoops are to be avoided. They are dangerous and |
Gabriel Charette | d883944 | 2018-03-15 18:56:22 | [diff] [blame] | 62 | // difficult to get right, so please use with extreme caution. |
Gabriel Charette | 3ff403e | 2017-08-07 04:22:48 | [diff] [blame] | 63 | // |
| 64 | // A specific example where this makes a difference is: |
| 65 | // - The thread is running a RunLoop. |
| 66 | // - It receives a task #1 and executes it. |
| 67 | // - The task #1 implicitly starts a RunLoop, like a MessageBox in the unit |
| 68 | // test. This can also be StartDoc or GetSaveFileName. |
| 69 | // - The thread receives a task #2 before or while in this second RunLoop. |
| 70 | // - With a kNestableTasksAllowed RunLoop, the task #2 will run right away. |
| 71 | // Otherwise, it will get executed right after task #1 completes in the main |
| 72 | // RunLoop. |
| 73 | enum class Type { |
| 74 | kDefault, |
| 75 | kNestableTasksAllowed, |
| 76 | }; |
| 77 | |
David Bienvenu | 5f4d4f0 | 2020-09-27 16:55:03 | [diff] [blame] | 78 | explicit RunLoop(Type type = Type::kDefault); |
| 79 | RunLoop(const RunLoop&) = delete; |
| 80 | RunLoop& operator=(const RunLoop&) = delete; |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 81 | ~RunLoop(); |
| 82 | |
Gabriel Charette | 3a2ce02 | 2020-05-28 19:44:35 | [diff] [blame] | 83 | // Run the current RunLoop::Delegate. This blocks until Quit is called |
| 84 | // (directly or by running the RunLoop::QuitClosure). |
danakj | e125e8d6 | 2021-01-21 22:06:31 | [diff] [blame] | 85 | void Run(const Location& location = Location::Current()); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 86 | |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 87 | // Run the current RunLoop::Delegate until it doesn't find any tasks or |
Gabriel Charette | 69ab2e0 | 2019-01-15 15:41:05 | [diff] [blame] | 88 | // messages in its queue (it goes idle). |
Gabriel Charette | 76a5534 | 2019-01-16 00:26:11 | [diff] [blame] | 89 | // WARNING #1: This may run long (flakily timeout) and even never return! Do |
| 90 | // not use this when repeating tasks such as animated web pages |
| 91 | // are present. |
Gabriel Charette | 69ab2e0 | 2019-01-15 15:41:05 | [diff] [blame] | 92 | // WARNING #2: This may return too early! For example, if used to run until an |
| 93 | // incoming event has occurred but that event depends on a task in |
Gabriel Charette | bd9c67b | 2019-03-20 15:22:34 | [diff] [blame] | 94 | // a different queue -- e.g. another TaskRunner or a system event. |
| 95 | // Per the warnings above, this tends to lead to flaky tests; prefer |
Gabriel Charette | 69ab2e0 | 2019-01-15 15:41:05 | [diff] [blame] | 96 | // QuitClosure()+Run() when at all possible. |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 97 | void RunUntilIdle(); |
| 98 | |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 99 | bool running() const { |
gab | 980a5271 | 2017-05-18 16:20:16 | [diff] [blame] | 100 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 101 | return running_; |
| 102 | } |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 103 | |
Gabriel Charette | 3a2ce02 | 2020-05-28 19:44:35 | [diff] [blame] | 104 | // Quit() transitions this RunLoop to a state where no more tasks will be |
| 105 | // allowed to run at the run-loop-level of this RunLoop. If invoked from the |
| 106 | // owning thread, the effect is immediate; otherwise it is thread-safe but |
| 107 | // asynchronous. When the transition takes effect, the underlying message loop |
| 108 | // quits this run-loop-level if it is topmost (otherwise the desire to quit |
| 109 | // this level is saved until run-levels nested above it are quit). |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 110 | // |
Gabriel Charette | 3a2ce02 | 2020-05-28 19:44:35 | [diff] [blame] | 111 | // QuitWhenIdle() results in this RunLoop returning true from |
| 112 | // ShouldQuitWhenIdle() at this run-level (the delegate decides when "idle" is |
| 113 | // reached). This is also thread-safe. |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 114 | // |
Gabriel Charette | 3a2ce02 | 2020-05-28 19:44:35 | [diff] [blame] | 115 | // There can be other nested RunLoops servicing the same task queue. As |
| 116 | // mentioned above, quitting one RunLoop has no bearing on the others. Hence, |
| 117 | // you may never assume that a call to Quit() will terminate the underlying |
| 118 | // message loop. If a nested RunLoop continues running, the target may NEVER |
| 119 | // terminate. |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 120 | void Quit(); |
fdoray | a4f28ec | 2016-06-10 00:08:58 | [diff] [blame] | 121 | void QuitWhenIdle(); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 122 | |
kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 123 | // Returns a RepeatingClosure that safely calls Quit() or QuitWhenIdle() (has |
| 124 | // no effect if the RunLoop instance is gone). |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 125 | // |
Gabriel Charette | 3a2ce02 | 2020-05-28 19:44:35 | [diff] [blame] | 126 | // The closures must be obtained from the thread owning the RunLoop but may |
| 127 | // then be invoked from any thread. |
Gabriel Charette | d913e5b | 2019-06-27 05:41:59 | [diff] [blame] | 128 | // |
kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 129 | // Returned closures may be safely: |
Gabriel Charette | d913e5b | 2019-06-27 05:41:59 | [diff] [blame] | 130 | // * Passed to other threads. |
| 131 | // * Run() from other threads, though this will quit the RunLoop |
| 132 | // asynchronously. |
| 133 | // * Run() after the RunLoop has stopped or been destroyed, in which case |
| 134 | // they are a no-op). |
| 135 | // * Run() before RunLoop::Run(), in which case RunLoop::Run() returns |
| 136 | // immediately." |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 137 | // |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 138 | // Example: |
| 139 | // RunLoop run_loop; |
Gabriel Charette | d913e5b | 2019-06-27 05:41:59 | [diff] [blame] | 140 | // DoFooAsyncAndNotify(run_loop.QuitClosure()); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 141 | // run_loop.Run(); |
Gabriel Charette | d913e5b | 2019-06-27 05:41:59 | [diff] [blame] | 142 | // |
| 143 | // Note that Quit() itself is thread-safe and may be invoked directly if you |
| 144 | // have access to the RunLoop reference from another thread (e.g. from a |
| 145 | // capturing lambda or test observer). |
Peter Kasting | 0bc6a68 | 2024-09-09 09:38:34 | [diff] [blame] | 146 | [[nodiscard]] RepeatingClosure QuitClosure() &; |
| 147 | [[nodiscard]] RepeatingClosure QuitWhenIdleClosure() &; |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 148 | |
Gabriel Charette | 2a5335017 | 2021-05-06 20:22:55 | [diff] [blame] | 149 | // Returns true if Quit() or QuitWhenIdle() was called. |
| 150 | bool AnyQuitCalled(); |
| 151 | |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 152 | // Returns true if there is an active RunLoop on this thread. |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 153 | // Safe to call before RegisterDelegateForCurrentThread(). |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 154 | static bool IsRunningOnCurrentThread(); |
| 155 | |
| 156 | // Returns true if there is an active RunLoop on this thread and it's nested |
| 157 | // within another active RunLoop. |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 158 | // Safe to call before RegisterDelegateForCurrentThread(). |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 159 | static bool IsNestedOnCurrentThread(); |
| 160 | |
Francois Doray | 80bdddf | 2018-01-04 16:17:32 | [diff] [blame] | 161 | // A NestingObserver is notified when a nested RunLoop begins and ends. |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 162 | class BASE_EXPORT NestingObserver { |
| 163 | public: |
Francois Doray | 80bdddf | 2018-01-04 16:17:32 | [diff] [blame] | 164 | // Notified before a nested loop starts running work on the current thread. |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 165 | virtual void OnBeginNestedRunLoop() = 0; |
Francois Doray | 80bdddf | 2018-01-04 16:17:32 | [diff] [blame] | 166 | // Notified after a nested loop is done running work on the current thread. |
| 167 | virtual void OnExitNestedRunLoop() {} |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 168 | |
| 169 | protected: |
| 170 | virtual ~NestingObserver() = default; |
| 171 | }; |
| 172 | |
| 173 | static void AddNestingObserverOnCurrentThread(NestingObserver* observer); |
| 174 | static void RemoveNestingObserverOnCurrentThread(NestingObserver* observer); |
| 175 | |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 176 | // A RunLoop::Delegate is a generic interface that allows RunLoop to be |
Gabriel Charette | a3ec961 | 2017-12-14 17:22:40 | [diff] [blame] | 177 | // separate from the underlying implementation of the message loop for this |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 178 | // thread. It holds private state used by RunLoops on its associated thread. |
| 179 | // One and only one RunLoop::Delegate must be registered on a given thread |
| 180 | // via RunLoop::RegisterDelegateForCurrentThread() before RunLoop instances |
| 181 | // and RunLoop static methods can be used on it. |
| 182 | class BASE_EXPORT Delegate { |
Gabriel Charette | 46c535d2 | 2017-12-03 12:14:01 | [diff] [blame] | 183 | public: |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 184 | Delegate(); |
David Bienvenu | 5f4d4f0 | 2020-09-27 16:55:03 | [diff] [blame] | 185 | Delegate(const Delegate&) = delete; |
| 186 | Delegate& operator=(const Delegate&) = delete; |
Gabriel Charette | 1ef212b | 2017-12-03 12:47:21 | [diff] [blame] | 187 | virtual ~Delegate(); |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 188 | |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 189 | // Used by RunLoop to inform its Delegate to Run/Quit. Implementations are |
| 190 | // expected to keep on running synchronously from the Run() call until the |
Alex Clarke | 9752bbf | 2019-04-01 10:41:20 | [diff] [blame] | 191 | // eventual matching Quit() call or a delay of |timeout| expires. Upon |
| 192 | // receiving a Quit() call or timing out it should return from the Run() |
| 193 | // call as soon as possible without executing remaining tasks/messages. |
| 194 | // Run() calls can nest in which case each Quit() call should result in the |
| 195 | // topmost active Run() call returning. The only other trigger for Run() |
| 196 | // to return is the |should_quit_when_idle_callback_| which the Delegate |
| 197 | // should probe before sleeping when it becomes idle. |
| 198 | // |application_tasks_allowed| is true if this is the first Run() call on |
| 199 | // the stack or it was made from a nested RunLoop of |
| 200 | // Type::kNestableTasksAllowed (otherwise this Run() level should only |
| 201 | // process system tasks). |
| 202 | virtual void Run(bool application_tasks_allowed, TimeDelta timeout) = 0; |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 203 | virtual void Quit() = 0; |
| 204 | |
Gabriel Charette | 3ff403e | 2017-08-07 04:22:48 | [diff] [blame] | 205 | // Invoked right before a RunLoop enters a nested Run() call on this |
| 206 | // Delegate iff this RunLoop is of type kNestableTasksAllowed. The Delegate |
| 207 | // should ensure that the upcoming Run() call will result in processing |
| 208 | // application tasks queued ahead of it without further probing. e.g. |
| 209 | // message pumps on some platforms, like Mac, need an explicit request to |
| 210 | // process application tasks when nested, otherwise they'll only wait for |
| 211 | // system messages. |
| 212 | virtual void EnsureWorkScheduled() = 0; |
| 213 | |
Gabriel Charette | a3ec961 | 2017-12-14 17:22:40 | [diff] [blame] | 214 | protected: |
| 215 | // Returns the result of this Delegate's |should_quit_when_idle_callback_|. |
Gabriel Charette | fbf3c62 | 2020-11-19 14:51:13 | [diff] [blame] | 216 | // "protected" so it can be invoked only by the Delegate itself. The |
| 217 | // Delegate is expected to quit Run() if this returns true. |
Gabriel Charette | a3ec961 | 2017-12-14 17:22:40 | [diff] [blame] | 218 | bool ShouldQuitWhenIdle(); |
| 219 | |
Gabriel Charette | 46c535d2 | 2017-12-03 12:14:01 | [diff] [blame] | 220 | private: |
| 221 | // While the state is owned by the Delegate subclass, only RunLoop can use |
| 222 | // it. |
| 223 | friend class RunLoop; |
| 224 | |
Hans Wennborg | 9b292ba | 2022-02-15 16:01:29 | [diff] [blame] | 225 | friend class ScopedDisallowRunningRunLoop; |
| 226 | |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 227 | // A vector-based stack is more memory efficient than the default |
| 228 | // deque-based stack as the active RunLoop stack isn't expected to ever |
| 229 | // have more than a few entries. |
Ali Hijazi | e63cbaf6 | 2023-12-20 19:29:35 | [diff] [blame] | 230 | using RunLoopStack = |
| 231 | stack<raw_ptr<RunLoop, VectorExperimental>, |
| 232 | std::vector<raw_ptr<RunLoop, VectorExperimental>>>; |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 233 | |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 234 | RunLoopStack active_run_loops_; |
Trent Apted | a250ec3ab | 2018-08-19 08:52:19 | [diff] [blame] | 235 | ObserverList<RunLoop::NestingObserver>::Unchecked nesting_observers_; |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 236 | |
Gabriel Charette | a4497505 | 2017-08-21 23:14:04 | [diff] [blame] | 237 | #if DCHECK_IS_ON() |
| 238 | bool allow_running_for_testing_ = true; |
| 239 | #endif |
| 240 | |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 241 | // True once this Delegate is bound to a thread via |
| 242 | // RegisterDelegateForCurrentThread(). |
| 243 | bool bound_ = false; |
| 244 | |
gab | 980a5271 | 2017-05-18 16:20:16 | [diff] [blame] | 245 | // Thread-affine per its use of TLS. |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 246 | THREAD_CHECKER(bound_thread_checker_); |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 247 | }; |
| 248 | |
| 249 | // Registers |delegate| on the current thread. Must be called once and only |
| 250 | // once per thread before using RunLoop methods on it. |delegate| is from then |
Gabriel Charette | a3ec961 | 2017-12-14 17:22:40 | [diff] [blame] | 251 | // on forever bound to that thread (including its destruction). |
Peter Kasting | 960e2d3 | 2023-03-14 17:18:41 | [diff] [blame] | 252 | static void RegisterDelegateForCurrentThread(Delegate* new_delegate); |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 253 | |
Wez | 9d5dd28 | 2020-02-10 17:21:22 | [diff] [blame] | 254 | // Support for //base/test/scoped_run_loop_timeout.h. |
| 255 | // This must be public for access by the implementation code in run_loop.cc. |
| 256 | struct BASE_EXPORT RunLoopTimeout { |
| 257 | RunLoopTimeout(); |
| 258 | ~RunLoopTimeout(); |
| 259 | TimeDelta timeout; |
danakj | e125e8d6 | 2021-01-21 22:06:31 | [diff] [blame] | 260 | RepeatingCallback<void(const Location&)> on_timeout; |
Wez | 9d5dd28 | 2020-02-10 17:21:22 | [diff] [blame] | 261 | }; |
| 262 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 263 | private: |
Carlos Caballero | 40b6d04 | 2020-06-16 06:50:25 | [diff] [blame] | 264 | FRIEND_TEST_ALL_PREFIXES(SingleThreadTaskExecutorTypedTest, |
| 265 | RunLoopQuitOrderAfter); |
Wez | 325eafc | 2018-07-17 17:01:49 | [diff] [blame] | 266 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 267 | #if BUILDFLAG(IS_ANDROID) |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 268 | // Android doesn't support the blocking RunLoop::Run, so it calls |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 269 | // BeforeRun and AfterRun directly. |
Egor Pasko | 6da5a07c | 2024-03-11 19:56:29 | [diff] [blame] | 270 | friend class MessagePumpAndroid; |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 271 | #endif |
| 272 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 273 | #if BUILDFLAG(IS_IOS) |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 274 | // iOS doesn't support the blocking RunLoop::Run, so it calls |
[email protected] | feb727e | 2012-07-13 11:02:57 | [diff] [blame] | 275 | // BeforeRun directly. |
Wez | d9e4cb77 | 2019-01-09 03:07:03 | [diff] [blame] | 276 | friend class MessagePumpUIApplication; |
[email protected] | feb727e | 2012-07-13 11:02:57 | [diff] [blame] | 277 | #endif |
| 278 | |
Wez | 9d5dd28 | 2020-02-10 17:21:22 | [diff] [blame] | 279 | // Support for //base/test/scoped_run_loop_timeout.h. |
| 280 | friend class test::ScopedRunLoopTimeout; |
| 281 | friend class test::ScopedDisableRunLoopTimeout; |
| 282 | |
| 283 | static void SetTimeoutForCurrentThread(const RunLoopTimeout* timeout); |
| 284 | static const RunLoopTimeout* GetTimeoutForCurrentThread(); |
| 285 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 286 | // Return false to abort the Run. |
| 287 | bool BeforeRun(); |
| 288 | void AfterRun(); |
| 289 | |
Gabriel Charette | b87279d | 2019-06-27 03:21:04 | [diff] [blame] | 290 | // A cached reference of RunLoop::Delegate for the thread driven by this |
| 291 | // RunLoop for quick access without using TLS (also allows access to state |
| 292 | // from another sequence during Run(), ref. |sequence_checker_| below). |
Bartek Nowierski | f5eeeba | 2024-01-25 12:49:39 | [diff] [blame] | 293 | const raw_ptr<Delegate, DanglingUntriaged> delegate_; |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 294 | |
Gabriel Charette | 3ff403e | 2017-08-07 04:22:48 | [diff] [blame] | 295 | const Type type_; |
| 296 | |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 297 | #if DCHECK_IS_ON() |
Gabriel Charette | 2a5335017 | 2021-05-06 20:22:55 | [diff] [blame] | 298 | bool run_allowed_ = true; |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 299 | #endif |
| 300 | |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 301 | bool quit_called_ = false; |
| 302 | bool running_ = false; |
Gabriel Charette | 2a5335017 | 2021-05-06 20:22:55 | [diff] [blame] | 303 | |
| 304 | // Used to record that QuitWhenIdle() was called on this RunLoop. |
| 305 | bool quit_when_idle_called_ = false; |
| 306 | // Whether the Delegate should quit Run() once it becomes idle (it's |
| 307 | // responsible for probing this state via ShouldQuitWhenIdle()). This state is |
| 308 | // stored here rather than pushed to Delegate to support nested RunLoops. |
| 309 | bool quit_when_idle_ = false; |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 310 | |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 311 | // RunLoop is not thread-safe. Its state/methods, unless marked as such, may |
| 312 | // not be accessed from any other sequence than the thread it was constructed |
| 313 | // on. Exception: RunLoop can be safely accessed from one other sequence (or |
| 314 | // single parallel task) during Run() -- e.g. to Quit() without having to |
Sean Maher | e672a66 | 2023-01-09 21:42:28 | [diff] [blame] | 315 | // plumb SingleThreadTaskRunner::GetCurrentDefault() throughout a test to |
| 316 | // repost QuitClosure to origin thread. |
gab | 980a5271 | 2017-05-18 16:20:16 | [diff] [blame] | 317 | SEQUENCE_CHECKER(sequence_checker_); |
ahest | 72c1b44 | 2016-12-09 20:40:38 | [diff] [blame] | 318 | |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 319 | const scoped_refptr<SingleThreadTaskRunner> origin_task_runner_; |
| 320 | |
[email protected] | dcf1063 | 2013-10-08 19:23:33 | [diff] [blame] | 321 | // WeakPtrFactory for QuitClosure safety. |
Jeremy Roman | 577d8849 | 2019-07-05 14:30:23 | [diff] [blame] | 322 | WeakPtrFactory<RunLoop> weak_factory_{this}; |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 323 | }; |
| 324 | |
Hans Wennborg | 9b292ba | 2022-02-15 16:01:29 | [diff] [blame] | 325 | // RunLoop::Run() will DCHECK if called while there's a |
| 326 | // ScopedDisallowRunningRunLoop in scope on its thread. This is useful to add |
| 327 | // safety to some test constructs which allow multiple task runners to share the |
| 328 | // main thread in unit tests. While the main thread can be shared by multiple |
| 329 | // runners to deterministically fake multi threading, there can still only be a |
| 330 | // single RunLoop::Delegate per thread and RunLoop::Run() should only be invoked |
| 331 | // from it (or it would result in incorrectly driving TaskRunner A while in |
| 332 | // TaskRunner B's context). |
Peter Kasting | 960e2d3 | 2023-03-14 17:18:41 | [diff] [blame] | 333 | class BASE_EXPORT [[maybe_unused, nodiscard]] ScopedDisallowRunningRunLoop { |
Hans Wennborg | 9b292ba | 2022-02-15 16:01:29 | [diff] [blame] | 334 | public: |
| 335 | ScopedDisallowRunningRunLoop(); |
| 336 | ScopedDisallowRunningRunLoop(const ScopedDisallowRunningRunLoop&) = delete; |
| 337 | ScopedDisallowRunningRunLoop& operator=(const ScopedDisallowRunningRunLoop&) = |
| 338 | delete; |
| 339 | ~ScopedDisallowRunningRunLoop(); |
| 340 | |
| 341 | private: |
| 342 | #if DCHECK_IS_ON() |
Keishi Hattori | e175ac5 | 2022-06-07 06:24:57 | [diff] [blame] | 343 | raw_ptr<RunLoop::Delegate> current_delegate_; |
Hans Wennborg | 9b292ba | 2022-02-15 16:01:29 | [diff] [blame] | 344 | const bool previous_run_allowance_; |
| 345 | #endif // DCHECK_IS_ON() |
| 346 | }; |
| 347 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 348 | } // namespace base |
| 349 | |
| 350 | #endif // BASE_RUN_LOOP_H_ |