Skip to contentSkip to conversation

add function spin_event_timeout()

PATCH Superseded Timur Tabi 4 participants 6 messages Mar 7, 2009 – Mar 9, 2009 High-resolution timers, timer wheel, clockevents

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Timur Tabi

author
#0 lore raw

PATCH 1/1 add function spin_event_timeout()

The function spin_event_timeout() takes a condition and timeout value (in jiffies) as parameters. It spins until either the condition is true or the timeout expires. It returns non-zero if the condition is true, zero otherwise.

---
 include/linux/delay.h |   21 +++++++++++++++++++++
 1 files changed, 21 insertions(+), 0 deletions(-)
include/linux/delay.h +21 -0
index fd832c6..00ac466 100644
@@ -51,4 +51,25 @@ static inline void ssleep(unsigned int seconds)
 	msleep(seconds * 1000);
 }
 
+/**
+ * spin_event_timeout - spin until a condition gets true or a timeout elapses
+ * @condition: a C expression for the event to wait for
+ * @timeout: timeout, in jiffies
+ *
+ * The process spins until the @condition evaluates to true or the @timeout
+ * elapses.
+ *
+ * The function returns non-zero if the @condition evaluated to true, or
+ * zero if the @timeout elapsed.  If both occurs (e.g. the loop was
+ * pre-empted and the @condition became true in the meantime, but when the
+ * loop resumed the @timeout had already elapsed), then non-zero will be
+ * returned.
+ */
+#define spin_event_timeout(condition, timeout)		\
+({							\
+	long __timeout = jiffies + (timeout);		\
+	while (!(condition) && (jiffies < __timeout));	\
+	(condition);					\
+})
+
 #endif /* defined(_LINUX_DELAY_H) */
Signature · 1 line
1.6.1.3
#1 lore raw
Quoted 4 lines

The function spin_event_timeout() takes a condition and timeout value (in jiffies) as parameters. It spins until either the condition is true or the timeout expires. It returns non-zero if the condition is true, zero otherwise.

What's the motivation for this? Where do you plan to use it?

Quoted 1 line

+ * The function returns non-zero if the @condition evaluated to true, or

It's not a function but rather a macro.

include/linux/delay.h static inline void ssleep(unsigned int seconds) · line 73

Quoted 6 lines
include/linux/delay.h +6 -0
+#define spin_event_timeout(condition, timeout)		\
+({							\
+	long __timeout = jiffies + (timeout);		\
+	while (!(condition) && (jiffies < __timeout));	\
+	(condition);					\
+})

If we're going to make this core infrastructure, it seems we should implement it with all best practices... eg I would think the while loop should include cpu_relax().

author
#2 lore raw
Quoted 6 lines · Roland Dreier
Quoted 4 lines

The function spin_event_timeout() takes a condition and timeout value (in jiffies) as parameters. It spins until either the condition is true or the timeout expires. It returns non-zero if the condition is true, zero otherwise.

What's the motivation for this? Where do you plan to use it?

I expect it would be useful for drivers that need to wait until a specific register has changed its value, but don't want to risk an infinite loop if the hardware is not functioning. For example, to replace the while loop and subsequent check in this code:

long timeout = jiffies + 10;

setbits32(&ssi->scr, CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_RE);

/* Wait until the SSI has filled its FIFO. Without this
* delay, ALSA complains about overruns. When the FIFO
* is full, the DMA controller initiates its first
* transfer. Until then, however, the DMA's DAR
* register is zero, which translates to an
* out-of-bounds pointer. This makes ALSA think an
* overrun has occurred.
*/
while (!(in_be32(&ssi->sisr) & CCSR_SSI_SISR_RFF0) &&

        (jiffies < timeout));
if (!(in_be32(&ssi->sisr) & CCSR_SSI_SISR_RFF0))
        return -EIO;
Quoted 3 lines

If we're going to make this core infrastructure, it seems we should implement it with all best practices... eg I would think the while loop should include cpu_relax().

Ok. I'll post version 2 on Monday. Anything else I'm missing?

Signature · 2 lines
Timur Tabi
Linux kernel developer at Freescale
#3 lore raw
Quoted 1 line · Timur Tabi

Ok. I'll post version 2 on Monday. Anything else I'm missing?

Use time_after().

#4 lore raw
In reply to Timur Tabi
Quoted 7 lines · Timur Tabi
Quoted 5 lines · Roland Dreier
Quoted 4 lines

The function spin_event_timeout() takes a condition and timeout value (in jiffies) as parameters. It spins until either the condition is true or the timeout expires. It returns non-zero if the condition is true, zero otherwise.

Quoted 8 lines
Quoted 2 lines

What's the motivation for this? Where do you plan to use it?

I expect it would be useful for drivers that need to wait until a specific register has changed its value, but don't want to risk an infinite loop if the hardware is not functioning. For example, to replace the while loop and subsequent check in this code:

A jiffy is a really long time to be spinning, let alone liffies plural. In this use case are you expecting that the loop will iterate a very small number of times before returning, and the timeout case is a very unlikely error condition?

J

author
#5 lore raw
Quoted 4 lines · Jeremy Fitzhardinge

A jiffy is a really long time to be spinning, let alone liffies plural. In this use case are you expecting that the loop will iterate a very small number of times before returning, and the timeout case is a very unlikely error condition?

Yes, in version 3 of this patch, I switch to using udelay(). I might need to post a v4 though.

Signature · 2 lines
Timur Tabi
Linux kernel developer at Freescale