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.
15 static void addtimer(Timer*);
16 static bool deltimer(Timer*);
19 // Godoc uses the comments in package time, not these.
21 // time.now is implemented in assembly.
23 // Sleep puts the current goroutine to sleep for at least ns nanoseconds.
24 func Sleep(ns int64) {
29 g->waitreason = "sleep";
33 // startTimer adds t to the timer heap.
34 func startTimer(t *Timer) {
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.
60 // The caller must have set g->status and g->waitreason.
62 runtime_tsleep(int64 ns)
69 t.when = runtime_nanotime() + ns;
72 t.arg.__object = runtime_g();
77 // Add a timer to the heap and start or kick the timer proc
78 // if the new timer is earlier than any of the others.
85 runtime_lock(&timers);
86 if(timers.len >= timers.cap) {
91 nt = runtime_malloc(n*sizeof nt[0]);
92 runtime_memmove(nt, timers.t, timers.len*sizeof nt[0]);
93 runtime_free(timers.t);
101 // siftup moved to top: new earliest deadline.
102 if(timers.sleeping) {
103 timers.sleeping = false;
104 runtime_notewakeup(&timers.waitnote);
106 if(timers.rescheduling) {
107 timers.rescheduling = false;
108 runtime_ready(timers.timerproc);
111 if(timers.timerproc == nil)
112 timers.timerproc = __go_go(timerproc, nil);
113 runtime_unlock(&timers);
116 // Delete timer t from the heap.
117 // Do not need to update the timerproc:
118 // if it wakes up early, no big deal.
124 runtime_lock(&timers);
126 // t may not be registered anymore and may have
127 // a bogus i (typically 0, if generated by Go).
128 // Verify it before proceeding.
130 if(i < 0 || i >= timers.len || timers.t[i] != t) {
131 runtime_unlock(&timers);
136 if(i == timers.len) {
139 timers.t[i] = timers.t[timers.len];
140 timers.t[timers.len] = nil;
145 runtime_unlock(&timers);
149 // Timerproc runs the time-driven events.
150 // It sleeps until the next event in the timers heap.
151 // If addtimer inserts a new earlier event, addtimer
152 // wakes timerproc early.
154 timerproc(void* dummy __attribute__ ((unused)))
159 void (*f)(int64, Eface);
164 runtime_lock(&timers);
165 now = runtime_nanotime();
167 if(timers.len == 0) {
172 delta = t->when - now;
176 // leave in heap but adjust next time to fire
177 t->when += t->period * (1 + -delta/t->period);
181 timers.t[0] = timers.t[--timers.len];
184 t->i = -1; // mark as removed
188 runtime_unlock(&timers);
190 runtime_lock(&timers);
193 // No timers left - put goroutine to sleep.
194 timers.rescheduling = true;
195 g->status = Gwaiting;
196 g->waitreason = "timer goroutine (idle)";
197 runtime_unlock(&timers);
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 (*scan)(byte*, int64))
262 scan((byte*)&timers, sizeof timers);