2 #include "../port/lib.h"
15 static Timers timers[MAXMACH];
17 ulong intrcount[MAXMACH];
18 ulong fcallcount[MAXMACH];
21 tadd(Timers *tt, Timer *nt)
25 /* Called with tt locked */
26 assert(nt->tt == nil);
34 nt->twhen = fastticks(nil) + ns2fastticks(nt->tns);
37 assert(nt->tns >= 100000); /* At least 100 µs period */
39 /* look for another timer at same frequency for combining */
40 for(t = tt->head; t; t = t->tnext){
41 if(t->tmode == Tperiodic && t->tns == nt->tns)
47 nt->twhen = fastticks(nil);
49 nt->twhen += ns2fastticks(nt->tns);
53 for(last = &tt->head; t = *last; last = &t->tnext){
54 if(t->twhen > nt->twhen)
75 for(last = &tt->head; t = *last; last = &t->tnext){
83 if(last == &tt->head && tt->head)
84 return tt->head->twhen;
88 /* add or modify a timer */
95 /* Must lock Timer struct before Timers struct */
102 tt = &timers[m->machno];
122 if(when && tt == &timers[m->machno])
123 timerset(tt->head->twhen);
134 m->proc->pc = ur->pc;
145 if(kproftimer != nil)
148 if((active.machs&(1<<m->machno)) == 0)
152 print("someone's exiting\n");
158 if(up && up->state == Running)
159 hzsched(); /* in proc.c */
163 timerintr(Ureg *u, Tval)
171 intrcount[m->machno]++;
173 tt = &timers[m->machno];
174 now = fastticks(nil);
178 * No need to ilock t here: any manipulation of t
179 * requires tdel(t) and this must be done with a
180 * lock to tt held. We have tt, so the tdel will
181 * wait until we're done
194 fcallcount[m->machno]++;
201 if(t->tmode == Tperiodic)
213 * T->tf == nil means the HZ clock for this processor.
216 t = malloc(sizeof(*t));
217 t->tmode = Tperiodic;
219 t->tns = 1000000000/HZ;
225 addclock0link(void (*f)(void), int ms)
230 /* Synchronize to hztimer if ms is 0 */
231 nt = malloc(sizeof(Timer));
234 nt->tns = (vlong)ms*1000000LL;
235 nt->tmode = Tperiodic;
237 nt->tf = (void (*)(Ureg*, Timer*))f;
240 when = tadd(&timers[0], nt);
248 * This tk2ms avoids overflows that the macro version is prone to.
249 * It is a LOT slower so shouldn't be used if you're just converting
268 /* avoid overflows at the cost of precision */
269 if(ms >= 1000000000/HZ)
271 return (ms*HZ+500)/1000;