OLD | NEW |
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 runtime | 7 package runtime |
8 | 8 |
9 import "unsafe" | 9 import "unsafe" |
10 | 10 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 | 42 |
43 // time.now is implemented in assembly. | 43 // time.now is implemented in assembly. |
44 | 44 |
45 // Sleep puts the current goroutine to sleep for at least ns nanoseconds. | 45 // Sleep puts the current goroutine to sleep for at least ns nanoseconds. |
46 func timeSleep(ns int64) { | 46 func timeSleep(ns int64) { |
47 if ns <= 0 { | 47 if ns <= 0 { |
48 return | 48 return |
49 } | 49 } |
50 | 50 |
51 t := new(timer) | 51 t := new(timer) |
52 » t.when = gonanotime() + ns | 52 » t.when = nanotime() + ns |
53 t.f = goroutineReady | 53 t.f = goroutineReady |
54 t.arg = getg() | 54 t.arg = getg() |
55 golock(&timers.lock) | 55 golock(&timers.lock) |
56 addtimerLocked(t) | 56 addtimerLocked(t) |
57 goparkunlock(&timers.lock, "sleep") | 57 goparkunlock(&timers.lock, "sleep") |
58 } | 58 } |
59 | 59 |
60 // startTimer adds t to the timer heap. | 60 // startTimer adds t to the timer heap. |
61 func startTimer(t *timer) { | 61 func startTimer(t *timer) { |
62 if raceenabled { | 62 if raceenabled { |
(...skipping 30 matching lines...) Expand all Loading... |
93 if t.when < 0 { | 93 if t.when < 0 { |
94 t.when = 1<<63 - 1 | 94 t.when = 1<<63 - 1 |
95 } | 95 } |
96 t.i = len(timers.t) | 96 t.i = len(timers.t) |
97 timers.t = append(timers.t, t) | 97 timers.t = append(timers.t, t) |
98 siftupTimer(t.i) | 98 siftupTimer(t.i) |
99 if t.i == 0 { | 99 if t.i == 0 { |
100 // siftup moved to top: new earliest deadline. | 100 // siftup moved to top: new earliest deadline. |
101 if timers.sleeping { | 101 if timers.sleeping { |
102 timers.sleeping = false | 102 timers.sleeping = false |
103 » » » gonotewakeup(&timers.waitnote) | 103 » » » notewakeup(&timers.waitnote) |
104 } | 104 } |
105 if timers.rescheduling { | 105 if timers.rescheduling { |
106 timers.rescheduling = false | 106 timers.rescheduling = false |
107 goready(timers.gp) | 107 goready(timers.gp) |
108 } | 108 } |
109 } | 109 } |
110 if !timers.created { | 110 if !timers.created { |
111 timers.created = true | 111 timers.created = true |
112 go timerproc() | 112 go timerproc() |
113 } | 113 } |
(...skipping 28 matching lines...) Expand all Loading... |
142 } | 142 } |
143 gounlock(&timers.lock) | 143 gounlock(&timers.lock) |
144 return true | 144 return true |
145 } | 145 } |
146 | 146 |
147 // Timerproc runs the time-driven events. | 147 // Timerproc runs the time-driven events. |
148 // It sleeps until the next event in the timers heap. | 148 // It sleeps until the next event in the timers heap. |
149 // If addtimer inserts a new earlier event, addtimer1 wakes timerproc early. | 149 // If addtimer inserts a new earlier event, addtimer1 wakes timerproc early. |
150 func timerproc() { | 150 func timerproc() { |
151 timers.gp = getg() | 151 timers.gp = getg() |
152 » timers.gp.issystem = 1 | 152 » timers.gp.issystem = true |
153 for { | 153 for { |
154 golock(&timers.lock) | 154 golock(&timers.lock) |
155 timers.sleeping = false | 155 timers.sleeping = false |
156 » » now := gonanotime() | 156 » » now := nanotime() |
157 delta := int64(-1) | 157 delta := int64(-1) |
158 for { | 158 for { |
159 if len(timers.t) == 0 { | 159 if len(timers.t) == 0 { |
160 delta = -1 | 160 delta = -1 |
161 break | 161 break |
162 } | 162 } |
163 t := timers.t[0] | 163 t := timers.t[0] |
164 delta = t.when - now | 164 delta = t.when - now |
165 if delta > 0 { | 165 if delta > 0 { |
166 break | 166 break |
(...skipping 26 matching lines...) Expand all Loading... |
193 golock(&timers.lock) | 193 golock(&timers.lock) |
194 } | 194 } |
195 if delta < 0 { | 195 if delta < 0 { |
196 // No timers left - put goroutine to sleep. | 196 // No timers left - put goroutine to sleep. |
197 timers.rescheduling = true | 197 timers.rescheduling = true |
198 goparkunlock(&timers.lock, "timer goroutine (idle)") | 198 goparkunlock(&timers.lock, "timer goroutine (idle)") |
199 continue | 199 continue |
200 } | 200 } |
201 // At least one timer pending. Sleep until then. | 201 // At least one timer pending. Sleep until then. |
202 timers.sleeping = true | 202 timers.sleeping = true |
203 » » gonoteclear(&timers.waitnote) | 203 » » noteclear(&timers.waitnote) |
204 gounlock(&timers.lock) | 204 gounlock(&timers.lock) |
205 » » gonotetsleepg(&timers.waitnote, delta) | 205 » » notetsleepg(&timers.waitnote, delta) |
206 } | 206 } |
207 } | 207 } |
208 | 208 |
209 // Heap maintenance algorithms. | 209 // Heap maintenance algorithms. |
210 | 210 |
211 func siftupTimer(i int) { | 211 func siftupTimer(i int) { |
212 t := timers.t | 212 t := timers.t |
213 when := t[i].when | 213 when := t[i].when |
214 tmp := t[i] | 214 tmp := t[i] |
215 for i > 0 { | 215 for i > 0 { |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 if w >= when { | 255 if w >= when { |
256 break | 256 break |
257 } | 257 } |
258 t[i] = t[c] | 258 t[i] = t[c] |
259 t[i].i = i | 259 t[i].i = i |
260 t[c] = tmp | 260 t[c] = tmp |
261 t[c].i = c | 261 t[c].i = c |
262 i = c | 262 i = c |
263 } | 263 } |
264 } | 264 } |
OLD | NEW |