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