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.
22 static void addtimer(Timer*);
23 static void dumptimers(const char*);
25 // nacl fake time support.
29 // Godoc uses the comments in package time, not these.
31 // time.now is implemented in assembly.
33 // runtimeNano returns the current value of the runtime clock in nanoseconds.
34 func runtimeNano() (ns int64) {
35 ns = runtime_nanotime();
38 // Sleep puts the current goroutine to sleep for at least ns nanoseconds.
39 func Sleep(ns int64) {
40 runtime_tsleep(ns, "sleep");
43 // startTimer adds t to the timer heap.
44 func startTimer(t *Timer) {
46 runtime_racerelease(t);
50 // stopTimer removes t from the timer heap if it is there.
51 // It returns true if t was removed, false if t wasn't even there.
52 func stopTimer(t *Timer) (stopped bool) {
53 stopped = runtime_deltimer(t);
58 int64 runtime_unixnanotime(void)
60 struct time_now_ret r;
63 return r.sec*1000000000 + r.nsec;
66 static void timerproc(void*);
67 static void siftup(int32);
68 static void siftdown(int32);
70 // Ready the goroutine e.data.
72 ready(int64 now, Eface e)
76 runtime_ready(e.__object);
79 static FuncVal readyv = {(void(*)(void))ready};
81 // Put the current goroutine to sleep for ns nanoseconds.
83 runtime_tsleep(int64 ns, const char *reason)
93 t.when = runtime_nanotime() + ns;
97 runtime_lock(&timers);
99 runtime_parkunlock(&timers, reason);
103 runtime_addtimer(Timer *t)
105 runtime_lock(&timers);
107 runtime_unlock(&timers);
110 // Add a timer to the heap and start or kick the timer proc
111 // if the new timer is earlier than any of the others.
118 // when must never be negative; otherwise timerproc will overflow
119 // during its delta calculation and never expire other timers.
121 t->when = (int64)((1ULL<<63)-1);
123 if(timers.len >= timers.cap) {
127 n = timers.cap*3 / 2;
128 nt = runtime_malloc(n*sizeof nt[0]);
129 runtime_memmove(nt, timers.t, timers.len*sizeof nt[0]);
130 runtime_free(timers.t);
138 // siftup moved to top: new earliest deadline.
139 if(timers.sleeping) {
140 timers.sleeping = false;
141 runtime_notewakeup(&timers.waitnote);
143 if(timers.rescheduling) {
144 timers.rescheduling = false;
145 runtime_ready(timers.timerproc);
148 if(timers.timerproc == nil) {
149 timers.timerproc = __go_go(timerproc, nil);
150 timers.timerproc->issystem = true;
153 dumptimers("addtimer");
156 // Used to force a dereference before the lock is acquired.
159 // Delete timer t from the heap.
160 // Do not need to update the timerproc:
161 // if it wakes up early, no big deal.
163 runtime_deltimer(Timer *t)
167 // Dereference t so that any panic happens before the lock is held.
168 // Discard result, because t might be moving in the heap.
172 runtime_lock(&timers);
174 // t may not be registered anymore and may have
175 // a bogus i (typically 0, if generated by Go).
176 // Verify it before proceeding.
178 if(i < 0 || i >= timers.len || timers.t[i] != t) {
179 runtime_unlock(&timers);
184 if(i == timers.len) {
187 timers.t[i] = timers.t[timers.len];
188 timers.t[timers.len] = nil;
194 dumptimers("deltimer");
195 runtime_unlock(&timers);
199 // Timerproc runs the time-driven events.
200 // It sleeps until the next event in the timers heap.
201 // If addtimer inserts a new earlier event, addtimer
202 // wakes timerproc early.
204 timerproc(void* dummy __attribute__ ((unused)))
209 void (*f)(int64, Eface);
213 runtime_lock(&timers);
214 timers.sleeping = false;
215 now = runtime_nanotime();
217 if(timers.len == 0) {
222 delta = t->when - now;
226 // leave in heap but adjust next time to fire
227 t->when += t->period * (1 + -delta/t->period);
231 timers.t[0] = timers.t[--timers.len];
234 t->i = -1; // mark as removed
237 f = (void*)t->fv->fn;
239 runtime_unlock(&timers);
241 runtime_raceacquire(t);
242 __go_set_closure(fv);
245 // clear f and arg to avoid leak while sleeping for next timer
248 arg.__type_descriptor = nil;
252 runtime_lock(&timers);
255 // No timers left - put goroutine to sleep.
256 timers.rescheduling = true;
257 runtime_g()->isbackground = true;
258 runtime_parkunlock(&timers, "timer goroutine (idle)");
259 runtime_g()->isbackground = false;
262 // At least one timer pending. Sleep until then.
263 timers.sleeping = true;
264 runtime_noteclear(&timers.waitnote);
265 runtime_unlock(&timers);
266 runtime_notetsleepg(&timers.waitnote, delta);
270 // heap maintenance algorithms.
283 p = (i-1)/4; // parent
284 if(when >= t[p]->when)
306 c = i*4 + 1; // left child
307 c3 = c + 2; // mid child
312 if(c+1 < len && t[c+1]->when < w) {
318 if(c3+1 < len && t[c3+1]->when < w3) {
338 dumptimers(const char *msg)
343 runtime_printf("timers: %s\n", msg);
344 for(i = 0; i < timers.len; i++) {
346 runtime_printf("\t%d\t%p:\ti %d when %D period %D fn %p\n",
347 i, t, t->i, t->when, t->period, t->fv->fn);
349 runtime_printf("\n");
353 runtime_time_scan(struct Workbuf** wbufp, void (*enqueue1)(struct Workbuf**, Obj))
355 enqueue1(wbufp, (Obj){(byte*)&timers, sizeof timers, 0});