Skip to content

Conversation

@ghost
Copy link

@ghost ghost commented Jun 29, 2018

When unparking a worker thread, we do the following steps:

  1. Try transitioning IDLE -> NOTIFY.
  2. Lock the mutex.
  3. Try transitioning SLEEP -> NOTIFY and wakeup on success.

The problem occurs if the thread transitions from SLEEP to IDLE between steps 1 and 2. In that case, step 3 will fail to transition and simply return from unpark.

This PR changes the step 3 so that we always transition to NOTIFY, no matter what the previous state was.

The park/unpark mechanism in std::thread works somewhat similarly. In step 3, it attempts to CAS from SLEEP to NOTIFY, but jumps back to step 1 if the previous state was IDLE. See here. I've decided to unconditionally move to state NOTIFY instead - the end result should be the same.

In addition to that, I did a bunch of random improvements to the documentation.

/// use std::time::Duration;
///
/// # pub fn main() {
/// // Create a thread pool with default configuration values
Copy link
Author

Choose a reason for hiding this comment

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

I found this comment confusing. It seems to imply that the example shows how the builder is configured by default. However, this comment applies only to Builder::new(), while the chained methods do custom configuration (which is different from default).

Perhaps a different sentence would work better, but due to lack of ideas I've decided to simply remove it. :)

Copy link
Member

Choose a reason for hiding this comment

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

👍

Copy link
Member

@carllerche carllerche left a comment

Choose a reason for hiding this comment

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

Good eye! That is definitely an issue.

/// use std::time::Duration;
///
/// # pub fn main() {
/// // Create a thread pool with default configuration values
Copy link
Member

Choose a reason for hiding this comment

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

👍

@carllerche carllerche merged commit 7fb579c into tokio-rs:master Jul 3, 2018
@ghost ghost deleted the fix-wakeup-race branch July 3, 2018 23:32
@ghost ghost mentioned this pull request Jul 28, 2018
This was referenced Aug 6, 2018
@ghost ghost mentioned this pull request Sep 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant