time.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright (C) 2000 - 2007 Jeff Dike (jdike{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include <stddef.h>
  6. #include <errno.h>
  7. #include <signal.h>
  8. #include <time.h>
  9. #include <sys/time.h>
  10. #include "kern_util.h"
  11. #include "kern_constants.h"
  12. #include "os.h"
  13. #include "user.h"
  14. int set_interval(int is_virtual)
  15. {
  16. int usec = 1000000/hz();
  17. int timer_type = is_virtual ? ITIMER_VIRTUAL : ITIMER_REAL;
  18. struct itimerval interval = ((struct itimerval) { { 0, usec },
  19. { 0, usec } });
  20. if (setitimer(timer_type, &interval, NULL) == -1)
  21. return -errno;
  22. return 0;
  23. }
  24. void disable_timer(void)
  25. {
  26. struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }});
  27. if ((setitimer(ITIMER_VIRTUAL, &disable, NULL) < 0) ||
  28. (setitimer(ITIMER_REAL, &disable, NULL) < 0))
  29. printk(UM_KERN_ERR "disable_timer - setitimer failed, "
  30. "errno = %d\n", errno);
  31. /* If there are signals already queued, after unblocking ignore them */
  32. signal(SIGALRM, SIG_IGN);
  33. signal(SIGVTALRM, SIG_IGN);
  34. }
  35. void switch_timers(int to_real)
  36. {
  37. struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }});
  38. struct itimerval enable = ((struct itimerval) { { 0, 1000000/hz() },
  39. { 0, 1000000/hz() }});
  40. int old, new;
  41. if (to_real) {
  42. old = ITIMER_VIRTUAL;
  43. new = ITIMER_REAL;
  44. }
  45. else {
  46. old = ITIMER_REAL;
  47. new = ITIMER_VIRTUAL;
  48. }
  49. if ((setitimer(old, &disable, NULL) < 0) ||
  50. (setitimer(new, &enable, NULL)))
  51. printk(UM_KERN_ERR "switch_timers - setitimer failed, "
  52. "errno = %d\n", errno);
  53. }
  54. unsigned long long os_nsecs(void)
  55. {
  56. struct timeval tv;
  57. gettimeofday(&tv, NULL);
  58. return (unsigned long long) tv.tv_sec * BILLION + tv.tv_usec * 1000;
  59. }
  60. void idle_sleep(int secs)
  61. {
  62. struct timespec ts;
  63. ts.tv_sec = secs;
  64. ts.tv_nsec = 0;
  65. nanosleep(&ts, NULL);
  66. }