diff options
author | Taru Karttunen <taruti@taruti.net> | 2011-03-30 15:46:40 +0300 |
---|---|---|
committer | Taru Karttunen <taruti@taruti.net> | 2011-03-30 15:46:40 +0300 |
commit | e5888a1ffdae813d7575f5fb02275c6bb07e5199 (patch) | |
tree | d8d51eac403f07814b9e936eed0c9a79195e2450 /sys/src/cmd/unix/drawterm/libc/nsec.c |
Import sources from 2011-03-30 iso image
Diffstat (limited to 'sys/src/cmd/unix/drawterm/libc/nsec.c')
-rwxr-xr-x | sys/src/cmd/unix/drawterm/libc/nsec.c | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/sys/src/cmd/unix/drawterm/libc/nsec.c b/sys/src/cmd/unix/drawterm/libc/nsec.c new file mode 100755 index 000000000..0f78cefff --- /dev/null +++ b/sys/src/cmd/unix/drawterm/libc/nsec.c @@ -0,0 +1,65 @@ +#include <u.h> +#include <libc.h> + +static uvlong order = (uvlong) 0x0001020304050607ULL; + +static void +be2vlong(vlong *to, uchar *f) +{ + uchar *t, *o; + int i; + + t = (uchar*)to; + o = (uchar*)ℴ + for(i = 0; i < 8; i++) + t[o[i]] = f[i]; +} + +/* + * After a fork with fd's copied, both fd's are pointing to + * the same Chan structure. Since the offset is kept in the Chan + * structure, the seek's and read's in the two processes can + * compete at moving the offset around. Hence the retry loop. + * + * Since the bintime version doesn't need a seek, it doesn't + * have the loop. + */ +vlong +nsec(void) +{ + char b[12+1]; + static int f = -1; + static int usebintime; + int retries; + vlong t; + + if(f < 0){ + usebintime = 1; + f = open("/dev/bintime", OREAD|OCEXEC); + if(f < 0){ + usebintime = 0; + f = open("/dev/nsec", OREAD|OCEXEC); + if(f < 0) + return 0; + } + } + + if(usebintime){ + if(read(f, b, sizeof(uvlong)) < 0) + goto error; + be2vlong(&t, (uchar*)b); + return t; + } else { + for(retries = 0; retries < 100; retries++){ + if(seek(f, 0, 0) >= 0 && read(f, b, sizeof(b)-1) >= 0){ + b[sizeof(b)-1] = 0; + return strtoll(b, 0, 0); + } + } + } + +error: + close(f); + f = -1; + return 0; +} |