123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- #ifndef BASE_TASK_CURRENT_THREAD_H_
- #define BASE_TASK_CURRENT_THREAD_H_
- #include <ostream>
- #include "base/base_export.h"
- #include "base/check.h"
- #include "base/memory/scoped_refptr.h"
- #include "base/message_loop/message_pump_for_io.h"
- #include "base/message_loop/message_pump_for_ui.h"
- #include "base/pending_task.h"
- #include "base/single_thread_task_runner.h"
- #include "base/task/task_observer.h"
- #include "build/build_config.h"
- namespace web {
- class WebTaskEnvironment;
- }
- namespace base {
- namespace sequence_manager {
- namespace internal {
- class SequenceManagerImpl;
- }
- }
- class BASE_EXPORT CurrentThread {
- public:
-
-
- CurrentThread(const CurrentThread& other) = default;
- CurrentThread(CurrentThread&& other) = default;
- CurrentThread& operator=(const CurrentThread& other) = default;
- bool operator==(const CurrentThread& other) const;
-
-
- static CurrentThread Get();
-
-
- static CurrentThread GetNull();
-
-
-
- static bool IsSet();
-
-
-
- CurrentThread* operator->() { return this; }
- explicit operator bool() const { return !!current_; }
-
-
-
-
-
-
-
-
-
-
-
- class BASE_EXPORT DestructionObserver {
- public:
-
-
- virtual void WillDestroyCurrentMessageLoop() = 0;
- protected:
- virtual ~DestructionObserver() = default;
- };
-
-
- void AddDestructionObserver(DestructionObserver* destruction_observer);
-
-
- void RemoveDestructionObserver(DestructionObserver* destruction_observer);
-
-
-
- void SetTaskRunner(scoped_refptr<SingleThreadTaskRunner> task_runner);
-
-
-
- void AddTaskObserver(TaskObserver* task_observer);
- void RemoveTaskObserver(TaskObserver* task_observer);
-
-
- void SetAddQueueTimeToTasks(bool enable);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- class BASE_EXPORT ScopedAllowApplicationTasksInNativeNestedLoop {
- public:
- ScopedAllowApplicationTasksInNativeNestedLoop();
- ~ScopedAllowApplicationTasksInNativeNestedLoop();
- private:
- sequence_manager::internal::SequenceManagerImpl* const sequence_manager_;
- const bool previous_state_;
- };
-
-
-
- using ScopedNestableTaskAllower =
- ScopedAllowApplicationTasksInNativeNestedLoop;
-
-
-
- bool NestableTasksAllowed() const;
-
- bool IsBoundToCurrentThread() const;
-
-
-
-
- bool IsIdleForTesting();
- protected:
- explicit CurrentThread(
- sequence_manager::internal::SequenceManagerImpl* sequence_manager)
- : current_(sequence_manager) {}
- static sequence_manager::internal::SequenceManagerImpl*
- GetCurrentSequenceManagerImpl();
- friend class MessagePumpLibeventTest;
- friend class ScheduleWorkTest;
- friend class Thread;
- friend class sequence_manager::internal::SequenceManagerImpl;
- friend class MessageLoopTaskRunnerTest;
- friend class web::WebTaskEnvironment;
- sequence_manager::internal::SequenceManagerImpl* current_;
- };
- #if !defined(OS_NACL)
- class BASE_EXPORT CurrentUIThread : public CurrentThread {
- public:
-
-
- static CurrentUIThread Get();
-
- static bool IsSet();
- CurrentUIThread* operator->() { return this; }
- #if defined(USE_OZONE) && !defined(OS_FUCHSIA) && !defined(OS_WIN)
- static_assert(
- std::is_base_of<WatchableIOMessagePumpPosix, MessagePumpForUI>::value,
- "CurrentThreadForUI::WatchFileDescriptor is supported only"
- "by MessagePumpLibevent and MessagePumpGlib implementations.");
- bool WatchFileDescriptor(int fd,
- bool persistent,
- MessagePumpForUI::Mode mode,
- MessagePumpForUI::FdWatchController* controller,
- MessagePumpForUI::FdWatcher* delegate);
- #endif
- #if defined(OS_IOS)
-
-
-
-
- void Attach();
- #endif
- #if defined(OS_ANDROID)
-
-
-
-
- void Abort();
- #endif
- #if defined(OS_WIN)
- void AddMessagePumpObserver(MessagePumpForUI::Observer* observer);
- void RemoveMessagePumpObserver(MessagePumpForUI::Observer* observer);
- #endif
- private:
- explicit CurrentUIThread(
- sequence_manager::internal::SequenceManagerImpl* current)
- : CurrentThread(current) {}
- MessagePumpForUI* GetMessagePumpForUI() const;
- };
- #endif
- class BASE_EXPORT CurrentIOThread : public CurrentThread {
- public:
-
-
- static CurrentIOThread Get();
-
- static bool IsSet();
- CurrentIOThread* operator->() { return this; }
- #if !defined(OS_NACL_SFI)
- #if defined(OS_WIN)
-
- HRESULT RegisterIOHandler(HANDLE file, MessagePumpForIO::IOHandler* handler);
- bool RegisterJobObject(HANDLE job, MessagePumpForIO::IOHandler* handler);
- bool WaitForIOCompletion(DWORD timeout, MessagePumpForIO::IOHandler* filter);
- #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
-
-
- bool WatchFileDescriptor(int fd,
- bool persistent,
- MessagePumpForIO::Mode mode,
- MessagePumpForIO::FdWatchController* controller,
- MessagePumpForIO::FdWatcher* delegate);
- #endif
- #if defined(OS_MAC)
- bool WatchMachReceivePort(
- mach_port_t port,
- MessagePumpForIO::MachPortWatchController* controller,
- MessagePumpForIO::MachPortWatcher* delegate);
- #endif
- #if defined(OS_FUCHSIA)
-
- bool WatchZxHandle(zx_handle_t handle,
- bool persistent,
- zx_signals_t signals,
- MessagePumpForIO::ZxHandleWatchController* controller,
- MessagePumpForIO::ZxHandleWatcher* delegate);
- #endif
- #endif
- private:
- explicit CurrentIOThread(
- sequence_manager::internal::SequenceManagerImpl* current)
- : CurrentThread(current) {}
- MessagePumpForIO* GetMessagePumpForIO() const;
- };
- }
- #endif
|