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.
21 static void addtimer(Timer*);
22 static void dumptimers(const char*);
24 // nacl fake time support.
28 // Godoc uses the comments in package time, not these.
30 // time.now is implemented in assembly.
32 // runtimeNano returns the current value of the runtime clock in nanoseconds.
33 func runtimeNano() (ns int64) {
34 ns = runtime_nanotime();
37 // Sleep puts the current goroutine to sleep for at least ns nanoseconds.
38 func Sleep(ns int64) {
39 runtime_tsleep(ns, "sleep");
42 // startTimer adds t to the timer heap.
43 func startTimer(t *Timer) {
47 // stopTimer removes t from the timer heap if it is there.
48 // It returns true if t was removed, false if t wasn't even there.
49 func stopTimer(t *Timer) (stopped bool) {
50 stopped = runtime_deltimer(t);
55 int64 runtime_unixnanotime(void)
57 struct time_now_ret r;
60 return r.sec*1000000000 + r.nsec;
63 static void timerproc(void*);
64 static void siftup(int32);
65 static void siftdown(int32);
67 // Ready the goroutine e.data.
69 ready(int64 now, Eface e)
73 runtime_ready(e.__object);
76 static FuncVal readyv = {(void(*)(void))ready};
78 // Put the current goroutine to sleep for ns nanoseconds.
80 runtime_tsleep(int64 ns, const char *reason)
90 t.when = runtime_nanotime() + ns;
94 runtime_lock(&timers);
96 runtime_parkunlock(&timers, reason);
100 runtime_addtimer(Timer *t)
102 runtime_lock(&timers);
104 runtime_unlock(&timers);
107 // Add a timer to the heap and start or kick the timer proc
108 // if the new timer is earlier than any of the others.
115 // when must never be negative; otherwise timerproc will overflow
116 // during its delta calculation and never expire other timers.
118 t->when = (int64)((1ULL<<63)-1);
120 if(timers.len >= timers.cap) {
124 n = timers.cap*3 / 2;
125 nt = runtime_malloc(n*sizeof nt[0]);
126 runtime_memmove(nt, timers.t, timers.len*sizeof nt[0]);
127 runtime_free(timers.t);
135 // siftup moved to top: new earliest deadline.
136 if(timers.sleeping) {
137 timers.sleeping = false;
138 runtime_notewakeup(&timers.waitnote);
140 if(timers.rescheduling) {
141 timers.rescheduling = false;
142 runtime_ready(timers.timerproc);
145 if(timers.timerproc == nil) {
146 timers.timerproc = __go_go(timerproc, nil);
147 timers.timerproc->issystem = true;
150 dumptimers("addtimer");
153 // Used to force a dereference before the lock is acquired.
156 // Delete timer t from the heap.
157 // Do not need to update the timerproc:
158 // if it wakes up early, no big deal.
160 runtime_deltimer(Timer *t)
164 // Dereference t so that any panic happens before the lock is held.
165 // Discard result, because t might be moving in the heap.
169 runtime_lock(&timers);
171 // t may not be registered anymore and may have
172 // a bogus i (typically 0, if generated by Go).
173 // Verify it before proceeding.
175 if(i < 0 || i >= timers.len || timers.t[i] != t) {
176 runtime_unlock(&timers);
181 if(i == timers.len) {
184 timers.t[i] = timers.t[timers.len];
185 timers.t[timers.len] = nil;
191 dumptimers("deltimer");
192 runtime_unlock(&timers);
196 // Timerproc runs the time-driven events.
197 // It sleeps until the next event in the timers heap.
198 // If addtimer inserts a new earlier event, addtimer
199 // wakes timerproc early.
201 timerproc(void* dummy __attribute__ ((unused)))
206 void (*f)(int64, Eface);
210 runtime_lock(&timers);
211 timers.sleeping = false;
212 now = runtime_nanotime();
214 if(timers.len == 0) {
219 delta = t->when - now;
223 // leave in heap but adjust next time to fire
224 t->when += t->period * (1 + -delta/t->period);
228 timers.t[0] = timers.t[--timers.len];
231 t->i = -1; // mark as removed
234 f = (void*)t->fv->fn;
236 runtime_unlock(&timers);
237 __go_set_closure(fv);
240 // clear f and arg to avoid leak while sleeping for next timer
243 arg.__type_descriptor = nil;
247 runtime_lock(&timers);
250 // No timers left - put goroutine to sleep.
251 timers.rescheduling = true;
252 runtime_g()->isbackground = true;
253 runtime_parkunlock(&timers, "timer goroutine (idle)");
254 runtime_g()->isbackground = false;
257 // At least one timer pending. Sleep until then.
258 timers.sleeping = true;
259 runtime_noteclear(&timers.waitnote);
260 runtime_unlock(&timers);
261 runtime_notetsleepg(&timers.waitnote, delta);
265 // heap maintenance algorithms.
278 p = (i-1)/4; // parent
279 if(when >= t[p]->when)
301 c = i*4 + 1; // left child
302 c3 = c + 2; // mid child
307 if(c+1 < len && t[c+1]->when < w) {
313 if(c3+1 < len && t[c3+1]->when < w3) {
333 dumptimers(const char *msg)
338 runtime_printf("timers: %s\n", msg);
339 for(i = 0; i < timers.len; i++) {
341 runtime_printf("\t%d\t%p:\ti %d when %D period %D fn %p\n",
342 i, t, t->i, t->when, t->period, t->fv->fn);
344 runtime_printf("\n");
348 runtime_time_scan(struct Workbuf** wbufp, void (*enqueue1)(struct Workbuf**, Obj))
350 enqueue1(wbufp, (Obj){(byte*)&timers, sizeof timers, 0});