time.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. /* If there are signals already queued, after unblocking ignore them */
  31. signal(SIGALRM, SIG_IGN);
  32. signal(SIGVTALRM, SIG_IGN);
  33. }
  34. int switch_timers(int to_real)
  35. {
  36. struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }});
  37. struct itimerval enable;
  38. int old, new, old_type = is_real_timer;
  39. if(to_real == old_type)
  40. return to_real;
  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, &enable) < 0)
  50. printk(UM_KERN_ERR "switch_timers - setitimer disable failed, "
  51. "errno = %d\n", errno);
  52. if((enable.it_value.tv_sec == 0) && (enable.it_value.tv_usec == 0))
  53. enable.it_value = enable.it_interval;
  54. if (setitimer(new, &enable, NULL))
  55. printk(UM_KERN_ERR "switch_timers - setitimer enable failed, "
  56. "errno = %d\n", errno);
  57. is_real_timer = to_real;
  58. return old_type;
  59. }
  60. unsigned long long os_nsecs(void)
  61. {
  62. struct timeval tv;
  63. gettimeofday(&tv, NULL);
  64. return (unsigned long long) tv.tv_sec * BILLION + tv.tv_usec * 1000;
  65. }
  66. void idle_sleep(int secs)
  67. {
  68. struct timespec ts;
  69. ts.tv_sec = secs;
  70. ts.tv_nsec = 0;
  71. nanosleep(&ts, NULL);
  72. }