Commit 973df87d authored by Karolina Soltys's avatar Karolina Soltys Committed by Commit Bot

[scheduler] Adding base::CurrentThread trait.

I'm adding a trait specifying that the task should run on the current
thread. It will be one of the 3 possible destinations (alongside
ThreadPool and BrowserThread), specifying which will be made mandatory
in a future CL.

A future CL will add the desired functionality backing this trait.

Bug: 968047
Change-Id: I16f244911b9e25da354148b034bd766c988dec11
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1642559Reviewed-by: default avatarRichard Coles <torne@chromium.org>
Reviewed-by: default avatarKarolina Soltys <ksolt@chromium.org>
Reviewed-by: default avatarAlex Clarke <alexclarke@chromium.org>
Commit-Queue: Karolina Soltys <ksolt@chromium.org>
Cr-Commit-Position: refs/heads/master@{#666288}
parent c8101e8d
...@@ -86,7 +86,8 @@ public class PostTask { ...@@ -86,7 +86,8 @@ public class PostTask {
getTaskExecutorForTraits(taskTraits).postDelayedTask(taskTraits, task, delay); getTaskExecutorForTraits(taskTraits).postDelayedTask(taskTraits, task, delay);
} else { } else {
nativePostDelayedTask(taskTraits.mPrioritySetExplicitly, taskTraits.mPriority, nativePostDelayedTask(taskTraits.mPrioritySetExplicitly, taskTraits.mPriority,
taskTraits.mMayBlock, taskTraits.mUseThreadPool, taskTraits.mExtensionId, taskTraits.mMayBlock, taskTraits.mUseThreadPool,
taskTraits.mUseCurrentThread, taskTraits.mExtensionId,
taskTraits.mExtensionData, task, delay); taskTraits.mExtensionData, task, delay);
} }
} }
...@@ -256,6 +257,6 @@ public class PostTask { ...@@ -256,6 +257,6 @@ public class PostTask {
} }
private static native void nativePostDelayedTask(boolean prioritySetExplicitly, int priority, private static native void nativePostDelayedTask(boolean prioritySetExplicitly, int priority,
boolean mayBlock, boolean useThreadPool, byte extensionId, byte[] extensionData, boolean mayBlock, boolean useThreadPool, boolean useCurrentThread, byte extensionId,
Runnable task, long delay); byte[] extensionData, Runnable task, long delay);
} }
...@@ -156,10 +156,11 @@ public class TaskRunnerImpl implements TaskRunner { ...@@ -156,10 +156,11 @@ public class TaskRunnerImpl implements TaskRunner {
@GuardedBy("mLock") @GuardedBy("mLock")
protected void initNativeTaskRunnerInternal() { protected void initNativeTaskRunnerInternal() {
if (mNativeTaskRunnerAndroid == 0) { if (mNativeTaskRunnerAndroid == 0) {
mNativeTaskRunnerAndroid = nativeInit(mTaskRunnerType, mNativeTaskRunnerAndroid =
mTaskTraits.mPrioritySetExplicitly, mTaskTraits.mPriority, nativeInit(mTaskRunnerType, mTaskTraits.mPrioritySetExplicitly,
mTaskTraits.mMayBlock, mTaskTraits.mUseThreadPool, mTaskTraits.mExtensionId, mTaskTraits.mPriority, mTaskTraits.mMayBlock,
mTaskTraits.mExtensionData); mTaskTraits.mUseThreadPool, mTaskTraits.mUseCurrentThread,
mTaskTraits.mExtensionId, mTaskTraits.mExtensionData);
} }
} }
...@@ -185,7 +186,7 @@ public class TaskRunnerImpl implements TaskRunner { ...@@ -185,7 +186,7 @@ public class TaskRunnerImpl implements TaskRunner {
// NB due to Proguard obfuscation it's easiest to pass the traits via arguments. // NB due to Proguard obfuscation it's easiest to pass the traits via arguments.
private native long nativeInit(@TaskRunnerType int taskRunnerType, private native long nativeInit(@TaskRunnerType int taskRunnerType,
boolean prioritySetExplicitly, int priority, boolean mayBlock, boolean useThreadPool, boolean prioritySetExplicitly, int priority, boolean mayBlock, boolean useThreadPool,
byte extensionId, byte[] extensionData); boolean useCurrentThread, byte extensionId, byte[] extensionData);
private native void nativeDestroy(long nativeTaskRunnerAndroid); private native void nativeDestroy(long nativeTaskRunnerAndroid);
private native void nativePostDelayedTask( private native void nativePostDelayedTask(
long nativeTaskRunnerAndroid, Runnable task, long delay); long nativeTaskRunnerAndroid, Runnable task, long delay);
......
...@@ -76,12 +76,22 @@ public class TaskTraits { ...@@ -76,12 +76,22 @@ public class TaskTraits {
public static final TaskTraits THREAD_POOL_BEST_EFFORT = public static final TaskTraits THREAD_POOL_BEST_EFFORT =
THREAD_POOL.taskPriority(TaskPriority.BEST_EFFORT); THREAD_POOL.taskPriority(TaskPriority.BEST_EFFORT);
// For tasks that should run on the current thread.
public static final TaskTraits CURRENT_THREAD = new TaskTraits().currentThread();
public static final TaskTraits CURRENT_THREAD_USER_BLOCKING =
CURRENT_THREAD.taskPriority(TaskPriority.USER_BLOCKING);
public static final TaskTraits CURRENT_THREAD_USER_VISIBLE =
CURRENT_THREAD.taskPriority(TaskPriority.USER_VISIBLE);
public static final TaskTraits CURRENT_THREAD_BEST_EFFORT =
CURRENT_THREAD.taskPriority(TaskPriority.BEST_EFFORT);
// For convenience of the JNI code, we use primitive types only. // For convenience of the JNI code, we use primitive types only.
// Note shutdown behavior is not supported on android. // Note shutdown behavior is not supported on android.
boolean mPrioritySetExplicitly; boolean mPrioritySetExplicitly;
int mPriority; int mPriority;
boolean mMayBlock; boolean mMayBlock;
boolean mUseThreadPool; boolean mUseThreadPool;
boolean mUseCurrentThread;
byte mExtensionId; byte mExtensionId;
byte mExtensionData[]; byte mExtensionData[];
boolean mIsChoreographerFrame; boolean mIsChoreographerFrame;
...@@ -96,6 +106,7 @@ public class TaskTraits { ...@@ -96,6 +106,7 @@ public class TaskTraits {
mPriority = other.mPriority; mPriority = other.mPriority;
mMayBlock = other.mMayBlock; mMayBlock = other.mMayBlock;
mUseThreadPool = other.mUseThreadPool; mUseThreadPool = other.mUseThreadPool;
mUseCurrentThread = other.mUseCurrentThread;
mExtensionId = other.mExtensionId; mExtensionId = other.mExtensionId;
mExtensionData = other.mExtensionData; mExtensionData = other.mExtensionData;
} }
...@@ -126,6 +137,12 @@ public class TaskTraits { ...@@ -126,6 +137,12 @@ public class TaskTraits {
return taskTraits; return taskTraits;
} }
public TaskTraits currentThread() {
TaskTraits taskTraits = new TaskTraits(this);
taskTraits.mUseCurrentThread = true;
return taskTraits;
}
/** /**
* @return true if this task is using some TaskTraits extension. * @return true if this task is using some TaskTraits extension.
*/ */
...@@ -180,6 +197,7 @@ public class TaskTraits { ...@@ -180,6 +197,7 @@ public class TaskTraits {
hash = 37 * hash + mPriority; hash = 37 * hash + mPriority;
hash = 37 * hash + (mMayBlock ? 0 : 1); hash = 37 * hash + (mMayBlock ? 0 : 1);
hash = 37 * hash + (mUseThreadPool ? 0 : 1); hash = 37 * hash + (mUseThreadPool ? 0 : 1);
hash = 37 * hash + (mUseCurrentThread ? 0 : 1);
hash = 37 * hash + (int) mExtensionId; hash = 37 * hash + (int) mExtensionId;
hash = 37 * hash + Arrays.hashCode(mExtensionData); hash = 37 * hash + Arrays.hashCode(mExtensionData);
hash = 37 * hash + (mIsChoreographerFrame ? 0 : 1); hash = 37 * hash + (mIsChoreographerFrame ? 0 : 1);
......
...@@ -49,11 +49,12 @@ TaskTraits PostTaskAndroid::CreateTaskTraits( ...@@ -49,11 +49,12 @@ TaskTraits PostTaskAndroid::CreateTaskTraits(
jint priority, jint priority,
jboolean may_block, jboolean may_block,
jboolean use_thread_pool, jboolean use_thread_pool,
jboolean use_current_thread,
jbyte extension_id, jbyte extension_id,
const base::android::JavaParamRef<jbyteArray>& extension_data) { const base::android::JavaParamRef<jbyteArray>& extension_data) {
return TaskTraits(priority_set_explicitly, return TaskTraits(priority_set_explicitly,
static_cast<TaskPriority>(priority), may_block, static_cast<TaskPriority>(priority), may_block,
use_thread_pool, use_thread_pool, use_current_thread,
TaskTraitsExtensionStorage( TaskTraitsExtensionStorage(
extension_id, GetExtensionData(env, extension_data))); extension_id, GetExtensionData(env, extension_data)));
} }
...@@ -64,6 +65,7 @@ void JNI_PostTask_PostDelayedTask( ...@@ -64,6 +65,7 @@ void JNI_PostTask_PostDelayedTask(
jint priority, jint priority,
jboolean may_block, jboolean may_block,
jboolean use_thread_pool, jboolean use_thread_pool,
jboolean use_current_thread,
jbyte extension_id, jbyte extension_id,
const base::android::JavaParamRef<jbyteArray>& extension_data, const base::android::JavaParamRef<jbyteArray>& extension_data,
const base::android::JavaParamRef<jobject>& task, const base::android::JavaParamRef<jobject>& task,
...@@ -72,9 +74,9 @@ void JNI_PostTask_PostDelayedTask( ...@@ -72,9 +74,9 @@ void JNI_PostTask_PostDelayedTask(
// BindOnce because JNIEnv is thread specific. // BindOnce because JNIEnv is thread specific.
PostDelayedTaskWithTraits( PostDelayedTaskWithTraits(
FROM_HERE, FROM_HERE,
PostTaskAndroid::CreateTaskTraits(env, priority_set_explicitly, priority, PostTaskAndroid::CreateTaskTraits(
may_block, use_thread_pool, env, priority_set_explicitly, priority, may_block, use_thread_pool,
extension_id, extension_data), use_current_thread, extension_id, extension_data),
BindOnce(&PostTaskAndroid::RunJavaTask, BindOnce(&PostTaskAndroid::RunJavaTask,
base::android::ScopedJavaGlobalRef<jobject>(task)), base::android::ScopedJavaGlobalRef<jobject>(task)),
TimeDelta::FromMilliseconds(delay)); TimeDelta::FromMilliseconds(delay));
......
...@@ -28,6 +28,7 @@ class BASE_EXPORT PostTaskAndroid { ...@@ -28,6 +28,7 @@ class BASE_EXPORT PostTaskAndroid {
jint priority, jint priority,
jboolean may_block, jboolean may_block,
jboolean use_thread_pool, jboolean use_thread_pool,
jboolean use_current_thread,
jbyte extension_id, jbyte extension_id,
const base::android::JavaParamRef<jbyteArray>& extension_data); const base::android::JavaParamRef<jbyteArray>& extension_data);
......
...@@ -21,11 +21,12 @@ jlong JNI_TaskRunnerImpl_Init( ...@@ -21,11 +21,12 @@ jlong JNI_TaskRunnerImpl_Init(
jint priority, jint priority,
jboolean may_block, jboolean may_block,
jboolean thread_pool, jboolean thread_pool,
jboolean current_thread,
jbyte extension_id, jbyte extension_id,
const base::android::JavaParamRef<jbyteArray>& extension_data) { const base::android::JavaParamRef<jbyteArray>& extension_data) {
TaskTraits task_traits = PostTaskAndroid::CreateTaskTraits( TaskTraits task_traits = PostTaskAndroid::CreateTaskTraits(
env, priority_set_explicitly, priority, may_block, thread_pool, env, priority_set_explicitly, priority, may_block, thread_pool,
extension_id, extension_data); current_thread, extension_id, extension_data);
scoped_refptr<TaskRunner> task_runner; scoped_refptr<TaskRunner> task_runner;
switch (static_cast<TaskRunnerType>(task_runner_type)) { switch (static_cast<TaskRunnerType>(task_runner_type)) {
case TaskRunnerType::BASE: case TaskRunnerType::BASE:
......
...@@ -182,6 +182,10 @@ struct WithBaseSyncPrimitives {}; ...@@ -182,6 +182,10 @@ struct WithBaseSyncPrimitives {};
// between tasks, see base::PostTask::CreateSequencedTaskRunner. // between tasks, see base::PostTask::CreateSequencedTaskRunner.
struct ThreadPool {}; struct ThreadPool {};
// Tasks and task runners with this trait will run on the current virtual thread
// (sequence).
struct CurrentThread {};
// Describes metadata for a single task or a group of tasks. // Describes metadata for a single task or a group of tasks.
class BASE_EXPORT TaskTraits { class BASE_EXPORT TaskTraits {
public: public:
...@@ -193,6 +197,7 @@ class BASE_EXPORT TaskTraits { ...@@ -193,6 +197,7 @@ class BASE_EXPORT TaskTraits {
ValidTrait(MayBlock); ValidTrait(MayBlock);
ValidTrait(WithBaseSyncPrimitives); ValidTrait(WithBaseSyncPrimitives);
ValidTrait(ThreadPool); ValidTrait(ThreadPool);
ValidTrait(CurrentThread);
}; };
// Invoking this constructor without arguments produces TaskTraits that are // Invoking this constructor without arguments produces TaskTraits that are
...@@ -248,21 +253,23 @@ class BASE_EXPORT TaskTraits { ...@@ -248,21 +253,23 @@ class BASE_EXPORT TaskTraits {
may_block_(trait_helpers::HasTrait<MayBlock>(args...)), may_block_(trait_helpers::HasTrait<MayBlock>(args...)),
with_base_sync_primitives_( with_base_sync_primitives_(
trait_helpers::HasTrait<WithBaseSyncPrimitives>(args...)), trait_helpers::HasTrait<WithBaseSyncPrimitives>(args...)),
use_thread_pool_(trait_helpers::HasTrait<ThreadPool>(args...)) {} use_thread_pool_(trait_helpers::HasTrait<ThreadPool>(args...)),
use_current_thread_(trait_helpers::HasTrait<CurrentThread>(args...)) {}
constexpr TaskTraits(const TaskTraits& other) = default; constexpr TaskTraits(const TaskTraits& other) = default;
TaskTraits& operator=(const TaskTraits& other) = default; TaskTraits& operator=(const TaskTraits& other) = default;
// TODO(eseckler): Default the comparison operator once C++20 arrives. // TODO(eseckler): Default the comparison operator once C++20 arrives.
bool operator==(const TaskTraits& other) const { bool operator==(const TaskTraits& other) const {
static_assert(sizeof(TaskTraits) == 15, static_assert(sizeof(TaskTraits) == 16,
"Update comparison operator when TaskTraits change"); "Update comparison operator when TaskTraits change");
return extension_ == other.extension_ && priority_ == other.priority_ && return extension_ == other.extension_ && priority_ == other.priority_ &&
shutdown_behavior_ == other.shutdown_behavior_ && shutdown_behavior_ == other.shutdown_behavior_ &&
thread_policy_ == other.thread_policy_ && thread_policy_ == other.thread_policy_ &&
may_block_ == other.may_block_ && may_block_ == other.may_block_ &&
with_base_sync_primitives_ == other.with_base_sync_primitives_ && 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|. // Sets the priority of tasks with these traits to |priority|.
...@@ -319,6 +326,9 @@ class BASE_EXPORT TaskTraits { ...@@ -319,6 +326,9 @@ class BASE_EXPORT TaskTraits {
// Returns true if tasks with these traits execute on the thread pool. // Returns true if tasks with these traits execute on the thread pool.
constexpr bool use_thread_pool() const { return use_thread_pool_; } constexpr bool use_thread_pool() const { return use_thread_pool_; }
// Returns true if tasks with these traits execute on the current thread.
constexpr bool use_current_thread() const { return use_current_thread_; }
uint8_t extension_id() const { return extension_.extension_id; } uint8_t extension_id() const { return extension_.extension_id; }
// Access the extension data by parsing it into the provided extension type. // Access the extension data by parsing it into the provided extension type.
...@@ -337,6 +347,7 @@ class BASE_EXPORT TaskTraits { ...@@ -337,6 +347,7 @@ class BASE_EXPORT TaskTraits {
TaskPriority priority, TaskPriority priority,
bool may_block, bool may_block,
bool use_thread_pool, bool use_thread_pool,
bool use_current_thread,
TaskTraitsExtensionStorage extension) TaskTraitsExtensionStorage extension)
: extension_(extension), : extension_(extension),
priority_(static_cast<uint8_t>(priority) | priority_(static_cast<uint8_t>(priority) |
...@@ -346,8 +357,9 @@ class BASE_EXPORT TaskTraits { ...@@ -346,8 +357,9 @@ class BASE_EXPORT TaskTraits {
thread_policy_(static_cast<uint8_t>(ThreadPolicy::PREFER_BACKGROUND)), thread_policy_(static_cast<uint8_t>(ThreadPolicy::PREFER_BACKGROUND)),
may_block_(may_block), may_block_(may_block),
with_base_sync_primitives_(false), with_base_sync_primitives_(false),
use_thread_pool_(use_thread_pool) { use_thread_pool_(use_thread_pool),
static_assert(sizeof(TaskTraits) == 15, "Keep this constructor up to date"); 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_| // This bit is set in |priority_|, |shutdown_behavior_| and |thread_policy_|
...@@ -362,6 +374,7 @@ class BASE_EXPORT TaskTraits { ...@@ -362,6 +374,7 @@ class BASE_EXPORT TaskTraits {
bool may_block_; bool may_block_;
bool with_base_sync_primitives_; bool with_base_sync_primitives_;
bool use_thread_pool_; bool use_thread_pool_;
bool use_current_thread_;
}; };
// Returns string literals for the enums defined in this file. These methods // Returns string literals for the enums defined in this file. These methods
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment