1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Time-related runtime and pieces of package time.
16 static void addtimer(Timer*);
17 static bool deltimer(Timer*);
20 // Godoc uses the comments in package time, not these.
22 // time.now is implemented in assembly.
24 // Sleep puts the current goroutine to sleep for at least ns nanoseconds.
25 func Sleep(ns int64) {
26 runtime_tsleep(ns, "sleep");
29 // startTimer adds t to the timer heap.
30 func startTimer(t *Timer) {
32 runtime_racerelease(t);
33 runtime_lock(&timers);
35 runtime_unlock(&timers);
38 // stopTimer removes t from the timer heap if it is there.
39 // It returns true if t was removed, false if t wasn't even there.
40 func stopTimer(t *Timer) (stopped bool) {
41 stopped = deltimer(t);
46 static void timerproc(void*);
47 static void siftup(int32);
48 static void siftdown(int32);
50 // Ready the goroutine e.data.
52 ready(int64 now, Eface e)
56 runtime_ready(e.__object);
59 // Put the current goroutine to sleep for ns nanoseconds.
61 runtime_tsleep(int64 ns, const char *reason)
71 t.when = runtime_nanotime() + ns;
75 runtime_lock(&timers);
77 runtime_park(runtime_unlock, &timers, reason);
80 // Add a timer to the heap and start or kick the timer proc
81 // if the new timer is earlier than any of the others.
88 if(timers.len >= timers.cap) {
93 nt = runtime_malloc(n*sizeof nt[0]);
94 runtime_memmove(nt, timers.t, timers.len*sizeof nt[0]);
95 runtime_free(timers.t);
103 // siftup moved to top: new earliest deadline.
104 if(timers.sleeping) {
105 timers.sleeping = false;
106 runtime_notewakeup(&timers.waitnote);
108 if(timers.rescheduling) {
109 timers.rescheduling = false;
110 runtime_ready(timers.timerproc);
113 if(timers.timerproc == nil) {
114 timers.timerproc = __go_go(timerproc, nil);
115 timers.timerproc->issystem = true;
119 // Delete timer t from the heap.
120 // Do not need to update the timerproc:
121 // if it wakes up early, no big deal.
127 runtime_lock(&timers);
129 // t may not be registered anymore and may have
130 // a bogus i (typically 0, if generated by Go).
131 // Verify it before proceeding.
133 if(i < 0 || i >= timers.len || timers.t[i] != t) {
134 runtime_unlock(&timers);
139 if(i == timers.len) {
142 timers.t[i] = timers.t[timers.len];
143 timers.t[timers.len] = nil;
148 runtime_unlock(&timers);
152 // Timerproc runs the time-driven events.
153 // It sleeps until the next event in the timers heap.
154 // If addtimer inserts a new earlier event, addtimer
155 // wakes timerproc early.
157 timerproc(void* dummy __attribute__ ((unused)))
161 void (*f)(int64, Eface);
165 runtime_lock(&timers);
166 now = runtime_nanotime();
168 if(timers.len == 0) {
173 delta = t->when - now;
177 // leave in heap but adjust next time to fire
178 t->when += t->period * (1 + -delta/t->period);
182 timers.t[0] = timers.t[--timers.len];
185 t->i = -1; // mark as removed
189 runtime_unlock(&timers);
191 runtime_raceacquire(t);
193 runtime_lock(&timers);
196 // No timers left - put goroutine to sleep.
197 timers.rescheduling = true;
198 runtime_park(runtime_unlock, &timers, "timer goroutine (idle)");
201 // At least one timer pending. Sleep until then.
202 timers.sleeping = true;
203 runtime_noteclear(&timers.waitnote);
204 runtime_unlock(&timers);
205 runtime_entersyscall();
206 runtime_notetsleep(&timers.waitnote, delta);
207 runtime_exitsyscall();
211 // heap maintenance algorithms.
221 p = (i-1)/2; // parent
222 if(t[i]->when >= t[p]->when)
242 c = i*2 + 1; // left child
246 if(c+1 < len && t[c+1]->when < t[c]->when)
248 if(t[c]->when >= t[i]->when)
260 runtime_time_scan(void (*addroot)(Obj))
262 addroot((Obj){(byte*)&timers, sizeof timers, 0});