--- /n/sources/plan9/sys/src/libc/9sys/nsec.c Tue Jul 3 21:31:23 2012 +++ /sys/src/libc/9sys/nsec.c Sun Feb 28 00:00:00 2016 @@ -1,76 +1,19 @@ #include #include -#include -static uvlong order = 0x0001020304050607ULL; - -static void -be2vlong(vlong *to, uchar *f) -{ - uchar *t, *o; - int i; - - t = (uchar*)to; - o = (uchar*)ℴ - for(i = 0; i < sizeof order; i++) - t[o[i]] = f[i]; -} - -static int fd = -1; -static struct { - int pid; - int fd; -} fds[64]; +#define U32(x) (((((((x)[0]<<8)|(x)[1])<<8)|(x)[2])<<8)|(x)[3]) vlong nsec(void) { uchar b[8]; - vlong t; - int pid, i, f, tries; - - /* - * Threaded programs may have multiple procs - * with different fd tables, so we may need to open - * /dev/bintime on a per-pid basis - */ + int f, n; - /* First, look if we've opened it for this particular pid */ - if((pid = _tos->pid) == 0) /* 9vx bug, perhaps? */ - _tos->pid = pid = getpid(); - do{ - f = -1; - for(i = 0; i < nelem(fds); i++) - if(fds[i].pid == pid){ - f = fds[i].fd; - break; - } - tries = 0; - if(f < 0){ - /* If it's not open for this pid, try the global pid */ - if(fd >= 0) - f = fd; - else{ - /* must open */ - if((f = open("/dev/bintime", OREAD|OCEXEC)) < 0) - return 0; - fd = f; - for(i = 0; i < nelem(fds); i++) - if(fds[i].pid == pid || fds[i].pid == 0){ - fds[i].pid = pid; - fds[i].fd = f; - break; - } - } - } - if(pread(f, b, sizeof b, 0) == sizeof b){ - be2vlong(&t, b); - return t; - } + if((f = open("/dev/bintime", OREAD)) >= 0){ + n = pread(f, b, sizeof(b), 0); close(f); - if(i < nelem(fds)) - fds[i].fd = -1; - }while(tries++ == 0); /* retry once */ - USED(tries); + if(n == sizeof(b)) + return (u64int)U32(b)<<32 | U32(b+4); + } return 0; }