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);
117 // Delete timer t from the heap.
118 // Do not need to update the timerproc:
119 // if it wakes up early, no big deal.
125 runtime_lock(&timers);
127 // t may not be registered anymore and may have
128 // a bogus i (typically 0, if generated by Go).
129 // Verify it before proceeding.
131 if(i < 0 || i >= timers.len || timers.t[i] != t) {
132 runtime_unlock(&timers);
137 if(i == timers.len) {
140 timers.t[i] = timers.t[timers.len];
141 timers.t[timers.len] = nil;
146 runtime_unlock(&timers);
150 // Timerproc runs the time-driven events.
151 // It sleeps until the next event in the timers heap.
152 // If addtimer inserts a new earlier event, addtimer
153 // wakes timerproc early.
155 timerproc(void* dummy __attribute__ ((unused)))
159 void (*f)(int64, Eface);
163 runtime_lock(&timers);
164 now = runtime_nanotime();
166 if(timers.len == 0) {
171 delta = t->when - now;
175 // leave in heap but adjust next time to fire
176 t->when += t->period * (1 + -delta/t->period);
180 timers.t[0] = timers.t[--timers.len];
183 t->i = -1; // mark as removed
187 runtime_unlock(&timers);
189 runtime_raceacquire(t);
191 runtime_lock(&timers);
194 // No timers left - put goroutine to sleep.
195 timers.rescheduling = true;
196 runtime_park(runtime_unlock, &timers, "timer goroutine (idle)");
199 // At least one timer pending. Sleep until then.
200 timers.sleeping = true;
201 runtime_noteclear(&timers.waitnote);
202 runtime_unlock(&timers);
203 runtime_entersyscall();
204 runtime_notetsleep(&timers.waitnote, delta);
205 runtime_exitsyscall();
209 // heap maintenance algorithms.
219 p = (i-1)/2; // parent
220 if(t[i]->when >= t[p]->when)
240 c = i*2 + 1; // left child
244 if(c+1 < len && t[c+1]->when < t[c]->when)
246 if(t[c]->when >= t[i]->when)
258 runtime_time_scan(void (*addroot)(byte*, uintptr))
260 addroot((byte*)&timers, sizeof timers);