1 #include <u.h>
2 #include <libc.h>
3
4
5 /*
6 * After a fork with fd's copied, both fd's are pointing to
7 * the same Chan structure. Since the offset is kept in the Chan
8 * structure, the seek's and read's in the two processes can
9 * compete at moving the offset around. Hence the unusual loop
10 * in the middle of this routine.
11 */
12 static long
oldtime(long * tp)13 oldtime(long *tp)
14 {
15 char b[20];
16 static int f = -1;
17 int i, retries;
18 long t;
19
20 memset(b, 0, sizeof(b));
21 for(retries = 0; retries < 100; retries++){
22 if(f < 0)
23 f = open("/dev/time", OREAD|OCEXEC);
24 if(f < 0)
25 break;
26 if(seek(f, 0, 0) < 0 || (i = read(f, b, sizeof(b))) < 0){
27 close(f);
28 f = -1;
29 } else {
30 if(i != 0)
31 break;
32 }
33 }
34 t = atol(b);
35 if(tp)
36 *tp = t;
37 return t;
38 }
39
40 long
time(long * tp)41 time(long *tp)
42 {
43 vlong t;
44
45 t = nsec()/((vlong)1000000000);
46 if(t == 0)
47 t = oldtime(0);
48 if(tp != nil)
49 *tp = t;
50 return t;
51 }
52