usleep.c 826 B

1234567891011121314151617181920212223242526272829303132333435
  1. /*
  2. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  3. *
  4. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  5. */
  6. #include <time.h>
  7. #include <sys/time.h>
  8. #include <sys/types.h>
  9. #include <unistd.h>
  10. #if defined __USE_BSD || defined __USE_POSIX98
  11. #if defined __UCLIBC_HAS_REALTIME__
  12. /*libc_hidden_proto(nanosleep) need the reloc for cancellation*/
  13. int usleep (__useconds_t usec)
  14. {
  15. const struct timespec ts = {
  16. .tv_sec = (long int) (usec / 1000000),
  17. .tv_nsec = (long int) (usec % 1000000) * 1000ul
  18. };
  19. return(nanosleep(&ts, NULL));
  20. }
  21. #else /* __UCLIBC_HAS_REALTIME__ */
  22. /* libc_hidden_proto(select) */
  23. int usleep (__useconds_t usec)
  24. {
  25. struct timeval tv;
  26. tv.tv_sec = 0;
  27. tv.tv_usec = usec;
  28. return select(0, NULL, NULL, NULL, &tv);
  29. }
  30. #endif /* __UCLIBC_HAS_REALTIME__ */
  31. #endif