-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
improve documentation on std::thread::sleep #54646
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @aidanhs (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
This is wrong. The function will not return early and the implementation reflects it. It may result in the backing call being called multiple times, though. |
@nagisa oh, so it's instead a guarantee that Rust provides. |
It seems like it would be less confusing for it to just say that it guarantees that the function will not return for at least the specified duration. |
It is under the platform-specific behaviour for unix because the function does not behave the same way as the underlying syscall would. On Windows, for example, the underlying syscall does not sporadically wake. I agree that it would make sense to move the guarantee of lack of spurious wakeups to the regular documentation. |
lgtm, but cc @rust-lang/docs for their sign-off. |
src/libstd/thread/mod.rs
Outdated
/// On Unix platforms this function will not return early due to a | ||
/// signal being received or a spurious wakeup. | ||
/// On Unix platforms this function may invoke multiple syscalls | ||
/// in case of a signal being received or a spurious wakeup. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On Unix platforms this function may invoke multiple syscalls in case of a signal being received or a spurious wakeup.
I don't quite understand why we're talking about system calls here. Can someone explain how it's relevant?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@frewsxcv The way how "sleep" is usually implemented is telling the OS to put a thread to sleep. This is done via a "syscall". "sleep" is so often implemented via syscall that some people may even assume it's the same. And in many languages this is basically the same.
Also, making more than one syscall could probably have some performance impact in very specific cases -- though I'm not entirely sure about that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the explanation! Why should the reader care how many system calls are invoked per platform? Just trying to figure out what the value-add is for this section.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@frewsxcv I guess the value-add is clarity.
Some people are just too used to the "old"/"usual" behavior. Even JVM, which poses itself as cross-platform, still makes only one underlying sleeping syscall, which results in the specific behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do you feel about this text?
> On Unix platforms, the [underlying `sleep` system call][syscall] can sporadically
> wake early. To ensure we sleep for at least the specified duration, this function
> may invoke that system call multiple times.
>
> [syscall]: http://man7.org/linux/man-pages/man3/sleep.3.html
I think this provides some more context to the reader why we're talking about system calls and why this is platform-specific. Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds interesting. Some remarks though:
-
the underlying syscall is
nanosleep
: http://man7.org/linux/man-pages/man2/nanosleep.2.html (note the number -- it's "2" for systemcalls. Number "3" is for "library calls" -- basically the C interface). -
the thread can as well wake up due to a signal received (as documented in the manual). That needs to be mentioned as well.
I'll return to this a bit later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another attempt:
> On Unix platforms, the [underlying `nanosleep` system call][syscall] can
> spuriously wake early or wake early from a signal interruption. To
> ensure the sleep occurs for at least the specified duration, this function
> may invoke that system call multiple times.
>
> [syscall]: http://man7.org/linux/man-pages/man2/nanosleep.2.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@frewsxcv but then again if I think more about the problem, it seems that the exact system call might be Linux-specific, whereas the start of paragraph says "Unix".
I've pushed an update taking the idea and wording of your proposal, please take a look.
The documentation should match what it says in the |
@Havvy wait, was that a review request? Can you please point out where exactly would you like a fix to be made? // I'm new to Rust-s documentation rules. |
The |
@Havvy thanks, I see. Updated. |
Any updates? All review comments are addressed I think. |
Thanks for being patient with this 🙏 Once we finish up that comment thread I'll approve this ✅ |
looks great, thanks for being patient! @bors r+ rollup |
📌 Commit 7a0fa95 has been approved by |
Thanks for all the feedback as well -- it was very helpful. |
improve documentation on std::thread::sleep
Rollup of 18 pull requests Successful merges: - #54646 (improve documentation on std::thread::sleep) - #54933 (Cleanup the rest of codegen_llvm) - #54964 (Run both lldb and gdb tests) - #55016 (Deduplicate some code and compile-time values around vtables) - #55031 (Improve verify_llvm_ir config option) - #55050 (doc std::fmt: the Python inspiration is already mentioned in precedin…) - #55077 (rustdoc: Use dyn keyword when rendering dynamic traits) - #55080 (Detect if access to localStorage is forbidden by the user's browser) - #55090 (regression test for move out of borrow via pattern) - #55102 (resolve: Do not skip extern prelude during speculative resolution) - #55104 (Add test for #34229) - #55111 ([Rustc Book] Explain --cfg's arguments) - #55122 (Cleanup mir/borrowck) - #55127 (Remove HybridBitSet::dummy) - #55128 (Fix LLVMRustInlineAsmVerify return type mismatch) - #55142 (miri: layout should not affect CTFE checks (outside of validation)) - #55151 (Cleanup nll) - #55161 ([librustdoc] Disable spellcheck for search field)
No description provided.