Nanosleep was implemented twice, removed from _sleep.c
This commit is contained in:
parent
3573bc1abe
commit
f5421e64f8
@ -1,9 +1,10 @@
|
|||||||
/* nanosleep() - Sleep for a number of nanoseconds. Author: Erik van der Kouwe
|
/* nanosleep() - Sleep for a number of seconds. Author: Erik van der Kouwe
|
||||||
* 25 July 2009
|
* 25 July 2009
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <lib.h>
|
#include <lib.h>
|
||||||
#define nanosleep _nanosleep
|
#define nanosleep _nanosleep
|
||||||
|
#include <signal.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
@ -22,6 +23,15 @@ int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
|
|||||||
struct timeval timeout, timestart = { 0, 0 }, timeend;
|
struct timeval timeout, timestart = { 0, 0 }, timeend;
|
||||||
int errno_select, r;
|
int errno_select, r;
|
||||||
|
|
||||||
|
/* check parameters */
|
||||||
|
if (!rqtp)
|
||||||
|
return EFAULT;
|
||||||
|
|
||||||
|
if (rqtp->tv_sec < 0 ||
|
||||||
|
rqtp->tv_nsec < 0 ||
|
||||||
|
rqtp->tv_nsec >= NSEC_PER_SEC)
|
||||||
|
return EINVAL;
|
||||||
|
|
||||||
/* keep track of start time if needed */
|
/* keep track of start time if needed */
|
||||||
if (rmtp)
|
if (rmtp)
|
||||||
{
|
{
|
||||||
@ -50,8 +60,7 @@ int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
|
|||||||
|
|
||||||
/* compute remaining time */
|
/* compute remaining time */
|
||||||
rmtp->tv_sec = rqtp->tv_sec - (timeend.tv_sec - timestart.tv_sec);
|
rmtp->tv_sec = rqtp->tv_sec - (timeend.tv_sec - timestart.tv_sec);
|
||||||
rmtp->tv_nsec = rqtp->tv_nsec -
|
rmtp->tv_nsec = rqtp->tv_nsec - (timeend.tv_usec - timestart.tv_usec) * NSEC_PER_USEC;
|
||||||
(timeend.tv_usec - timestart.tv_usec) * NSEC_PER_USEC;
|
|
||||||
|
|
||||||
/* bring remaining time into canonical form */
|
/* bring remaining time into canonical form */
|
||||||
while (rmtp->tv_nsec < 0)
|
while (rmtp->tv_nsec < 0)
|
||||||
@ -75,3 +84,4 @@ int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
|
|||||||
|
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,7 +5,6 @@
|
|||||||
|
|
||||||
#include <lib.h>
|
#include <lib.h>
|
||||||
#define sleep _sleep
|
#define sleep _sleep
|
||||||
#define nanosleep _nanosleep
|
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
@ -13,80 +12,6 @@
|
|||||||
#include <sys/select.h>
|
#include <sys/select.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
|
|
||||||
#define MSEC_PER_SEC 1000
|
|
||||||
#define USEC_PER_MSEC 1000
|
|
||||||
#define NSEC_PER_USEC 1000
|
|
||||||
|
|
||||||
#define USEC_PER_SEC (USEC_PER_MSEC * MSEC_PER_SEC)
|
|
||||||
#define NSEC_PER_SEC (NSEC_PER_USEC * USEC_PER_SEC)
|
|
||||||
|
|
||||||
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
|
|
||||||
{
|
|
||||||
struct timeval timeout, timestart = { 0, 0 }, timeend;
|
|
||||||
int errno_select, r;
|
|
||||||
|
|
||||||
/* check parameters */
|
|
||||||
if (!rqtp)
|
|
||||||
return EFAULT;
|
|
||||||
|
|
||||||
if (rqtp->tv_sec < 0 ||
|
|
||||||
rqtp->tv_nsec < 0 ||
|
|
||||||
rqtp->tv_nsec >= NSEC_PER_SEC)
|
|
||||||
return EINVAL;
|
|
||||||
|
|
||||||
/* keep track of start time if needed */
|
|
||||||
if (rmtp)
|
|
||||||
{
|
|
||||||
rmtp->tv_sec = 0;
|
|
||||||
rmtp->tv_nsec = 0;
|
|
||||||
if (gettimeofday(×tart, NULL) < 0)
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* use select to wait */
|
|
||||||
timeout.tv_sec = rqtp->tv_sec;
|
|
||||||
timeout.tv_usec = (rqtp->tv_nsec + NSEC_PER_USEC - 1) / NSEC_PER_USEC;
|
|
||||||
r = select(0, NULL, NULL, NULL, &timeout);
|
|
||||||
|
|
||||||
/* return remaining time only if requested */
|
|
||||||
/* if select succeeded then we slept all time */
|
|
||||||
if (!rmtp || r >= 0)
|
|
||||||
return r;
|
|
||||||
|
|
||||||
/* measure end time; preserve errno */
|
|
||||||
errno_select = errno;
|
|
||||||
if (gettimeofday(&timeend, NULL) < 0)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
errno = errno_select;
|
|
||||||
|
|
||||||
/* compute remaining time */
|
|
||||||
rmtp->tv_sec = rqtp->tv_sec - (timeend.tv_sec - timestart.tv_sec);
|
|
||||||
rmtp->tv_nsec = rqtp->tv_nsec - (timeend.tv_usec - timestart.tv_usec) * NSEC_PER_USEC;
|
|
||||||
|
|
||||||
/* bring remaining time into canonical form */
|
|
||||||
while (rmtp->tv_nsec < 0)
|
|
||||||
{
|
|
||||||
rmtp->tv_sec -= 1;
|
|
||||||
rmtp->tv_nsec += NSEC_PER_SEC;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (rmtp->tv_nsec > NSEC_PER_SEC)
|
|
||||||
{
|
|
||||||
rmtp->tv_sec += 1;
|
|
||||||
rmtp->tv_nsec -= NSEC_PER_SEC;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* remaining time must not be negative */
|
|
||||||
if (rmtp->tv_sec < 0)
|
|
||||||
{
|
|
||||||
rmtp->tv_sec = 0;
|
|
||||||
rmtp->tv_nsec = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned sleep(unsigned sleep_seconds)
|
unsigned sleep(unsigned sleep_seconds)
|
||||||
{
|
{
|
||||||
struct timespec rqtp, rmtp;
|
struct timespec rqtp, rmtp;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user