diff options
Diffstat (limited to 'chromium/base/task/task_traits.h')
-rw-r--r-- | chromium/base/task/task_traits.h | 38 |
1 files changed, 31 insertions, 7 deletions
diff --git a/chromium/base/task/task_traits.h b/chromium/base/task/task_traits.h index c1e52ed0f07..03abf17913f 100644 --- a/chromium/base/task/task_traits.h +++ b/chromium/base/task/task_traits.h @@ -185,6 +185,14 @@ struct WithBaseSyncPrimitives {}; // between tasks, see base::PostTask::CreateSequencedTaskRunner. struct ThreadPool {}; +// Tasks and task runners with this thread will run tasks on the virtual thread +// (sequence) they are posted/created from. Other traits may be specified +// alongside this one to refine properties for the associated tasks +// (e.g. base::TaskPriority or content::BrowserTaskType) as long as those traits +// are compatible with the current thread (e.g. cannot specify base::MayBlock() +// on a non-blocking thread or alter base::TaskShutdownBehavior). +struct CurrentThread {}; + // Describes metadata for a single task or a group of tasks. class BASE_EXPORT TaskTraits { public: @@ -196,6 +204,7 @@ class BASE_EXPORT TaskTraits { ValidTrait(MayBlock); ValidTrait(WithBaseSyncPrimitives); ValidTrait(ThreadPool); + ValidTrait(CurrentThread); }; // Invoking this constructor without arguments produces TaskTraits that are @@ -255,15 +264,22 @@ class BASE_EXPORT TaskTraits { may_block_(trait_helpers::HasTrait<MayBlock, ArgTypes...>()), with_base_sync_primitives_( trait_helpers::HasTrait<WithBaseSyncPrimitives, ArgTypes...>()), - use_thread_pool_(trait_helpers::HasTrait<ThreadPool, ArgTypes...>()) { + use_thread_pool_(trait_helpers::HasTrait<ThreadPool, ArgTypes...>()), + use_current_thread_( + trait_helpers::HasTrait<CurrentThread, ArgTypes...>()) { constexpr bool has_thread_pool = trait_helpers::HasTrait<ThreadPool, ArgTypes...>(); constexpr bool has_extension = !trait_helpers::AreValidTraits<ValidTrait, ArgTypes...>::value; + constexpr bool has_current_thread = + trait_helpers::HasTrait<CurrentThread, ArgTypes...>(); + static_assert( + !has_current_thread || !has_thread_pool, + "base::CurrentThread is mutually exclusive with base::ThreadPool"); static_assert( - has_thread_pool ^ has_extension, + has_thread_pool ^ has_extension || has_current_thread, "Traits must explicitly specify a destination (e.g. ThreadPool or a " - "named thread like BrowserThread)"); + "named thread like BrowserThread, or CurrentThread)"); } constexpr TaskTraits(const TaskTraits& other) = default; @@ -271,14 +287,15 @@ class BASE_EXPORT TaskTraits { // TODO(eseckler): Default the comparison operator once C++20 arrives. bool operator==(const TaskTraits& other) const { - static_assert(sizeof(TaskTraits) == 15, + static_assert(sizeof(TaskTraits) == 16, "Update comparison operator when TaskTraits change"); return extension_ == other.extension_ && priority_ == other.priority_ && shutdown_behavior_ == other.shutdown_behavior_ && thread_policy_ == other.thread_policy_ && may_block_ == other.may_block_ && with_base_sync_primitives_ == other.with_base_sync_primitives_ && - use_thread_pool_ == other.use_thread_pool_; + use_thread_pool_ == other.use_thread_pool_ && + use_current_thread_ == other.use_current_thread_; } // Sets the priority of tasks with these traits to |priority|. @@ -335,6 +352,10 @@ class BASE_EXPORT TaskTraits { // Returns true if tasks with these traits execute on the thread pool. constexpr bool use_thread_pool() const { return use_thread_pool_; } + // Returns true if tasks with these traits execute on the virtual thread + // (sequence) they are posted/created from. + constexpr bool use_current_thread() const { return use_current_thread_; } + uint8_t extension_id() const { return extension_.extension_id; } // Access the extension data by parsing it into the provided extension type. @@ -353,6 +374,7 @@ class BASE_EXPORT TaskTraits { TaskPriority priority, bool may_block, bool use_thread_pool, + bool use_current_thread, TaskTraitsExtensionStorage extension) : extension_(extension), priority_(static_cast<uint8_t>(priority) | @@ -362,8 +384,9 @@ class BASE_EXPORT TaskTraits { thread_policy_(static_cast<uint8_t>(ThreadPolicy::PREFER_BACKGROUND)), may_block_(may_block), with_base_sync_primitives_(false), - use_thread_pool_(use_thread_pool) { - static_assert(sizeof(TaskTraits) == 15, "Keep this constructor up to date"); + use_thread_pool_(use_thread_pool), + use_current_thread_(use_current_thread) { + static_assert(sizeof(TaskTraits) == 16, "Keep this constructor up to date"); } // This bit is set in |priority_|, |shutdown_behavior_| and |thread_policy_| @@ -378,6 +401,7 @@ class BASE_EXPORT TaskTraits { bool may_block_; bool with_base_sync_primitives_; bool use_thread_pool_; + bool use_current_thread_; }; // Returns string literals for the enums defined in this file. These methods |