3
0

bb_do_delay.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Busybox utility routines.
  4. *
  5. * Copyright (C) 2005 by Tito Ragusa <tito-wolit@tiscali.it>
  6. *
  7. * Licensed under GPLv2, see file LICENSE in this source tree.
  8. */
  9. #include "libbb.h"
  10. /* void FAST_FUNC bb_do_delay(unsigned seconds) { ... } - no users yet */
  11. #ifndef LOGIN_FAIL_DELAY
  12. #define LOGIN_FAIL_DELAY 3
  13. #endif
  14. void FAST_FUNC pause_after_failed_login(void)
  15. {
  16. #if 0 /* over-engineered madness */
  17. time_t end, diff;
  18. end = time(NULL) + LOGIN_FAIL_DELAY;
  19. diff = LOGIN_FAIL_DELAY;
  20. do {
  21. sleep(diff);
  22. diff = end - time(NULL);
  23. } while (diff > 0);
  24. #else
  25. sleep(LOGIN_FAIL_DELAY);
  26. #endif
  27. }
  28. void FAST_FUNC sleep1(void)
  29. {
  30. sleep(1);
  31. }
  32. void FAST_FUNC msleep(unsigned ms)
  33. {
  34. #if 0
  35. /* 1. usleep(n) is not guaranteed by standards to accept n >= 1000000
  36. * 2. multiplication in usleep(ms * 1000) can overflow if ms > 4294967
  37. * (sleep of ~71.5 minutes)
  38. * Let's play safe and loop:
  39. */
  40. while (ms > 500) {
  41. usleep(500000);
  42. ms -= 500;
  43. }
  44. usleep(ms * 1000);
  45. #else
  46. //usleep is often implemented as a call to nanosleep.
  47. //Simply do the same to implement msleep.
  48. //it's marginally larger, but wakes your CPU less often:
  49. //function old new delta
  50. //msleep 45 52 +7
  51. struct timespec ts;
  52. ts.tv_sec = ms / 1000;
  53. ts.tv_nsec = (ms % 1000) * 1000000;
  54. /*
  55. * If a signal has non-default handler, nanosleep returns early.
  56. * Our version of msleep doesn't return early
  57. * if interrupted by such signals:
  58. */
  59. while (nanosleep(&ts, &ts) != 0)
  60. continue;
  61. #endif
  62. }