sleepus.c 785 B

123456789101112131415161718192021222324252627282930
  1. #include <sys/types.h>
  2. #include <sys/time.h>
  3. #include <errno.h>
  4. #include <stddef.h>
  5. #include "ourhdr.h"
  6. void
  7. sleep_us(unsigned int nusecs)
  8. {
  9. struct timeval tval;
  10. for ( ; ; ) {
  11. tval.tv_sec = nusecs / 1000000;
  12. tval.tv_usec = nusecs % 1000000;
  13. if (select(0, NULL, NULL, NULL, &tval) == 0)
  14. break; /* all OK */
  15. /*
  16. * Note than on an interrupted system call (i.e, SIGIO) there's not
  17. * much we can do, since the timeval{} isn't updated with the time
  18. * remaining. We could obtain the clock time before the call, and
  19. * then obtain the clock time here, subtracting them to determine
  20. * how long select() blocked before it was interrupted, but that
  21. * seems like too much work :-)
  22. */
  23. if (errno == EINTR)
  24. continue;
  25. err_sys("sleep_us: select error");
  26. }
  27. }