123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392 |
- #ifndef BASE_TASK_TASK_TRAITS_H_
- #define BASE_TASK_TASK_TRAITS_H_
- #include <stdint.h>
- #include <iosfwd>
- #include <tuple>
- #include <type_traits>
- #include <utility>
- #include "base/base_export.h"
- #include "base/check_op.h"
- #include "base/task/task_traits_extension.h"
- #include "base/traits_bag.h"
- #include "build/build_config.h"
- #include "base/task/thread_pool.h"
- namespace base {
- class PostTaskAndroid;
- enum class TaskPriority : uint8_t {
-
- LOWEST = 0,
-
-
-
-
-
-
-
-
-
-
-
-
-
- BEST_EFFORT = LOWEST,
-
-
-
-
-
-
-
-
- USER_VISIBLE,
-
-
-
-
-
-
-
-
-
-
-
- USER_BLOCKING,
-
- HIGHEST = USER_BLOCKING
- };
- enum class TaskShutdownBehavior : uint8_t {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- CONTINUE_ON_SHUTDOWN,
-
-
-
-
-
-
-
-
-
- SKIP_ON_SHUTDOWN,
-
-
-
-
-
-
-
- BLOCK_SHUTDOWN,
- };
- enum class ThreadPolicy : uint8_t {
-
-
-
-
-
-
-
-
-
-
- PREFER_BACKGROUND,
-
- MUST_USE_FOREGROUND
- };
- struct MayBlock {};
- struct WithBaseSyncPrimitives {};
- class BASE_EXPORT TaskTraits {
- public:
-
- struct ValidTrait {
- ValidTrait(TaskPriority);
- ValidTrait(TaskShutdownBehavior);
- ValidTrait(ThreadPolicy);
- ValidTrait(MayBlock);
- ValidTrait(WithBaseSyncPrimitives);
- ValidTrait(ThreadPool);
- };
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- template <class... ArgTypes,
- class CheckArgumentsAreValid = std::enable_if_t<
- trait_helpers::AreValidTraits<ValidTrait, ArgTypes...>::value ||
- trait_helpers::AreValidTraitsForExtension<ArgTypes...>::value>>
- constexpr TaskTraits(ArgTypes... args)
- : extension_(trait_helpers::GetTaskTraitsExtension(
- trait_helpers::AreValidTraits<ValidTrait, ArgTypes...>{},
- args...)),
- priority_(
- trait_helpers::GetEnum<TaskPriority, TaskPriority::USER_BLOCKING>(
- args...)),
- shutdown_behavior_(
- static_cast<uint8_t>(
- trait_helpers::GetEnum<TaskShutdownBehavior,
- TaskShutdownBehavior::SKIP_ON_SHUTDOWN>(
- args...)) |
- (trait_helpers::HasTrait<TaskShutdownBehavior, ArgTypes...>()
- ? kIsExplicitFlag
- : 0)),
- thread_policy_(
- static_cast<uint8_t>(
- trait_helpers::GetEnum<ThreadPolicy,
- ThreadPolicy::PREFER_BACKGROUND>(
- args...)) |
- (trait_helpers::HasTrait<ThreadPolicy, ArgTypes...>()
- ? kIsExplicitFlag
- : 0)),
- may_block_(trait_helpers::HasTrait<MayBlock, ArgTypes...>()),
- with_base_sync_primitives_(
- trait_helpers::HasTrait<WithBaseSyncPrimitives, ArgTypes...>()),
- use_thread_pool_(trait_helpers::HasTrait<ThreadPool, ArgTypes...>()) {}
- constexpr TaskTraits(const TaskTraits& other) = default;
- TaskTraits& operator=(const TaskTraits& other) = default;
-
- bool operator==(const TaskTraits& other) const {
- static_assert(sizeof(TaskTraits) == 15,
- "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_;
- }
-
- void UpdatePriority(TaskPriority priority) { priority_ = priority; }
-
- constexpr TaskPriority priority() const { return priority_; }
-
- constexpr bool shutdown_behavior_set_explicitly() const {
- return shutdown_behavior_ & kIsExplicitFlag;
- }
-
- constexpr TaskShutdownBehavior shutdown_behavior() const {
- return static_cast<TaskShutdownBehavior>(shutdown_behavior_ &
- ~kIsExplicitFlag);
- }
-
- constexpr bool thread_policy_set_explicitly() const {
- return thread_policy_ & kIsExplicitFlag;
- }
-
- constexpr ThreadPolicy thread_policy() const {
- return static_cast<ThreadPolicy>(thread_policy_ & ~kIsExplicitFlag);
- }
-
- constexpr bool may_block() const { return may_block_; }
-
- constexpr bool with_base_sync_primitives() const {
- return with_base_sync_primitives_;
- }
-
- constexpr bool use_thread_pool() const { return use_thread_pool_; }
- uint8_t extension_id() const { return extension_.extension_id; }
-
-
- template <class TaskTraitsExtension>
- const TaskTraitsExtension GetExtension() const {
- DCHECK_EQ(TaskTraitsExtension::kExtensionId, extension_.extension_id);
- return TaskTraitsExtension::Parse(extension_);
- }
- private:
- friend PostTaskAndroid;
-
- TaskTraits(TaskPriority priority,
- bool may_block,
- bool use_thread_pool,
- TaskTraitsExtensionStorage extension)
- : extension_(extension),
- priority_(priority),
- shutdown_behavior_(
- static_cast<uint8_t>(TaskShutdownBehavior::SKIP_ON_SHUTDOWN)),
- 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");
-
-
- const bool has_extension =
- (extension_.extension_id !=
- TaskTraitsExtensionStorage::kInvalidExtensionId);
- DCHECK(use_thread_pool_ ^ has_extension)
- << "Traits must explicitly specify a destination (e.g. ThreadPool or a "
- "named thread like BrowserThread)";
- }
-
-
- static constexpr uint8_t kIsExplicitFlag = 0x80;
-
- TaskTraitsExtensionStorage extension_;
- TaskPriority priority_;
- uint8_t shutdown_behavior_;
- uint8_t thread_policy_;
- bool may_block_;
- bool with_base_sync_primitives_;
- bool use_thread_pool_;
- };
- BASE_EXPORT const char* TaskPriorityToString(TaskPriority task_priority);
- BASE_EXPORT const char* TaskShutdownBehaviorToString(
- TaskShutdownBehavior task_priority);
- BASE_EXPORT std::ostream& operator<<(std::ostream& os,
- const TaskPriority& shutdown_behavior);
- BASE_EXPORT std::ostream& operator<<(
- std::ostream& os,
- const TaskShutdownBehavior& shutdown_behavior);
- }
- #endif
|