time.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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_constants.h"
  11. #include "os.h"
  12. #include "user.h"
  13. static int is_real_timer = 0;
  14. int set_interval(void)
  15. {
  16. int usec = 1000000/UM_HZ;
  17. struct itimerval interval = ((struct itimerval) { { 0, usec },
  18. { 0, usec } });
  19. if (setitimer(ITIMER_VIRTUAL, &interval, NULL) == -1)
  20. return -errno;
  21. return 0;
  22. }
  23. void disable_timer(void)
  24. {
  25. struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }});
  26. if ((setitimer(ITIMER_VIRTUAL, &disable, NULL) < 0) ||
  27. (setitimer(ITIMER_REAL, &disable, NULL) < 0))
  28. printk(UM_KERN_ERR "disable_timer - setitimer failed, "
  29. "errno = %d\n", errno);
  30. }
  31. int switch_timers(int to_real)
  32. {
  33. struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }});
  34. struct itimerval enable;
  35. int old, new, old_type = is_real_timer;
  36. if(to_real == old_type)
  37. return to_real;
  38. if (to_real) {
  39. old = ITIMER_VIRTUAL;
  40. new = ITIMER_REAL;
  41. }
  42. else {
  43. old = ITIMER_REAL;
  44. new = ITIMER_VIRTUAL;
  45. }
  46. if (setitimer(old, &disable, &enable) < 0)
  47. printk(UM_KERN_ERR "switch_timers - setitimer disable failed, "
  48. "errno = %d\n", errno);
  49. if((enable.it_value.tv_sec == 0) && (enable.it_value.tv_usec == 0))
  50. enable.it_value = enable.it_interval;
  51. if (setitimer(new, &enable, NULL))
  52. printk(UM_KERN_ERR "switch_timers - setitimer enable failed, "
  53. "errno = %d\n", errno);
  54. is_real_timer = to_real;
  55. return old_type;
  56. }
  57. unsigned long long os_nsecs(void)
  58. {
  59. struct timeval tv;
  60. gettimeofday(&tv, NULL);
  61. return (unsigned long long) tv.tv_sec * BILLION + tv.tv_usec * 1000;
  62. }
  63. void idle_sleep(int secs)
  64. {
  65. struct timespec ts;
  66. ts.tv_sec = secs;
  67. ts.tv_nsec = 0;
  68. nanosleep(&ts, NULL);
  69. }