2009-02-16 18:07:11 -07: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.
|
|
|
|
|
|
|
|
package time
|
|
|
|
|
|
|
|
import (
|
2009-12-15 16:41:46 -07:00
|
|
|
"os"
|
|
|
|
"syscall"
|
2009-02-16 18:07:11 -07:00
|
|
|
)
|
|
|
|
|
2010-10-10 20:45:26 -06:00
|
|
|
// Sleep pauses the current goroutine for at least ns nanoseconds.
|
|
|
|
// Higher resolution sleeping may be provided by syscall.Nanosleep
|
|
|
|
// on some operating systems.
|
2010-02-04 14:09:02 -07:00
|
|
|
func Sleep(ns int64) os.Error {
|
2010-10-10 20:45:26 -06:00
|
|
|
_, 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 {
|
2010-02-04 14:09:02 -07:00
|
|
|
t := Nanoseconds()
|
2010-10-10 20:45:26 -06:00
|
|
|
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
|
2010-02-04 14:09:02 -07:00
|
|
|
end := t + ns
|
|
|
|
for t < end {
|
|
|
|
errno := syscall.Sleep(end - t)
|
|
|
|
if errno != 0 && errno != syscall.EINTR {
|
2010-10-10 20:45:26 -06:00
|
|
|
return 0, os.NewSyscallError("sleep", errno)
|
2010-02-04 14:09:02 -07:00
|
|
|
}
|
|
|
|
t = Nanoseconds()
|
|
|
|
}
|
2010-10-10 20:45:26 -06:00
|
|
|
return t, nil
|
2010-02-04 14:09:02 -07:00
|
|
|
}
|