1
0
mirror of https://github.com/golang/go synced 2024-11-26 15:36:59 -07:00

avoid skew in time.Tick; remove errors from time.Seconds, time.Nanoseconds

R=r
DELTA=46  (21 added, 10 deleted, 15 changed)
OCL=20785
CL=20787
This commit is contained in:
Russ Cox 2008-12-08 17:45:50 -08:00
parent be629138ab
commit 6478df1c41
3 changed files with 36 additions and 25 deletions

View File

@ -26,15 +26,32 @@ import (
func Ticker(ns int64, c *chan int64) {
var tv syscall.Timeval;
now := time.Nanoseconds();
when := now;
for {
syscall.nstotimeval(ns, &tv);
when += ns; // next alarm
// if c <- now took too long, skip ahead
if when < now {
// one big step
when += (now-when)/ns * ns;
}
for when <= now {
// little steps until when > now
when += ns
}
syscall.nstotimeval(when - now, &tv);
syscall.Syscall6(syscall.SYS_SELECT, 0, 0, 0, 0, syscall.TimevalPtr(&tv), 0);
nsec, err := time.Nanoseconds();
c <- nsec;
now = time.Nanoseconds();
c <- now;
}
}
export func Tick(ns int64) *chan int64 {
if ns <= 0 {
return nil
}
c := new(chan int64);
go Ticker(ns, c);
return c;

View File

@ -15,11 +15,11 @@ export func TestTick(t *testing.T) {
Count uint64 = 10;
);
c := Tick(Delta);
t0, err := Nanoseconds();
t0 := Nanoseconds();
for i := 0; i < Count; i++ {
<-c;
}
t1, err1 := Nanoseconds();
t1 := Nanoseconds();
ns := t1 - t0;
target := int64(Delta*Count);
slop := target*2/10;

View File

@ -10,17 +10,21 @@ import (
)
// Seconds since January 1, 1970 00:00:00 GMT
export func Seconds() (sec int64, err *os.Error) {
var nsec int64;
sec, nsec, err = os.Time();
return sec, err
export func Seconds() int64 {
sec, nsec, err := os.Time();
if err != nil {
panic("time: os.Time: ", err.String());
}
return sec
}
// Nanoseconds since January 1, 1970 00:00:00 GMT
export func Nanoseconds() (nsec int64, err *os.Error) {
var sec int64;
sec, nsec, err = os.Time();
return sec*1e9 + nsec, err
export func Nanoseconds() int64 {
sec, nsec, err := os.Time();
if err != nil {
panic("time: os.Time: ", err.String());
}
return sec*1e9 + nsec
}
export const (
@ -142,12 +146,7 @@ export func SecondsToUTC(sec int64) *Time {
}
export func UTC() (t *Time, err *os.Error) {
var sec int64;
sec, err = Seconds();
if err != nil {
return nil, err
}
return SecondsToUTC(sec), nil
return SecondsToUTC(Seconds()), nil
}
// TODO: Should this return an error?
@ -163,12 +162,7 @@ export func SecondsToLocalTime(sec int64) *Time {
}
export func LocalTime() (t *Time, err *os.Error) {
var sec int64;
sec, err = Seconds();
if err != nil {
return nil, err
}
return SecondsToLocalTime(sec), nil
return SecondsToLocalTime(Seconds()), nil
}
// Compute number of seconds since January 1, 1970.