#include #include #include #include #include "dat.h" #include "fns.h" static double dtime(void) { return nsec()/1000000000.0; } static void timerproc(void *v) { void **vv; char *tag; Channel *nsc; int ms; double dt, ct, nt; vv = v; tag = vv[0]; nsc = vv[1]; ms = (int)(uintptr)vv[2]; free(v); threadsetname("timerproc #%s %d", tag, ms); free(tag); ct = dtime(); for(;;){ if(sleep(ms) < 0){ chanclose(nsc); fprint(2, "%s exiting\n", threadgetname()); threadexits(nil); } nt = dtime(); dt = nt - ct; ct = nt; send(nsc, &dt); } } Channel* timerchan(int ms, char *tag) { void **v; Channel *ns; v = mallocz(sizeof(void*)*3, 1); ns = chancreate(sizeof(double), 0); v[0] = strdup(tag); v[1] = ns; v[2] = (void*)ms; proccreate(timerproc, v, 8192); return ns; }