current_thread.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_TASK_CURRENT_THREAD_H_
  5. #define BASE_TASK_CURRENT_THREAD_H_
  6. #include <ostream>
  7. #include "base/base_export.h"
  8. #include "base/check.h"
  9. #include "base/memory/scoped_refptr.h"
  10. #include "base/message_loop/message_pump_for_io.h"
  11. #include "base/message_loop/message_pump_for_ui.h"
  12. #include "base/pending_task.h"
  13. #include "base/single_thread_task_runner.h"
  14. #include "base/task/task_observer.h"
  15. #include "build/build_config.h"
  16. namespace web {
  17. class WebTaskEnvironment;
  18. }
  19. namespace base {
  20. namespace sequence_manager {
  21. namespace internal {
  22. class SequenceManagerImpl;
  23. }
  24. } // namespace sequence_manager
  25. // CurrentThread is a proxy to a subset of Task related APIs bound to the
  26. // current thread
  27. //
  28. // Current(UI|IO)Thread is available statically through
  29. // Current(UI|IO)Thread::Get() on threads that have registered as CurrentThread
  30. // on this physical thread (e.g. by using SingleThreadTaskExecutor). APIs
  31. // intended for all consumers on the thread should be on Current(UI|IO)Thread,
  32. // while internal APIs might be on multiple internal classes (e.g.
  33. // SequenceManager).
  34. //
  35. // Why: Historically MessageLoop would take care of everything related to event
  36. // processing on a given thread. Nowadays that functionality is split among
  37. // different classes. At that time MessageLoop::current() gave access to the
  38. // full MessageLoop API, preventing both addition of powerful owner-only APIs as
  39. // well as making it harder to remove callers of deprecated APIs (that need to
  40. // stick around for a few owner-only use cases and re-accrue callers after
  41. // cleanup per remaining publicly available).
  42. //
  43. // As such, many methods below are flagged as deprecated and should be removed
  44. // once all static callers have been migrated.
  45. class BASE_EXPORT CurrentThread {
  46. public:
  47. // CurrentThread is effectively just a disguised pointer and is fine to
  48. // copy/move around.
  49. CurrentThread(const CurrentThread& other) = default;
  50. CurrentThread(CurrentThread&& other) = default;
  51. CurrentThread& operator=(const CurrentThread& other) = default;
  52. bool operator==(const CurrentThread& other) const;
  53. // Returns a proxy object to interact with the Task related APIs for the
  54. // current thread. It must only be used on the thread it was obtained.
  55. static CurrentThread Get();
  56. // Return an empty CurrentThread. No methods should be called on this
  57. // object.
  58. static CurrentThread GetNull();
  59. // Returns true if the current thread is registered to expose CurrentThread
  60. // API. Prefer this to verifying the boolean value of Get() (so that Get() can
  61. // ultimately DCHECK it's only invoked when IsSet()).
  62. static bool IsSet();
  63. // Allow CurrentThread to be used like a pointer to support the many
  64. // callsites that used MessageLoop::current() that way when it was a
  65. // MessageLoop*.
  66. CurrentThread* operator->() { return this; }
  67. explicit operator bool() const { return !!current_; }
  68. // A DestructionObserver is notified when the current task execution
  69. // environment is being destroyed. These observers are notified prior to
  70. // CurrentThread::IsSet() being changed to return false. This gives interested
  71. // parties the chance to do final cleanup.
  72. //
  73. // NOTE: Any tasks posted to the current thread during this notification will
  74. // not be run. Instead, they will be deleted.
  75. //
  76. // Deprecation note: Prefer SequenceLocalStorageSlot<std::unique_ptr<Foo>> to
  77. // DestructionObserver to bind an object's lifetime to the current
  78. // thread/sequence.
  79. class BASE_EXPORT DestructionObserver {
  80. public:
  81. // TODO(https://crbug.com/891670): Rename to
  82. // WillDestroyCurrentTaskExecutionEnvironment
  83. virtual void WillDestroyCurrentMessageLoop() = 0;
  84. protected:
  85. virtual ~DestructionObserver() = default;
  86. };
  87. // Add a DestructionObserver, which will start receiving notifications
  88. // immediately.
  89. void AddDestructionObserver(DestructionObserver* destruction_observer);
  90. // Remove a DestructionObserver. It is safe to call this method while a
  91. // DestructionObserver is receiving a notification callback.
  92. void RemoveDestructionObserver(DestructionObserver* destruction_observer);
  93. // Forwards to SequenceManager::SetTaskRunner().
  94. // DEPRECATED(https://crbug.com/825327): only owners of the SequenceManager
  95. // instance should replace its TaskRunner.
  96. void SetTaskRunner(scoped_refptr<SingleThreadTaskRunner> task_runner);
  97. // Forwards to SequenceManager::(Add|Remove)TaskObserver.
  98. // DEPRECATED(https://crbug.com/825327): only owners of the SequenceManager
  99. // instance should add task observers on it.
  100. void AddTaskObserver(TaskObserver* task_observer);
  101. void RemoveTaskObserver(TaskObserver* task_observer);
  102. // When this functionality is enabled, the queue time will be recorded for
  103. // posted tasks.
  104. void SetAddQueueTimeToTasks(bool enable);
  105. // Enables nested task processing in scope of an upcoming native message loop.
  106. // Some unwanted message loops may occur when using common controls or printer
  107. // functions. Hence, nested task processing is disabled by default to avoid
  108. // unplanned reentrancy. This re-enables it in cases where the stack is
  109. // reentrancy safe and processing nestable tasks is explicitly safe.
  110. //
  111. // For instance,
  112. // - The current thread is running a message loop.
  113. // - It receives a task #1 and executes it.
  114. // - The task #1 implicitly starts a nested message loop, like a MessageBox in
  115. // the unit test. This can also be StartDoc or GetSaveFileName.
  116. // - The thread receives a task #2 before or while in this second message
  117. // loop.
  118. // - With NestableTasksAllowed set to true, the task #2 will run right away.
  119. // Otherwise, it will get executed right after task #1 completes at "thread
  120. // message loop level".
  121. //
  122. // Use RunLoop::Type::kNestableTasksAllowed when nesting is triggered by the
  123. // application RunLoop rather than by native code.
  124. class BASE_EXPORT ScopedAllowApplicationTasksInNativeNestedLoop {
  125. public:
  126. ScopedAllowApplicationTasksInNativeNestedLoop();
  127. ~ScopedAllowApplicationTasksInNativeNestedLoop();
  128. private:
  129. sequence_manager::internal::SequenceManagerImpl* const sequence_manager_;
  130. const bool previous_state_;
  131. };
  132. // TODO(https://crbug.com/781352): Remove usage of this old class. Either
  133. // renaming it to ScopedAllowApplicationTasksInNativeNestedLoop when truly
  134. // native or migrating it to RunLoop::Type::kNestableTasksAllowed otherwise.
  135. using ScopedNestableTaskAllower =
  136. ScopedAllowApplicationTasksInNativeNestedLoop;
  137. // Returns true if nestable tasks are allowed on the current thread at this
  138. // time (i.e. if a nested loop would start from the callee's point in the
  139. // stack, would it be allowed to run application tasks).
  140. bool NestableTasksAllowed() const;
  141. // Returns true if this instance is bound to the current thread.
  142. bool IsBoundToCurrentThread() const;
  143. // Returns true if the current thread is idle (ignoring delayed tasks). This
  144. // is the same condition which triggers DoWork() to return false: i.e. out of
  145. // tasks which can be processed at the current run-level -- there might be
  146. // deferred non-nestable tasks remaining if currently in a nested run level.
  147. bool IsIdleForTesting();
  148. protected:
  149. explicit CurrentThread(
  150. sequence_manager::internal::SequenceManagerImpl* sequence_manager)
  151. : current_(sequence_manager) {}
  152. static sequence_manager::internal::SequenceManagerImpl*
  153. GetCurrentSequenceManagerImpl();
  154. friend class MessagePumpLibeventTest;
  155. friend class ScheduleWorkTest;
  156. friend class Thread;
  157. friend class sequence_manager::internal::SequenceManagerImpl;
  158. friend class MessageLoopTaskRunnerTest;
  159. friend class web::WebTaskEnvironment;
  160. sequence_manager::internal::SequenceManagerImpl* current_;
  161. };
  162. #if !defined(OS_NACL)
  163. // UI extension of CurrentThread.
  164. class BASE_EXPORT CurrentUIThread : public CurrentThread {
  165. public:
  166. // Returns an interface for the CurrentUIThread of the current thread.
  167. // Asserts that IsSet().
  168. static CurrentUIThread Get();
  169. // Returns true if the current thread is running a CurrentUIThread.
  170. static bool IsSet();
  171. CurrentUIThread* operator->() { return this; }
  172. #if defined(USE_OZONE) && !defined(OS_FUCHSIA) && !defined(OS_WIN)
  173. static_assert(
  174. std::is_base_of<WatchableIOMessagePumpPosix, MessagePumpForUI>::value,
  175. "CurrentThreadForUI::WatchFileDescriptor is supported only"
  176. "by MessagePumpLibevent and MessagePumpGlib implementations.");
  177. bool WatchFileDescriptor(int fd,
  178. bool persistent,
  179. MessagePumpForUI::Mode mode,
  180. MessagePumpForUI::FdWatchController* controller,
  181. MessagePumpForUI::FdWatcher* delegate);
  182. #endif
  183. #if defined(OS_IOS)
  184. // Forwards to SequenceManager::Attach().
  185. // TODO(https://crbug.com/825327): Plumb the actual SequenceManager* to
  186. // callers and remove ability to access this method from
  187. // CurrentUIThread.
  188. void Attach();
  189. #endif
  190. #if defined(OS_ANDROID)
  191. // Forwards to MessagePumpForUI::Abort().
  192. // TODO(https://crbug.com/825327): Plumb the actual MessagePumpForUI* to
  193. // callers and remove ability to access this method from
  194. // CurrentUIThread.
  195. void Abort();
  196. #endif
  197. #if defined(OS_WIN)
  198. void AddMessagePumpObserver(MessagePumpForUI::Observer* observer);
  199. void RemoveMessagePumpObserver(MessagePumpForUI::Observer* observer);
  200. #endif
  201. private:
  202. explicit CurrentUIThread(
  203. sequence_manager::internal::SequenceManagerImpl* current)
  204. : CurrentThread(current) {}
  205. MessagePumpForUI* GetMessagePumpForUI() const;
  206. };
  207. #endif // !defined(OS_NACL)
  208. // ForIO extension of CurrentThread.
  209. class BASE_EXPORT CurrentIOThread : public CurrentThread {
  210. public:
  211. // Returns an interface for the CurrentIOThread of the current thread.
  212. // Asserts that IsSet().
  213. static CurrentIOThread Get();
  214. // Returns true if the current thread is running a CurrentIOThread.
  215. static bool IsSet();
  216. CurrentIOThread* operator->() { return this; }
  217. #if !defined(OS_NACL_SFI)
  218. #if defined(OS_WIN)
  219. // Please see MessagePumpWin for definitions of these methods.
  220. HRESULT RegisterIOHandler(HANDLE file, MessagePumpForIO::IOHandler* handler);
  221. bool RegisterJobObject(HANDLE job, MessagePumpForIO::IOHandler* handler);
  222. bool WaitForIOCompletion(DWORD timeout, MessagePumpForIO::IOHandler* filter);
  223. #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
  224. // Please see WatchableIOMessagePumpPosix for definition.
  225. // Prefer base::FileDescriptorWatcher for non-critical IO.
  226. bool WatchFileDescriptor(int fd,
  227. bool persistent,
  228. MessagePumpForIO::Mode mode,
  229. MessagePumpForIO::FdWatchController* controller,
  230. MessagePumpForIO::FdWatcher* delegate);
  231. #endif // defined(OS_WIN)
  232. #if defined(OS_MAC)
  233. bool WatchMachReceivePort(
  234. mach_port_t port,
  235. MessagePumpForIO::MachPortWatchController* controller,
  236. MessagePumpForIO::MachPortWatcher* delegate);
  237. #endif
  238. #if defined(OS_FUCHSIA)
  239. // Additional watch API for native platform resources.
  240. bool WatchZxHandle(zx_handle_t handle,
  241. bool persistent,
  242. zx_signals_t signals,
  243. MessagePumpForIO::ZxHandleWatchController* controller,
  244. MessagePumpForIO::ZxHandleWatcher* delegate);
  245. #endif // defined(OS_FUCHSIA)
  246. #endif // !defined(OS_NACL_SFI)
  247. private:
  248. explicit CurrentIOThread(
  249. sequence_manager::internal::SequenceManagerImpl* current)
  250. : CurrentThread(current) {}
  251. MessagePumpForIO* GetMessagePumpForIO() const;
  252. };
  253. } // namespace base
  254. #endif // BASE_TASK_CURRENT_THREAD_H_