/* clock.c simple code to provide a simulated clock interrupt, and routines for manipulating them. Winter 1996 Tian Lim 4/6/96 moved atomic_test_and_set to assembly so the interrupt handler can restart it. 4/7/96 renamed functions. 4/21/96 switch to sigsetmask for enabling/disabling. not necessarily good for threads that depend on lack of signals. */ #include "clock.h" #include /* interrupts flag */ int interrupts = 0; /* global handle to the user's interrupt service routine */ static ClockHandler interruptHandler; /* this routine simulates a periodic clock tick. every PERIOD microseconds it will be called. if interrupts are enabled, it will call your interrupt service routine */ void clock_tick(int sig, int code, struct sigcontext*scp) { if (interrupts==ENABLED) /* no longer necessary, but.... */ { interruptHandler(); }else { } } void enable_interrupts() { interrupts = ENABLED; sigsetmask(0); } void disable_interrupts() { interrupts = DISABLED; sigsetmask(sigmask(SIGALRM)); } /* setup the interval timer and install interrupt handler. After this routine is called, and after you enable_interrupts(), clock interrupts will begin to be sent. They will call the handler function h specified by the caller. */ void minithread_clock_init(ClockHandler h) { struct itimerval it, temp; interruptHandler = h; signal (SIGALRM, (ClockHandler)&clock_tick); it.it_interval.tv_sec = 0; it.it_interval.tv_usec = PERIOD; it.it_value.tv_sec = 0; it.it_value.tv_usec = PERIOD; disable_interrupts(); setitimer(ITIMER_REAL, &it, &temp); }