This is the mail archive of the
newlib@sources.redhat.com
mailing list for the newlib project.
sleep
- From: Shaun Jackman <sjackman at gmail dot com>
- To: newlib at sources dot redhat dot com
- Date: Wed, 26 Jan 2005 11:45:50 -0800
- Subject: sleep
- Reply-to: Shaun Jackman <sjackman at gmail dot com>
I've written a simple sleep function that depends on an incrementing
counter, _jiffies, and its rate, HZ. It busywaits, calling _sleep_hook
until either the timer expires or _sleep_hook returns -1, presumably
indicating a signal has interrupted the sleep. _jiffies is provided by
the user and most likely incremented by a timer interrupt service,
although it could be updated in _sleep_hook by some magic. If all the
newlib platforms support weak symbols, _sleep_hook could be a weak
symbol rather than a pointer to a function, though I think the latter
is probably more portable and flexible.
If HZ is undefined it returns immediately. Alternatively, sleep could
be left undefined, which is probably better, come to think of it, than
a defined function that does nothing useful whatsoever.
I'm not sure newlib is the ideal place for this code, but its
dependencies are few and provided by most common systems.
Cheers,
Shaun
#include <time.h>
clock_t _jiffies;
int (*_sleep_hook)(clock_t elapsed, clock_t delay);
unsigned int
sleep(unsigned int seconds)
{
#if HZ
clock_t now = _jiffies, delay = seconds * HZ, elapsed;
while( (elapsed = _jiffies - now) < delay )
if( _sleep_hook != NULL && _sleep_hook(elapsed, delay) == -1 )
break;
return (delay - elapsed) / HZ;
#else
return seconds;
#endif
}