2011-12-07 02:11:29 +01:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2011-12-13 00:40:51 +01:00
|
|
|
// Time-related runtime and pieces of package time.
|
2011-12-07 02:11:29 +01:00
|
|
|
|
|
|
|
package time
|
|
|
|
|
2014-07-19 10:53:52 +02:00
|
|
|
#include <sys/time.h>
|
|
|
|
|
2011-12-07 02:11:29 +01:00
|
|
|
#include "runtime.h"
|
2011-12-13 00:40:51 +01:00
|
|
|
#include "defs.h"
|
|
|
|
#include "arch.h"
|
|
|
|
#include "malloc.h"
|
2012-10-23 06:31:11 +02:00
|
|
|
#include "race.h"
|
2011-12-07 02:11:29 +01:00
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
enum {
|
|
|
|
debug = 0,
|
|
|
|
};
|
|
|
|
|
2011-12-13 00:40:51 +01:00
|
|
|
static Timers timers;
|
|
|
|
static void addtimer(Timer*);
|
2013-11-06 20:49:01 +01:00
|
|
|
static void dumptimers(const char*);
|
2011-12-13 00:40:51 +01:00
|
|
|
|
2014-07-19 10:53:52 +02:00
|
|
|
// nacl fake time support.
|
|
|
|
int64 runtime_timens;
|
|
|
|
|
2011-12-13 00:40:51 +01:00
|
|
|
// Package time APIs.
|
|
|
|
// Godoc uses the comments in package time, not these.
|
|
|
|
|
2011-12-13 20:16:27 +01:00
|
|
|
// time.now is implemented in assembly.
|
2011-12-13 00:40:51 +01:00
|
|
|
|
2014-07-19 10:53:52 +02:00
|
|
|
// runtimeNano returns the current value of the runtime clock in nanoseconds.
|
|
|
|
func runtimeNano() (ns int64) {
|
|
|
|
ns = runtime_nanotime();
|
|
|
|
}
|
|
|
|
|
2011-12-13 00:40:51 +01:00
|
|
|
// Sleep puts the current goroutine to sleep for at least ns nanoseconds.
|
|
|
|
func Sleep(ns int64) {
|
2012-10-23 06:31:11 +02:00
|
|
|
runtime_tsleep(ns, "sleep");
|
2011-12-13 00:40:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// startTimer adds t to the timer heap.
|
|
|
|
func startTimer(t *Timer) {
|
2012-10-23 06:31:11 +02:00
|
|
|
if(raceenabled)
|
|
|
|
runtime_racerelease(t);
|
2013-07-16 08:54:42 +02:00
|
|
|
runtime_addtimer(t);
|
2011-12-13 00:40:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// stopTimer removes t from the timer heap if it is there.
|
|
|
|
// It returns true if t was removed, false if t wasn't even there.
|
|
|
|
func stopTimer(t *Timer) (stopped bool) {
|
2013-07-16 08:54:42 +02:00
|
|
|
stopped = runtime_deltimer(t);
|
2011-12-13 00:40:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// C runtime.
|
|
|
|
|
2014-07-19 10:53:52 +02:00
|
|
|
int64 runtime_unixnanotime(void)
|
|
|
|
{
|
|
|
|
struct time_now_ret r;
|
|
|
|
|
|
|
|
r = now();
|
|
|
|
return r.sec*1000000000 + r.nsec;
|
|
|
|
}
|
|
|
|
|
2011-12-13 00:40:51 +01:00
|
|
|
static void timerproc(void*);
|
|
|
|
static void siftup(int32);
|
|
|
|
static void siftdown(int32);
|
|
|
|
|
|
|
|
// Ready the goroutine e.data.
|
|
|
|
static void
|
2013-09-03 23:52:37 +02:00
|
|
|
ready(int64 now, Eface e)
|
2011-12-13 00:40:51 +01:00
|
|
|
{
|
|
|
|
USED(now);
|
|
|
|
|
|
|
|
runtime_ready(e.__object);
|
|
|
|
}
|
|
|
|
|
2013-06-19 01:49:49 +02:00
|
|
|
static FuncVal readyv = {(void(*)(void))ready};
|
|
|
|
|
2011-12-13 00:40:51 +01:00
|
|
|
// Put the current goroutine to sleep for ns nanoseconds.
|
|
|
|
void
|
2012-10-23 06:31:11 +02:00
|
|
|
runtime_tsleep(int64 ns, const char *reason)
|
2011-12-13 00:40:51 +01:00
|
|
|
{
|
2012-06-25 18:20:03 +02:00
|
|
|
G* g;
|
2011-12-13 00:40:51 +01:00
|
|
|
Timer t;
|
|
|
|
|
2012-06-25 18:20:03 +02:00
|
|
|
g = runtime_g();
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
if(ns <= 0)
|
2011-12-13 00:40:51 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
t.when = runtime_nanotime() + ns;
|
|
|
|
t.period = 0;
|
2013-06-19 01:49:49 +02:00
|
|
|
t.fv = &readyv;
|
2012-06-25 18:20:03 +02:00
|
|
|
t.arg.__object = g;
|
2012-10-23 06:31:11 +02:00
|
|
|
runtime_lock(&timers);
|
2011-12-13 00:40:51 +01:00
|
|
|
addtimer(&t);
|
2014-06-07 00:37:27 +02:00
|
|
|
runtime_parkunlock(&timers, reason);
|
2011-12-13 00:40:51 +01:00
|
|
|
}
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
void
|
|
|
|
runtime_addtimer(Timer *t)
|
|
|
|
{
|
|
|
|
runtime_lock(&timers);
|
|
|
|
addtimer(t);
|
|
|
|
runtime_unlock(&timers);
|
|
|
|
}
|
|
|
|
|
2011-12-13 00:40:51 +01:00
|
|
|
// Add a timer to the heap and start or kick the timer proc
|
|
|
|
// if the new timer is earlier than any of the others.
|
|
|
|
static void
|
|
|
|
addtimer(Timer *t)
|
|
|
|
{
|
|
|
|
int32 n;
|
|
|
|
Timer **nt;
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
// when must never be negative; otherwise timerproc will overflow
|
|
|
|
// during its delta calculation and never expire other timers.
|
|
|
|
if(t->when < 0)
|
|
|
|
t->when = (int64)((1ULL<<63)-1);
|
|
|
|
|
2011-12-13 00:40:51 +01:00
|
|
|
if(timers.len >= timers.cap) {
|
|
|
|
// Grow slice.
|
|
|
|
n = 16;
|
|
|
|
if(n <= timers.cap)
|
|
|
|
n = timers.cap*3 / 2;
|
|
|
|
nt = runtime_malloc(n*sizeof nt[0]);
|
|
|
|
runtime_memmove(nt, timers.t, timers.len*sizeof nt[0]);
|
|
|
|
runtime_free(timers.t);
|
|
|
|
timers.t = nt;
|
|
|
|
timers.cap = n;
|
|
|
|
}
|
|
|
|
t->i = timers.len++;
|
|
|
|
timers.t[t->i] = t;
|
|
|
|
siftup(t->i);
|
|
|
|
if(t->i == 0) {
|
|
|
|
// siftup moved to top: new earliest deadline.
|
|
|
|
if(timers.sleeping) {
|
|
|
|
timers.sleeping = false;
|
|
|
|
runtime_notewakeup(&timers.waitnote);
|
|
|
|
}
|
|
|
|
if(timers.rescheduling) {
|
|
|
|
timers.rescheduling = false;
|
|
|
|
runtime_ready(timers.timerproc);
|
|
|
|
}
|
|
|
|
}
|
2013-01-30 02:37:13 +01:00
|
|
|
if(timers.timerproc == nil) {
|
2011-12-13 00:40:51 +01:00
|
|
|
timers.timerproc = __go_go(timerproc, nil);
|
2013-01-30 02:37:13 +01:00
|
|
|
timers.timerproc->issystem = true;
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
if(debug)
|
|
|
|
dumptimers("addtimer");
|
2011-12-13 00:40:51 +01:00
|
|
|
}
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
// Used to force a dereference before the lock is acquired.
|
|
|
|
static int32 gi;
|
|
|
|
|
2011-12-13 00:40:51 +01:00
|
|
|
// Delete timer t from the heap.
|
|
|
|
// Do not need to update the timerproc:
|
|
|
|
// if it wakes up early, no big deal.
|
2013-07-16 08:54:42 +02:00
|
|
|
bool
|
|
|
|
runtime_deltimer(Timer *t)
|
2011-12-13 00:40:51 +01:00
|
|
|
{
|
|
|
|
int32 i;
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
// Dereference t so that any panic happens before the lock is held.
|
|
|
|
// Discard result, because t might be moving in the heap.
|
|
|
|
i = t->i;
|
|
|
|
gi = i;
|
|
|
|
|
2011-12-13 00:40:51 +01:00
|
|
|
runtime_lock(&timers);
|
|
|
|
|
|
|
|
// t may not be registered anymore and may have
|
|
|
|
// a bogus i (typically 0, if generated by Go).
|
|
|
|
// Verify it before proceeding.
|
|
|
|
i = t->i;
|
|
|
|
if(i < 0 || i >= timers.len || timers.t[i] != t) {
|
|
|
|
runtime_unlock(&timers);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
timers.len--;
|
|
|
|
if(i == timers.len) {
|
|
|
|
timers.t[i] = nil;
|
|
|
|
} else {
|
|
|
|
timers.t[i] = timers.t[timers.len];
|
|
|
|
timers.t[timers.len] = nil;
|
|
|
|
timers.t[i]->i = i;
|
|
|
|
siftup(i);
|
|
|
|
siftdown(i);
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
if(debug)
|
|
|
|
dumptimers("deltimer");
|
2011-12-13 00:40:51 +01:00
|
|
|
runtime_unlock(&timers);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Timerproc runs the time-driven events.
|
|
|
|
// It sleeps until the next event in the timers heap.
|
|
|
|
// If addtimer inserts a new earlier event, addtimer
|
|
|
|
// wakes timerproc early.
|
|
|
|
static void
|
|
|
|
timerproc(void* dummy __attribute__ ((unused)))
|
|
|
|
{
|
|
|
|
int64 delta, now;
|
|
|
|
Timer *t;
|
2014-07-19 10:53:52 +02:00
|
|
|
FuncVal *fv;
|
2013-09-03 23:52:37 +02:00
|
|
|
void (*f)(int64, Eface);
|
2011-12-13 00:40:51 +01:00
|
|
|
Eface arg;
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
runtime_lock(&timers);
|
2013-11-06 20:49:01 +01:00
|
|
|
timers.sleeping = false;
|
2011-12-13 00:40:51 +01:00
|
|
|
now = runtime_nanotime();
|
|
|
|
for(;;) {
|
|
|
|
if(timers.len == 0) {
|
|
|
|
delta = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
t = timers.t[0];
|
|
|
|
delta = t->when - now;
|
|
|
|
if(delta > 0)
|
|
|
|
break;
|
|
|
|
if(t->period > 0) {
|
|
|
|
// leave in heap but adjust next time to fire
|
|
|
|
t->when += t->period * (1 + -delta/t->period);
|
|
|
|
siftdown(0);
|
|
|
|
} else {
|
|
|
|
// remove from heap
|
|
|
|
timers.t[0] = timers.t[--timers.len];
|
|
|
|
timers.t[0]->i = 0;
|
|
|
|
siftdown(0);
|
|
|
|
t->i = -1; // mark as removed
|
|
|
|
}
|
2014-07-19 10:53:52 +02:00
|
|
|
fv = t->fv;
|
2013-06-19 01:49:49 +02:00
|
|
|
f = (void*)t->fv->fn;
|
2011-12-13 00:40:51 +01:00
|
|
|
arg = t->arg;
|
|
|
|
runtime_unlock(&timers);
|
2012-10-23 06:31:11 +02:00
|
|
|
if(raceenabled)
|
|
|
|
runtime_raceacquire(t);
|
2014-07-19 10:53:52 +02:00
|
|
|
__go_set_closure(fv);
|
2013-09-03 23:52:37 +02:00
|
|
|
f(now, arg);
|
2014-06-07 00:37:27 +02:00
|
|
|
|
|
|
|
// clear f and arg to avoid leak while sleeping for next timer
|
|
|
|
f = nil;
|
|
|
|
USED(f);
|
|
|
|
arg.__type_descriptor = nil;
|
|
|
|
arg.__object = nil;
|
|
|
|
USED(&arg);
|
|
|
|
|
2011-12-13 00:40:51 +01:00
|
|
|
runtime_lock(&timers);
|
|
|
|
}
|
|
|
|
if(delta < 0) {
|
|
|
|
// No timers left - put goroutine to sleep.
|
|
|
|
timers.rescheduling = true;
|
2014-07-19 10:53:52 +02:00
|
|
|
runtime_g()->isbackground = true;
|
2014-06-07 00:37:27 +02:00
|
|
|
runtime_parkunlock(&timers, "timer goroutine (idle)");
|
2014-07-19 10:53:52 +02:00
|
|
|
runtime_g()->isbackground = false;
|
2011-12-13 00:40:51 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// At least one timer pending. Sleep until then.
|
|
|
|
timers.sleeping = true;
|
|
|
|
runtime_noteclear(&timers.waitnote);
|
|
|
|
runtime_unlock(&timers);
|
2013-11-06 20:49:01 +01:00
|
|
|
runtime_notetsleepg(&timers.waitnote, delta);
|
2011-12-13 00:40:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// heap maintenance algorithms.
|
|
|
|
|
|
|
|
static void
|
|
|
|
siftup(int32 i)
|
|
|
|
{
|
|
|
|
int32 p;
|
2013-11-06 20:49:01 +01:00
|
|
|
int64 when;
|
2011-12-13 00:40:51 +01:00
|
|
|
Timer **t, *tmp;
|
|
|
|
|
|
|
|
t = timers.t;
|
2013-11-06 20:49:01 +01:00
|
|
|
when = t[i]->when;
|
|
|
|
tmp = t[i];
|
2011-12-13 00:40:51 +01:00
|
|
|
while(i > 0) {
|
2013-11-06 20:49:01 +01:00
|
|
|
p = (i-1)/4; // parent
|
|
|
|
if(when >= t[p]->when)
|
2011-12-13 00:40:51 +01:00
|
|
|
break;
|
|
|
|
t[i] = t[p];
|
|
|
|
t[i]->i = i;
|
2013-11-06 20:49:01 +01:00
|
|
|
t[p] = tmp;
|
|
|
|
tmp->i = p;
|
2011-12-13 00:40:51 +01:00
|
|
|
i = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
siftdown(int32 i)
|
|
|
|
{
|
2013-11-06 20:49:01 +01:00
|
|
|
int32 c, c3, len;
|
|
|
|
int64 when, w, w3;
|
2011-12-13 00:40:51 +01:00
|
|
|
Timer **t, *tmp;
|
|
|
|
|
|
|
|
t = timers.t;
|
|
|
|
len = timers.len;
|
2013-11-06 20:49:01 +01:00
|
|
|
when = t[i]->when;
|
|
|
|
tmp = t[i];
|
2011-12-13 00:40:51 +01:00
|
|
|
for(;;) {
|
2013-11-06 20:49:01 +01:00
|
|
|
c = i*4 + 1; // left child
|
|
|
|
c3 = c + 2; // mid child
|
2011-12-13 00:40:51 +01:00
|
|
|
if(c >= len) {
|
|
|
|
break;
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
w = t[c]->when;
|
|
|
|
if(c+1 < len && t[c+1]->when < w) {
|
|
|
|
w = t[c+1]->when;
|
2011-12-13 00:40:51 +01:00
|
|
|
c++;
|
2013-11-06 20:49:01 +01:00
|
|
|
}
|
|
|
|
if(c3 < len) {
|
|
|
|
w3 = t[c3]->when;
|
|
|
|
if(c3+1 < len && t[c3+1]->when < w3) {
|
|
|
|
w3 = t[c3+1]->when;
|
|
|
|
c3++;
|
|
|
|
}
|
|
|
|
if(w3 < w) {
|
|
|
|
w = w3;
|
|
|
|
c = c3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(w >= when)
|
2011-12-13 00:40:51 +01:00
|
|
|
break;
|
|
|
|
t[i] = t[c];
|
|
|
|
t[i]->i = i;
|
2013-11-06 20:49:01 +01:00
|
|
|
t[c] = tmp;
|
|
|
|
tmp->i = c;
|
2011-12-13 00:40:51 +01:00
|
|
|
i = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
static void
|
|
|
|
dumptimers(const char *msg)
|
|
|
|
{
|
|
|
|
Timer *t;
|
|
|
|
int32 i;
|
|
|
|
|
|
|
|
runtime_printf("timers: %s\n", msg);
|
|
|
|
for(i = 0; i < timers.len; i++) {
|
|
|
|
t = timers.t[i];
|
|
|
|
runtime_printf("\t%d\t%p:\ti %d when %D period %D fn %p\n",
|
|
|
|
i, t, t->i, t->when, t->period, t->fv->fn);
|
|
|
|
}
|
|
|
|
runtime_printf("\n");
|
|
|
|
}
|
|
|
|
|
2011-12-13 00:40:51 +01:00
|
|
|
void
|
2014-06-07 00:37:27 +02:00
|
|
|
runtime_time_scan(struct Workbuf** wbufp, void (*enqueue1)(struct Workbuf**, Obj))
|
2011-12-13 00:40:51 +01:00
|
|
|
{
|
2014-06-07 00:37:27 +02:00
|
|
|
enqueue1(wbufp, (Obj){(byte*)&timers, sizeof timers, 0});
|
2011-12-13 00:40:51 +01:00
|
|
|
}
|