Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task/future: support spawning locally #24

Merged
merged 7 commits into from
Jan 2, 2020

Conversation

BusyJay
Copy link
Member

@BusyJay BusyJay commented Dec 31, 2019

It uses a thread local variable to detect if it's in thread pool.
Spawning locally can utilize maximum cache locality.

@BusyJay BusyJay added the enhancement New feature or request label Dec 31, 2019
It uses a thread local variable to detect if it's in thread pool.
Spawning locally can utilize maximum cache locality.

Signed-off-by: Jay Lee <busyjaylee@gmail.com>
Signed-off-by: Jay Lee <busyjaylee@gmail.com>
@@ -147,10 +149,57 @@ unsafe fn task_cell(task: *const Task) -> TaskCell {
#[inline]
unsafe fn clone_task(task: *const Task) -> TaskCell {
let task_cell = task_cell(task);
let extras = { &mut *task_cell.0.extras.get() };
// `remote` is none only when it has been constructed but never been polled.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the first poll, remote also seems None.

Signed-off-by: Jay Lee <busyjaylee@gmail.com>
Signed-off-by: Jay Lee <busyjaylee@gmail.com>
if ptr.get().is_null() {
// It's out of polling process, has to be spawn to global queue.
// It needs to clone to make it safe as it's unclear whether `self`
// is still used inside method `spawn` after `TaskCell` is dropped.
Copy link
Contributor

@sticnarf sticnarf Dec 31, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly speaking, there is still some risk because Waker is accidentally Sync. rust-lang/rust#66481
It means the user might be able to move the reference to other threads (for example, using crossbeam::scoped`) and break our assumption.

However, I think it can be ignored. Maybe we can give some documentation and use expect to give some messages?

Signed-off-by: Jay Lee <busyjaylee@gmail.com>
@@ -85,6 +88,23 @@ unsafe fn waker(task: *const Task) -> Waker {
#[inline]
unsafe fn clone_raw(this: *const ()) -> RawWaker {
let task_cell = clone_task(this as *const Task);
let extras = { &mut *task_cell.0.extras.get() };
Copy link
Contributor

@sticnarf sticnarf Dec 31, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use an immutable reference here and get the mutable reference only at L98 to make the program sound.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it unsound?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two wakers in different threads concurrently, then there can be two multable references to the extras at the same time.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you mean clone_raw can be executed concurrently due to L100, then it's unsound either to move it to L98. Lock is required.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, you're right. Executing L98 and L91 concurrently is unsound.

.remote
.as_ref()
.expect("waker should not be moved to other threads by reference")
.spawn(TaskCell(task.clone().into_owned()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I still don't understand why we need a clone here...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TaskCell can be dropped inside method spawn, which can make self invalid.

@@ -427,29 +483,62 @@ mod tests {
assert_eq!(res_rx.recv().unwrap(), 4);
}

struct ForwardWaker {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's identical to WakeLater?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly. Didn't notice it before.

Signed-off-by: Jay Lee <busyjaylee@gmail.com>
Signed-off-by: Jay Lee <busyjaylee@gmail.com>
@@ -257,6 +240,15 @@ impl crate::pool::Runner for Runner {
task.status.store(COMPLETED, SeqCst);
return true;
}
let extras = { &mut *task.extras.get() };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be executed before poll. Otherwise, before the first poll finished, waking a waker moved to other threads will panic.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use mut_extras here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it won't. Waking a polling future will mark it NOTIFIED instead of actually waking it up.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use mut_extras here

They are not the same extras. Besides, task_cell is moved.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. I got it.

Copy link
Contributor

@sticnarf sticnarf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@BusyJay BusyJay merged commit 7f5b56d into tikv:master Jan 2, 2020
@BusyJay BusyJay deleted the local-spawn-future branch January 2, 2020 08:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants