[lib] Add owl_msleep()

On *BSD, usleep() does not accept an argument greater than 1'000'000 (1
second). owl_msleep() decomposes its arguments to use sleep() and
usleep() calls.
This commit is contained in:
Matteo Cypriani 2011-08-24 15:31:10 +02:00
parent 40bc6d0b59
commit 98609d146d
2 changed files with 35 additions and 0 deletions

View File

@ -132,6 +132,40 @@ uint_fast8_t owl_frequency_to_channel(const uint_fast16_t channel)
/* *** Time *** */
/*
* Sleeps for a given amount of milliseconds.
* 'time_ms' is an unsigned value, so please be careful: passing a
* negative value may not do what you think.
* In case of error, a message is displayed and a non-zero error code
* is returned.
*/
int owl_msleep(uint32_t time_ms)
{
int ret ;
uint_fast32_t seconds, microseconds ;
if (! time_ms)
return 0 ;
seconds = time_ms / 1000 ;
microseconds = time_ms % 1000 * 1000 ;
if ((ret = sleep(seconds)))
{
perror("Cannot sleep()") ;
return ret ;
}
if ((ret = usleep(microseconds)))
{
perror("Cannot usleep()") ;
return ret ;
}
return 0 ;
}
/*
* Sets the owl_timestamp 'now' at the current time.
* Returns 0 in case of success non-zero otherwise.

View File

@ -187,6 +187,7 @@ owl_bool owl_mac_equals(const uint8_t *const mac1,
uint_fast8_t owl_frequency_to_channel(const uint_fast16_t channel) ;
// Time
int owl_msleep(uint32_t time_ms) ;
int owl_timestamp_now(owl_timestamp *const now) ;
owl_timestamp owl_timespec_to_timestamp(const struct timespec d) ;
owl_timestamp owl_timeval_to_timestamp(const struct timeval d) ;