time.c 2.61 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* 
 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
 * Licensed under the GPL
 */

#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <signal.h>
#include <errno.h>
#include "user_util.h"
#include "kern_util.h"
#include "user.h"
#include "process.h"
#include "signal_user.h"
17
#include "time_user.h"
18 19 20 21 22 23 24 25

extern struct timeval xtime;

void timer(void)
{
	gettimeofday(&xtime, NULL);
}

26
void set_interval(int timer_type)
27
{
28 29 30
	int usec = 1000000/hz();
	struct itimerval interval = ((struct itimerval) { { 0, usec },
							  { 0, usec } });
31 32 33 34 35

	if(setitimer(timer_type, &interval, NULL) == -1)
		panic("setitimer failed - errno = %d\n", errno);
}

36 37
void enable_timer(void)
{
38 39 40
	int usec = 1000000/hz();
	struct itimerval enable = ((struct itimerval) { { 0, usec },
							{ 0, usec }});
41 42 43 44 45
	if(setitimer(ITIMER_VIRTUAL, &enable, NULL))
		printk("enable_timer - setitimer failed, errno = %d\n",
		       errno);
}

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
void switch_timers(int to_real)
{
	struct itimerval disable = ((struct itimerval) { { 0, 0 }, { 0, 0 }});
	struct itimerval enable = ((struct itimerval) { { 0, 1000000/hz() },
							{ 0, 1000000/hz() }});
	int old, new;

	if(to_real){
		old = ITIMER_VIRTUAL;
		new = ITIMER_REAL;
	}
	else {
		old = ITIMER_REAL;
		new = ITIMER_VIRTUAL;
	}

	if((setitimer(old, &disable, NULL) < 0) ||
	   (setitimer(new, &enable, NULL)))
		printk("switch_timers - setitimer failed, errno = %d\n",
		       errno);
}

68 69 70 71
void idle_timer(void)
{
	if(signal(SIGVTALRM, SIG_IGN) == SIG_ERR)
		panic("Couldn't unset SIGVTALRM handler");
72
	
73
	set_handler(SIGALRM, (__sighandler_t) alarm_handler, 
74
		    SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1);
75 76 77 78 79 80 81 82 83 84 85 86 87 88
	set_interval(ITIMER_REAL);
}

void time_init(void)
{
	if(signal(SIGVTALRM, boot_timer_handler) == SIG_ERR)
		panic("Couldn't set SIGVTALRM handler");
	set_interval(ITIMER_VIRTUAL);
}

struct timeval local_offset = { 0, 0 };

void do_gettimeofday(struct timeval *tv)
{
89
	time_lock();
90 91
	gettimeofday(tv, NULL);
	timeradd(tv, &local_offset, tv);
92
	time_unlock();
93 94 95 96 97 98
}

void do_settimeofday(struct timeval *tv)
{
	struct timeval now;

99
	time_lock();
100 101
	gettimeofday(&now, NULL);
	timersub(tv, &now, &local_offset);
102
	time_unlock();
103 104 105 106 107 108 109 110
}

void idle_sleep(int secs)
{
	struct timespec ts;

	ts.tv_sec = secs;
	ts.tv_nsec = 0;
111
	nanosleep(&ts, NULL);
112 113 114 115 116 117 118 119 120 121 122 123
}

/*
 * Overrides for Emacs so that we follow Linus's tabbing style.
 * Emacs will notice this stuff at the end of the file and automatically
 * adjust the settings for this buffer only.  This must remain at the end
 * of the file.
 * ---------------------------------------------------------------------------
 * Local variables:
 * c-file-style: "linux"
 * End:
 */