Guiding Use of ssleep over msleep for Whole-Second Delays
cover Guiding Use of ssleep over msleep for Whole-Second Delays
This change particularly targets replacing msleep() with ssleep() for delays that are clear multiples of 1000ms, enhancing code semantics and readability.
I'm contemplating a kernel-wide update to conform to this practice,
pending community feedback. While a sed script
(like '/\bmsleep\s*\(\s*([0-9]+000)\s*\);/{s/\bmsleep\s*\(\s*([0-9]+)000\s*\);/ssleep(\1);/g}') could automate this, I'm cautious of potential implications and seek
your thoughts on both the patch and the idea of a broad codebase update.
Li Chen (1):
checkpatch: Add warning for msleep with durations suitable for ssleep
scripts/checkpatch.pl | 10 ++++++++++ 1 file changed, 10 insertions(+)
Signature · 1 line
2.44.0
RFC PATCH 1/1 checkpatch: Add warning for msleep with durations suitable for ssleep
From: Li Chen <[email protected]>
This patch enhances checkpatch.pl by introducing a warning for instances where msleep() is used with durations that are multiples of 1000 milliseconds. Such durations are more semantically appropriate for ssleep().
The goal is to encourage developers to use the most fitting sleep function, improving code readability.
Warn when msleep(x000); can be replaced with ssleep(x);
--- scripts/checkpatch.pl | 10 ++++++++++ 1 file changed, 10 insertions(+)
scripts/checkpatch.pl +10 -0
@@ -6599,6 +6599,16 @@ sub process { } } +# warn about msleep() calls with durations that should use ssleep() +if ($line =~ /\bmsleep\s*\((\d+)\);/) { + my $ms_duration = $1; + if ($ms_duration >= 1000 && ($ms_duration % 1000) == 0) { + my $ss_duration = $ms_duration / 1000; + WARN("SSLEEP", + "Prefer ssleep($ss_duration) over msleep($ms_duration);\n" . $herecurr); + } +} + # check for comparisons of jiffies if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) { WARN("JIFFIES_COMPARISON",
Signature · 1 line
2.44.0
Joe Perches
maintainerRe: [RFC PATCH 1/1] checkpatch: Add warning for msleep with durations suitable for ssleep
Quoted 1 line · Li Chen
From: Li Chen <[email protected]>
[]
Quoted 1 line
Warn when msleep(x000); can be replaced with ssleep(x);
While I don't really see the point as msleep is trivial to read and ssleep is just msleep * 1000 and there are already 3:1 more msleep(n*1000) uses than there are ssleep(n)
$ git grep -P '\bmsleep\s*\(\s*\d+000\s*\)' | wc -l 267 $ git grep -P '\bssleep\s*\(\s*\d+\s*\)' | wc -l 89
And about the patch itself:
scripts/checkpatch.pl
Quoted 1 line
scripts/checkpatch.pl +0
-0
[]
scripts/checkpatch.pl sub process { · line 6603
Quoted 6 lines
scripts/checkpatch.pl +2
-0@@ -6599,6 +6599,16 @@ sub process { } } +# warn about msleep() calls with durations that should use ssleep() +if ($line =~ /\bmsleep\s*\((\d+)\);/) {
This indentation is incorrect.
And this would be more sensible using
if ($line =~ /\bmsleep\s*\(\s*(\d+)000\s*\)/) {to avoid the extra tests below
scripts/checkpatch.pl sub process { · line 6608
Quoted 5 lines
scripts/checkpatch.pl +5
-0+ my $ms_duration = $1; + if ($ms_duration >= 1000 && ($ms_duration % 1000) == 0) { + my $ss_duration = $ms_duration / 1000; + WARN("SSLEEP", + "Prefer ssleep($ss_duration) over msleep($ms_duration);\n" . $herecurr);
And please add a $fix to this so:
if ($line =~ /\bmsleep\s*\(\s*(\d+)000\s*\)/) {
$secs = $1;
if (WARN("SSLEEP",
"Prefer ssleep($secs) over msleep(${secs}000\n") &&
$fix) {
$fixed[$fixlinenr] =~ s/\bmsleep\s*\(\s*${secs}000\s*\)/ssleep($secs)/;
}