mirror of
https://github.com/golang/go
synced 2024-11-26 17:26:56 -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:
parent
be629138ab
commit
6478df1c41
@ -26,15 +26,32 @@ import (
|
|||||||
|
|
||||||
func Ticker(ns int64, c *chan int64) {
|
func Ticker(ns int64, c *chan int64) {
|
||||||
var tv syscall.Timeval;
|
var tv syscall.Timeval;
|
||||||
|
now := time.Nanoseconds();
|
||||||
|
when := now;
|
||||||
for {
|
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);
|
syscall.Syscall6(syscall.SYS_SELECT, 0, 0, 0, 0, syscall.TimevalPtr(&tv), 0);
|
||||||
nsec, err := time.Nanoseconds();
|
now = time.Nanoseconds();
|
||||||
c <- nsec;
|
c <- now;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export func Tick(ns int64) *chan int64 {
|
export func Tick(ns int64) *chan int64 {
|
||||||
|
if ns <= 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
c := new(chan int64);
|
c := new(chan int64);
|
||||||
go Ticker(ns, c);
|
go Ticker(ns, c);
|
||||||
return c;
|
return c;
|
||||||
|
@ -15,11 +15,11 @@ export func TestTick(t *testing.T) {
|
|||||||
Count uint64 = 10;
|
Count uint64 = 10;
|
||||||
);
|
);
|
||||||
c := Tick(Delta);
|
c := Tick(Delta);
|
||||||
t0, err := Nanoseconds();
|
t0 := Nanoseconds();
|
||||||
for i := 0; i < Count; i++ {
|
for i := 0; i < Count; i++ {
|
||||||
<-c;
|
<-c;
|
||||||
}
|
}
|
||||||
t1, err1 := Nanoseconds();
|
t1 := Nanoseconds();
|
||||||
ns := t1 - t0;
|
ns := t1 - t0;
|
||||||
target := int64(Delta*Count);
|
target := int64(Delta*Count);
|
||||||
slop := target*2/10;
|
slop := target*2/10;
|
||||||
|
@ -10,17 +10,21 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Seconds since January 1, 1970 00:00:00 GMT
|
// Seconds since January 1, 1970 00:00:00 GMT
|
||||||
export func Seconds() (sec int64, err *os.Error) {
|
export func Seconds() int64 {
|
||||||
var nsec int64;
|
sec, nsec, err := os.Time();
|
||||||
sec, nsec, err = os.Time();
|
if err != nil {
|
||||||
return sec, err
|
panic("time: os.Time: ", err.String());
|
||||||
|
}
|
||||||
|
return sec
|
||||||
}
|
}
|
||||||
|
|
||||||
// Nanoseconds since January 1, 1970 00:00:00 GMT
|
// Nanoseconds since January 1, 1970 00:00:00 GMT
|
||||||
export func Nanoseconds() (nsec int64, err *os.Error) {
|
export func Nanoseconds() int64 {
|
||||||
var sec int64;
|
sec, nsec, err := os.Time();
|
||||||
sec, nsec, err = os.Time();
|
if err != nil {
|
||||||
return sec*1e9 + nsec, err
|
panic("time: os.Time: ", err.String());
|
||||||
|
}
|
||||||
|
return sec*1e9 + nsec
|
||||||
}
|
}
|
||||||
|
|
||||||
export const (
|
export const (
|
||||||
@ -142,12 +146,7 @@ export func SecondsToUTC(sec int64) *Time {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export func UTC() (t *Time, err *os.Error) {
|
export func UTC() (t *Time, err *os.Error) {
|
||||||
var sec int64;
|
return SecondsToUTC(Seconds()), nil
|
||||||
sec, err = Seconds();
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return SecondsToUTC(sec), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Should this return an error?
|
// TODO: Should this return an error?
|
||||||
@ -163,12 +162,7 @@ export func SecondsToLocalTime(sec int64) *Time {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export func LocalTime() (t *Time, err *os.Error) {
|
export func LocalTime() (t *Time, err *os.Error) {
|
||||||
var sec int64;
|
return SecondsToLocalTime(Seconds()), nil
|
||||||
sec, err = Seconds();
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return SecondsToLocalTime(sec), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compute number of seconds since January 1, 1970.
|
// Compute number of seconds since January 1, 1970.
|
||||||
|
Loading…
Reference in New Issue
Block a user