time.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <time.h>
  9. #include <sys/time.h>
  10. #include <signal.h>
  11. #include <errno.h>
  12. #include "user_util.h"
  13. #include "kern_util.h"
  14. #include "user.h"
  15. #include "process.h"
  16. #include "kern_constants.h"
  17. #include "os.h"
  18. static void set_interval(int timer_type)
  19. {
  20. int usec = 1000000/hz();
  21. struct itimerval interval = ((struct itimerval) { { 0, usec },
  22. { 0, usec } });
  23. if(setitimer(timer_type, &interval, NULL) == -1)
  24. panic("setitimer failed - errno = %d\n", errno);
  25. }
  26. void enable_timer(void)
  27. {
  28. set_interval(ITIMER_VIRTUAL);
  29. }
  30. void disable_timer(void)
  31. {
  32. struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }});
  33. if((setitimer(ITIMER_VIRTUAL, &disable, NULL) < 0) ||
  34. (setitimer(ITIMER_REAL, &disable, NULL) < 0))
  35. printk("disnable_timer - setitimer failed, errno = %d\n",
  36. errno);
  37. /* If there are signals already queued, after unblocking ignore them */
  38. set_handler(SIGALRM, SIG_IGN, 0, -1);
  39. set_handler(SIGVTALRM, SIG_IGN, 0, -1);
  40. }
  41. void switch_timers(int to_real)
  42. {
  43. struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }});
  44. struct itimerval enable = ((struct itimerval) { { 0, 1000000/hz() },
  45. { 0, 1000000/hz() }});
  46. int old, new;
  47. if(to_real){
  48. old = ITIMER_VIRTUAL;
  49. new = ITIMER_REAL;
  50. }
  51. else {
  52. old = ITIMER_REAL;
  53. new = ITIMER_VIRTUAL;
  54. }
  55. if((setitimer(old, &disable, NULL) < 0) ||
  56. (setitimer(new, &enable, NULL)))
  57. printk("switch_timers - setitimer failed, errno = %d\n",
  58. errno);
  59. }
  60. #ifdef UML_CONFIG_MODE_TT
  61. void uml_idle_timer(void)
  62. {
  63. if(signal(SIGVTALRM, SIG_IGN) == SIG_ERR)
  64. panic("Couldn't unset SIGVTALRM handler");
  65. set_handler(SIGALRM, (__sighandler_t) alarm_handler,
  66. SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1);
  67. set_interval(ITIMER_REAL);
  68. }
  69. #endif
  70. unsigned long long os_nsecs(void)
  71. {
  72. struct timeval tv;
  73. gettimeofday(&tv, NULL);
  74. return((unsigned long long) tv.tv_sec * BILLION + tv.tv_usec * 1000);
  75. }
  76. void idle_sleep(int secs)
  77. {
  78. struct timespec ts;
  79. ts.tv_sec = secs;
  80. ts.tv_nsec = 0;
  81. nanosleep(&ts, NULL);
  82. }
  83. void user_time_init(void)
  84. {
  85. set_interval(ITIMER_VIRTUAL);
  86. }