time.c 1.7 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
int set_interval(int is_virtual)
16
{
J
Jeff Dike 已提交
17
	int usec = 1000000/UM_HZ;
J
Jeff Dike 已提交
18
	int timer_type = is_virtual ? ITIMER_VIRTUAL : ITIMER_REAL;
19 20 21
	struct itimerval interval = ((struct itimerval) { { 0, usec },
							  { 0, usec } });

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

	return 0;
26 27 28 29 30
}

void disable_timer(void)
{
	struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }});
J
Jeff Dike 已提交
31 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
	/* If there are signals already queued, after unblocking ignore them */
38 39
	signal(SIGALRM, SIG_IGN);
	signal(SIGVTALRM, SIG_IGN);
40 41 42 43 44
}

void switch_timers(int to_real)
{
	struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }});
J
Jeff Dike 已提交
45 46
	struct itimerval enable = ((struct itimerval) { { 0, 1000000/UM_HZ },
							{ 0, 1000000/UM_HZ }});
47 48
	int old, new;

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

J
Jeff Dike 已提交
58 59 60 61
	if ((setitimer(old, &disable, NULL) < 0) ||
	    (setitimer(new, &enable, NULL)))
		printk(UM_KERN_ERR "switch_timers - setitimer failed, "
		       "errno = %d\n", errno);
62
}
L
Linus Torvalds 已提交
63

64
unsigned long long os_nsecs(void)
L
Linus Torvalds 已提交
65 66 67 68
{
	struct timeval tv;

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

72 73 74 75 76 77 78 79
void idle_sleep(int secs)
{
	struct timespec ts;

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