time.c 1.8 KB
Newer Older
1
/*
J
Jeff Dike 已提交
2
 * Copyright (C) 2000 - 2007 Jeff Dike (jdike{addtoit,linux.intel}.com)
3 4 5
 * Licensed under the GPL
 */

J
Jeff Dike 已提交
6 7 8
#include <stddef.h>
#include <errno.h>
#include <signal.h>
9
#include <time.h>
L
Linus Torvalds 已提交
10
#include <sys/time.h>
11 12
#include "kern_constants.h"
#include "os.h"
J
Jeff Dike 已提交
13
#include "user.h"
14

J
Jeff Dike 已提交
15 16
static int is_real_timer = 0;

J
Jeff Dike 已提交
17
int set_interval(void)
18
{
J
Jeff Dike 已提交
19
	int usec = 1000000/UM_HZ;
20 21 22
	struct itimerval interval = ((struct itimerval) { { 0, usec },
							  { 0, usec } });

J
Jeff Dike 已提交
23
	if (setitimer(ITIMER_VIRTUAL, &interval, NULL) == -1)
J
Jeff Dike 已提交
24 25 26
		return -errno;

	return 0;
27 28 29 30 31
}

void disable_timer(void)
{
	struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }});
J
Jeff Dike 已提交
32 33 34 35 36

	if ((setitimer(ITIMER_VIRTUAL, &disable, NULL) < 0) ||
	    (setitimer(ITIMER_REAL, &disable, NULL) < 0))
		printk(UM_KERN_ERR "disable_timer - setitimer failed, "
		       "errno = %d\n", errno);
37 38
}

J
Jeff Dike 已提交
39
int switch_timers(int to_real)
40 41
{
	struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }});
J
Jeff Dike 已提交
42 43 44 45 46
	struct itimerval enable;
	int old, new, old_type = is_real_timer;

	if(to_real == old_type)
		return to_real;
47

J
Jeff Dike 已提交
48
	if (to_real) {
49 50 51 52 53 54 55 56
		old = ITIMER_VIRTUAL;
		new = ITIMER_REAL;
	}
	else {
		old = ITIMER_REAL;
		new = ITIMER_VIRTUAL;
	}

J
Jeff Dike 已提交
57 58 59 60 61 62 63 64 65
	if (setitimer(old, &disable, &enable) < 0)
		printk(UM_KERN_ERR "switch_timers - setitimer disable failed, "
		       "errno = %d\n", errno);

	if((enable.it_value.tv_sec == 0) && (enable.it_value.tv_usec == 0))
		enable.it_value = enable.it_interval;

	if (setitimer(new, &enable, NULL))
		printk(UM_KERN_ERR "switch_timers - setitimer enable failed, "
J
Jeff Dike 已提交
66
		       "errno = %d\n", errno);
J
Jeff Dike 已提交
67 68 69

	is_real_timer = to_real;
	return old_type;
70
}
L
Linus Torvalds 已提交
71

72
unsigned long long os_nsecs(void)
L
Linus Torvalds 已提交
73 74 75 76
{
	struct timeval tv;

	gettimeofday(&tv, NULL);
J
Jeff Dike 已提交
77
	return (unsigned long long) tv.tv_sec * BILLION + tv.tv_usec * 1000;
L
Linus Torvalds 已提交
78 79
}

80 81 82 83 84 85 86 87
void idle_sleep(int secs)
{
	struct timespec ts;

	ts.tv_sec = secs;
	ts.tv_nsec = 0;
	nanosleep(&ts, NULL);
}