LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Time-related runtime and pieces of package time. | 5 // Time-related runtime and pieces of package time. |
6 | 6 |
7 package time | 7 package time |
8 | 8 |
9 #include "runtime.h" | 9 #include "runtime.h" |
10 #include "defs_GOOS_GOARCH.h" | 10 #include "defs_GOOS_GOARCH.h" |
11 #include "os_GOOS.h" | 11 #include "os_GOOS.h" |
12 #include "arch_GOARCH.h" | 12 #include "arch_GOARCH.h" |
13 #include "malloc.h" | 13 #include "malloc.h" |
14 #include "race.h" | 14 #include "race.h" |
15 | 15 |
16 static Timers timers; | 16 static Timers timers; |
17 static void addtimer(Timer*); | 17 static void addtimer(Timer*); |
18 static bool deltimer(Timer*); | |
19 | 18 |
20 // Package time APIs. | 19 // Package time APIs. |
21 // Godoc uses the comments in package time, not these. | 20 // Godoc uses the comments in package time, not these. |
22 | 21 |
23 // time.now is implemented in assembly. | 22 // time.now is implemented in assembly. |
24 | 23 |
25 // Sleep puts the current goroutine to sleep for at least ns nanoseconds. | 24 // Sleep puts the current goroutine to sleep for at least ns nanoseconds. |
26 func Sleep(ns int64) { | 25 func Sleep(ns int64) { |
27 runtime·tsleep(ns, "sleep"); | 26 runtime·tsleep(ns, "sleep"); |
28 } | 27 } |
29 | 28 |
30 // startTimer adds t to the timer heap. | 29 // startTimer adds t to the timer heap. |
31 func startTimer(t *Timer) { | 30 func startTimer(t *Timer) { |
| 31 runtime·addtimer(t); |
| 32 } |
| 33 |
| 34 void |
| 35 runtime·addtimer(Timer *t) |
| 36 { |
32 if(raceenabled) | 37 if(raceenabled) |
33 runtime·racerelease(t); | 38 runtime·racerelease(t); |
34 runtime·lock(&timers); | 39 runtime·lock(&timers); |
35 addtimer(t); | 40 addtimer(t); |
36 runtime·unlock(&timers); | 41 runtime·unlock(&timers); |
37 } | 42 } |
38 | 43 |
39 // stopTimer removes t from the timer heap if it is there. | 44 // stopTimer removes t from the timer heap if it is there. |
40 // It returns true if t was removed, false if t wasn't even there. | 45 // It returns true if t was removed, false if t wasn't even there. |
41 func stopTimer(t *Timer) (stopped bool) { | 46 func stopTimer(t *Timer) (stopped bool) { |
42 » stopped = deltimer(t); | 47 » stopped = runtime·deltimer(t); |
43 } | 48 } |
44 | 49 |
45 // C runtime. | 50 // C runtime. |
46 | 51 |
47 static void timerproc(void); | 52 static void timerproc(void); |
48 static void siftup(int32); | 53 static void siftup(int32); |
49 static void siftdown(int32); | 54 static void siftdown(int32); |
50 | 55 |
51 // Ready the goroutine e.data. | 56 // Ready the goroutine e.data. |
52 static void | 57 static void |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 runtime·ready(timers.timerproc); | 113 runtime·ready(timers.timerproc); |
109 } | 114 } |
110 } | 115 } |
111 if(timers.timerproc == nil) | 116 if(timers.timerproc == nil) |
112 timers.timerproc = runtime·newproc1((byte*)timerproc, nil, 0, 0,
addtimer); | 117 timers.timerproc = runtime·newproc1((byte*)timerproc, nil, 0, 0,
addtimer); |
113 } | 118 } |
114 | 119 |
115 // Delete timer t from the heap. | 120 // Delete timer t from the heap. |
116 // Do not need to update the timerproc: | 121 // Do not need to update the timerproc: |
117 // if it wakes up early, no big deal. | 122 // if it wakes up early, no big deal. |
118 static bool | 123 bool |
119 deltimer(Timer *t) | 124 runtime·deltimer(Timer *t) |
120 { | 125 { |
121 int32 i; | 126 int32 i; |
122 | 127 |
123 runtime·lock(&timers); | 128 runtime·lock(&timers); |
124 | 129 |
125 // t may not be registered anymore and may have | 130 // t may not be registered anymore and may have |
126 // a bogus i (typically 0, if generated by Go). | 131 // a bogus i (typically 0, if generated by Go). |
127 // Verify it before proceeding. | 132 // Verify it before proceeding. |
128 i = t->i; | 133 i = t->i; |
129 if(i < 0 || i >= timers.len || timers.t[i] != t) { | 134 if(i < 0 || i >= timers.len || timers.t[i] != t) { |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 if(t[c]->when >= t[i]->when) | 249 if(t[c]->when >= t[i]->when) |
245 break; | 250 break; |
246 tmp = t[i]; | 251 tmp = t[i]; |
247 t[i] = t[c]; | 252 t[i] = t[c]; |
248 t[c] = tmp; | 253 t[c] = tmp; |
249 t[i]->i = i; | 254 t[i]->i = i; |
250 t[c]->i = c; | 255 t[c]->i = c; |
251 i = c; | 256 i = c; |
252 } | 257 } |
253 } | 258 } |
LEFT | RIGHT |