// Returns true if the current thread is a thread on which a task // may be run, and false if no task will be run on the current // thread. // // It is valid for an implementation to always return true, or in // general to use 'true' as a default value. virtual bool RunsTasksOnCurrentThread() const = 0;
// Returns true iff a task different than the current task // that is posted to this TaskRunner can't run at the same // time as the current task. // // In particular, returns true if this is a // SequencedTaskRunner to which the current task was posted // and always returns false if this is a TaskRunner that // can run its tasks in parallel. virtual bool IsSequencedWithCurrentTask() const = 0;
However, if you call RunsTasksOnCurrentThread() on a SequencedTaskRunner returned by SequencedWorkerPool, it won't tell you if "the current thread is a thread on which a task may be run"! Rather than that, it will tell you if the current task is part of the sequence associated with the SequencedTaskRunner.
I believe that we should change TaskRunner::RunsTasksOnCurrentThread to:// Returns true iff a task different than the current task // that is posted to this TaskRunner can't run at the same // time as the current task.
Pros:
- The new name and description match the existing behavior of all TaskRunners returned by MessageLoop and SequencedWorkerPool.
- Tasks posted to base/task_scheduler may run on any base/task_scheduler thread. It wouldn't be very helpful to have a method to know whether a task runs in base/task_scheduler. It's more helpful to have a method to know whether the task is part of a given sequence.
- IsSequencedWithCurrentTask() make it clear that it can be used to DCHECK() that the current task runs as part of a sequence.
- IsSequencedWithCurrentTask() make it clear that it can be used to do this: (the previous description said that it is valid for RunsTasksOnCurrentThread to always return true):
void DoSomething() {if (task_runner->IsSequencedWithCurrentTask())// This doesn't run at the same time as a task posted to |task_runner|.task_runner->PostTask(FROM_HERE, Bind(&DoSomething));}Cons:
- The new name and description change the existing semantic. For example, IsSequencedWithCurrentTask() will always return false for a TaskRunner returned by base::WorkerPool.
What do you think?
On Tue, Sep 20, 2016 at 1:24 PM, François Doray <fdo...@chromium.org> wrote:However, if you call RunsTasksOnCurrentThread() on a SequencedTaskRunner returned by SequencedWorkerPool, it won't tell you if "the current thread is a thread on which a task may be run"! Rather than that, it will tell you if the current task is part of the sequence associated with the SequencedTaskRunner.But it does, in your example, meet the API contract. The current thread is a thread in which the task may be run. It doesn't say will, just may.
I believe that we should change TaskRunner::RunsTasksOnCurrentThread to:// Returns true iff a task different than the current task // that is posted to this TaskRunner can't run at the same // time as the current task.For what it's worth, this reads very difficult for me. I'm not sure if others find it as difficult to parse as I do.
In every attempt I've made to reword this, it seems to me that your attempt is to express a different semantic than what's currently being expressed.For example,// Returns true if tasks posted to this TaskRunner will not be executed until the current task completes.However, as that's not the semantics being expressed by RunsTasksOnCurrentThread, I don't believe you're simply expressing an API change.
// Returns true iff any task posted to this TaskRunner (in the past // or in the future) which isn't the current task can't run at the // same time as the current task. // // In particular: // - Returns true if this is a SequencedTaskRunner to which the // current task was posted. // - Returns true if this is a SequencedTaskRunner bound to the // same sequence as the SequencedTaskRunner to which the current // task was posted. // - Returns true if this is a SingleThreadTaskRunner bound to // the current thread. // - Returns false if this is a TaskRunner that can run its tasks // in parallel. virtual bool IsSequencedWithCurrentTask() const = 0;
Pros:
- The new name and description match the existing behavior of all TaskRunners returned by MessageLoop and SequencedWorkerPool.
As explained above, I don't believe it does.
- Tasks posted to base/task_scheduler may run on any base/task_scheduler thread. It wouldn't be very helpful to have a method to know whether a task runs in base/task_scheduler. It's more helpful to have a method to know whether the task is part of a given sequence.
I disagree. There are a number of cases in //net where we've concretely needed to know whether the target (Sequenced)TaskRunner is executing on the same thread, and if so, can avoid additional thread-hopping setup and safely execute code directly.
--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev...@chromium.org.
I would support improving the naming here, mainly because I find TaskRunner::RunsTasksOnCurrentThread() easy to confuse with SingleThreadTaskRunner::BelongsToCurrentThread().(ob. bikeshed: "BelongsToCurrentSequence"?)
On Thu, Sep 22, 2016 at 11:07 AM Sami Kyostila <skyo...@chromium.org> wrote:I would support improving the naming here, mainly because I find TaskRunner::RunsTasksOnCurrentThread() easy to confuse with SingleThreadTaskRunner::BelongsToCurrentThread().(ob. bikeshed: "BelongsToCurrentSequence"?)I like "BelongsToCurrentSequence".
// Returns true iff tasks posted to this TaskRunner are sequenced // with this call. // // In particular: // - Returns true if this is a SequencedTaskRunner to which the // current task was posted. // - Returns true if this is a SequencedTaskRunner bound to the // same sequence as the SequencedTaskRunner to which the current // task was posted. // - Returns true if this is a SingleThreadTaskRunner bound to // the current thread. // - Returns false if this is a TaskRunner that can run its tasks // in parallel. virtual bool RunsTasksInCurrentSequence() const = 0;
This SGTM (but of course it does ;).Given that (a) there's a comment on task_runner.h saying "TaskRunner does not guarantee the order in which posted tasks are run, whether tasks overlap, or whether they're run on a particular thread." and (b) TaskRunner already has virtual methods with default impls (e.g., OnDestruct()), I think that you could sharpen this by giving TaskRunner a default impl that returns false. It would then be clear that a subclass overriding RunsTasksInCurrentSequence() implies that that subclass is providing *some* kind of sequencing.