mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
time: add After
Permits one to easily put a timeout in a select:
select {
case <-ch:
// foo
case <-time.After(1e6):
// bar
}
R=r, rog, rsc, sameer1, PeterGo, iant, nigeltao_gnome
CC=golang-dev
https://golang.org/cl/2321043
This commit is contained in:
parent
fd311cb144
commit
1e66a21348
2 changed files with 37 additions and 5 deletions
|
|
@ -9,18 +9,38 @@ import (
|
||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Sleep pauses the current goroutine for at least ns nanoseconds. Higher resolution
|
// Sleep pauses the current goroutine for at least ns nanoseconds.
|
||||||
// sleeping may be provided by syscall.Nanosleep on some operating systems.
|
// Higher resolution sleeping may be provided by syscall.Nanosleep
|
||||||
|
// on some operating systems.
|
||||||
func Sleep(ns int64) os.Error {
|
func Sleep(ns int64) os.Error {
|
||||||
// TODO(cw): use monotonic-time once it's available
|
_, err := sleep(Nanoseconds(), ns)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// After waits at least ns nanoseconds before sending the current time
|
||||||
|
// on the returned channel.
|
||||||
|
func After(ns int64) <-chan int64 {
|
||||||
t := Nanoseconds()
|
t := Nanoseconds()
|
||||||
|
ch := make(chan int64, 1)
|
||||||
|
go func() {
|
||||||
|
t, _ = sleep(t, ns)
|
||||||
|
ch <- t
|
||||||
|
}()
|
||||||
|
return ch
|
||||||
|
}
|
||||||
|
|
||||||
|
// sleep takes the current time and a duration,
|
||||||
|
// pauses for at least ns nanoseconds, and
|
||||||
|
// returns the current time and an error.
|
||||||
|
func sleep(t, ns int64) (int64, os.Error) {
|
||||||
|
// TODO(cw): use monotonic-time once it's available
|
||||||
end := t + ns
|
end := t + ns
|
||||||
for t < end {
|
for t < end {
|
||||||
errno := syscall.Sleep(end - t)
|
errno := syscall.Sleep(end - t)
|
||||||
if errno != 0 && errno != syscall.EINTR {
|
if errno != 0 && errno != syscall.EINTR {
|
||||||
return os.NewSyscallError("sleep", errno)
|
return 0, os.NewSyscallError("sleep", errno)
|
||||||
}
|
}
|
||||||
t = Nanoseconds()
|
t = Nanoseconds()
|
||||||
}
|
}
|
||||||
return nil
|
return t, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,3 +24,15 @@ func TestSleep(t *testing.T) {
|
||||||
t.Fatalf("Sleep(%d) slept for only %d ns", delay, duration)
|
t.Fatalf("Sleep(%d) slept for only %d ns", delay, duration)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAfter(t *testing.T) {
|
||||||
|
const delay = int64(100e6)
|
||||||
|
start := Nanoseconds()
|
||||||
|
end := <-After(delay)
|
||||||
|
if duration := Nanoseconds() - start; duration < delay {
|
||||||
|
t.Fatalf("After(%d) slept for only %d ns", delay, duration)
|
||||||
|
}
|
||||||
|
if min := start + delay; end < min {
|
||||||
|
t.Fatalf("After(%d) expect >= %d, got %d", delay, min, end)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue