8 int die; /* flag: quit if set */
9 void (*f)(void*); /* call this each period */
10 void *a; /* argument to f */
11 int msec; /* period */
14 static void periodicThread(void *a);
17 periodicAlloc(void (*f)(void*), void *a, int msec)
21 p = vtmallocz(sizeof(Periodic));
28 proccreate(periodicThread, p, STACK);
33 periodicKill(Periodic *p)
43 periodicFree(Periodic *p)
49 periodicThread(void *a)
52 vlong t, ct, ts; /* times in ms. */
54 threadsetname("periodic");
56 ct = nsec() / 1000000;
57 t = ct + p->msec; /* call p->f at or after this time */
60 ts = t - ct; /* ms. to next cycle's start */
62 ts = 1000; /* bound sleep duration */
64 sleep(ts); /* wait for cycle's start */
71 ct = nsec() / 1000000;
72 if(t <= ct){ /* due to call p->f? */
74 ct = nsec() / 1000000;
75 while(t <= ct) /* advance t to future cycle start */