Source file
src/runtime/time_fake.go
1
2
3
4
5
6
7
8
9
10
11
12 package runtime
13
14 import "unsafe"
15
16
17
18 var faketime int64 = 1257894000000000000
19
20 var faketimeState struct {
21 lock mutex
22
23
24 lastfaketime int64
25
26
27
28
29
30
31 lastfd uintptr
32 }
33
34
35
36 func nanotime() int64 {
37 return faketime
38 }
39
40
41 func time_now() (sec int64, nsec int32, mono int64) {
42 return faketime / 1e9, int32(faketime % 1e9), faketime
43 }
44
45
46
47
48
49
50 func write(fd uintptr, p unsafe.Pointer, n int32) int32 {
51 if !(fd == 1 || fd == 2) {
52
53 return write1(fd, p, n)
54 }
55
56
57
58
59 lock(&faketimeState.lock)
60
61
62
63
64 t := faketimeState.lastfaketime
65 if fd != faketimeState.lastfd {
66 t++
67 faketimeState.lastfd = fd
68 }
69 if faketime > t {
70 t = faketime
71 }
72 faketimeState.lastfaketime = t
73
74
75 var buf [4 + 8 + 4]byte
76 buf[2] = 'P'
77 buf[3] = 'B'
78 tu := uint64(t)
79 buf[4] = byte(tu >> (7 * 8))
80 buf[5] = byte(tu >> (6 * 8))
81 buf[6] = byte(tu >> (5 * 8))
82 buf[7] = byte(tu >> (4 * 8))
83 buf[8] = byte(tu >> (3 * 8))
84 buf[9] = byte(tu >> (2 * 8))
85 buf[10] = byte(tu >> (1 * 8))
86 buf[11] = byte(tu >> (0 * 8))
87 nu := uint32(n)
88 buf[12] = byte(nu >> (3 * 8))
89 buf[13] = byte(nu >> (2 * 8))
90 buf[14] = byte(nu >> (1 * 8))
91 buf[15] = byte(nu >> (0 * 8))
92 write1(fd, unsafe.Pointer(&buf[0]), int32(len(buf)))
93
94
95 res := write1(fd, p, n)
96
97 unlock(&faketimeState.lock)
98 return res
99 }
100
View as plain text