Skip to contentSkip to conversation

timers: Add jsleep() and jsleep_interruptible()

PATCH In review Ian Abbott 2 participants 2 messages Feb 21, 2013

Subsystems (2)
lore
Share & tools
lore

Ian Abbott

author
#0 lore raw

PATCH 1/1 timers: Add jsleep() and jsleep_interruptible()

Sometimes you want the waitqueue wake-up ignoring effect of msleep() or msleep_interruptible(), but happen to know the desired timeout in jiffies rather than milliseconds. In this case you'd do something like this:

	msleep(jiffies_to_msecs(timeout_in_jiffies));

The first thing that msleep() and msleep_interruptible() do is convert the parameter to jiffies (and msleep_interruptible() converts its return value back to milliseconds).

The new functions jsleep() and jsleep_interruptible() behave like msleep() and msleep_interruptible() except that the desired timeout (and return value of jsleep_interruptible()) is specified in jiffies. So the above call could be simplified to:

	jsleep(timeout_in_jiffies);

I'm not sure how useful this would be, but there are a couple of places in "serial_core.c" that could usefully call jsleep_interruptible() instead of msleep_interruptible().

---
 Documentation/timers/timers-howto.txt | 11 +++++++++--
 include/linux/delay.h                 |  2 ++
 kernel/timer.c                        | 32 +++++++++++++++++++++++++++-----
 3 files changed, 38 insertions(+), 7 deletions(-)
Documentation/timers/timers-howto.txt +9 -2
index 038f8c7..2911600 100644
@@ -46,8 +46,11 @@ NON-ATOMIC CONTEXT:
 	-- Backed by hrtimers:
 		usleep_range(unsigned long min, unsigned long max)
 	-- Backed by jiffies / legacy_timers
-		msleep(unsigned long msecs)
-		msleep_interruptible(unsigned long msecs)
+		jsleep(unsigned long timeout)
+		jsleep_interruptible(unsigned long timeout)
+		msleep(unsigned int msecs)
+		msleep_interruptible(unsigned int msecs)
+		ssleep(unsigned int secs)
 
 	Unlike the *delay family, the underlying mechanism
 	driving each of these calls varies, thus there are
@@ -103,3 +106,7 @@ NON-ATOMIC CONTEXT:
 			short, the difference is whether the sleep can be ended
 			early by a signal. In general, just use msleep unless
 			you know you have a need for the interruptible variant.
+
+			If the required delay is in jiffies, jsleep and
+			jsleep_interruptible are good alternatives to msleep
+			and msleep_interruptible, respectively.
include/linux/delay.h +2 -0
index a6ecb34..dffa295 100644
@@ -43,6 +43,8 @@ static inline void ndelay(unsigned long x)
 
 extern unsigned long lpj_fine;
 void calibrate_delay(void);
+void jsleep(unsigned long timeout);
+unsigned long jsleep_interruptible(unsigned long timeout);
 void msleep(unsigned int msecs);
 unsigned long msleep_interruptible(unsigned int msecs);
 void usleep_range(unsigned long min, unsigned long max);
kernel/timer.c +27 -5
index dbf7a78..ebb3733 100644
@@ -1794,6 +1794,31 @@ void __init init_timers(void)
 }
 
 /**
+ * jsleep - sleep safely even with waitqueue interruptions
+ * @timeout: Time in jiffies to sleep for
+ */
+void jsleep(unsigned long timeout)
+{
+	while (timeout)
+		timeout = schedule_timeout_uninterruptible(timeout);
+}
+
+EXPORT_SYMBOL_GPL(jsleep);
+
+/**
+ * jsleep_interruptible - sleep waiting for signals
+ * @timeout: Time in jiffies to sleep for
+ */
+unsigned long jsleep_interruptible(unsigned long timeout)
+{
+	while (timeout && !signal_pending(current))
+		timeout = schedule_timeout_interruptible(timeout);
+	return timeout;
+}
+
+EXPORT_SYMBOL(jsleep_interruptible);
+
+/**
  * msleep - sleep safely even with waitqueue interruptions
  * @msecs: Time in milliseconds to sleep for
  */
@@ -1801,8 +1826,7 @@ void msleep(unsigned int msecs)
 {
 	unsigned long timeout = msecs_to_jiffies(msecs) + 1;
 
-	while (timeout)
-		timeout = schedule_timeout_uninterruptible(timeout);
+	jsleep(timeout);
 }
 
 EXPORT_SYMBOL(msleep);
@@ -1815,9 +1839,7 @@ unsigned long msleep_interruptible(unsigned int msecs)
 {
 	unsigned long timeout = msecs_to_jiffies(msecs) + 1;
 
-	while (timeout && !signal_pending(current))
-		timeout = schedule_timeout_interruptible(timeout);
-	return jiffies_to_msecs(timeout);
+	return jiffies_to_msecs(jsleep_interruptible(timeout));
 }
 
 EXPORT_SYMBOL(msleep_interruptible);
Signature · 1 line
1.8.1.2
#1 lore raw
Quoted 3 lines · Ian Abbott

I'm not sure how useful this would be, but there are a couple of places in "serial_core.c" that could usefully call jsleep_interruptible() instead of msleep_interruptible().

Oh no. jiffies is such a misconception.

We want to get rid of jiffies in the long run not fostering the usage.

Thanks,

tglx