]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgo/go/time/sleep.go
Add Go frontend, libgo library, and Go testsuite.
[thirdparty/gcc.git] / libgo / go / time / sleep.go
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package time
6
7 import (
8 "os"
9 "syscall"
10 )
11
12 // Sleep pauses the current goroutine for at least ns nanoseconds.
13 // Higher resolution sleeping may be provided by syscall.Nanosleep
14 // on some operating systems.
15 func Sleep(ns int64) os.Error {
16 _, err := sleep(Nanoseconds(), ns)
17 return err
18 }
19
20 // After waits at least ns nanoseconds before sending the current time
21 // on the returned channel.
22 func After(ns int64) <-chan int64 {
23 t := Nanoseconds()
24 ch := make(chan int64, 1)
25 go func() {
26 t, _ = sleep(t, ns)
27 ch <- t
28 }()
29 return ch
30 }
31
32 // sleep takes the current time and a duration,
33 // pauses for at least ns nanoseconds, and
34 // returns the current time and an error.
35 func sleep(t, ns int64) (int64, os.Error) {
36 // TODO(cw): use monotonic-time once it's available
37 end := t + ns
38 for t < end {
39 errno := syscall.Sleep(end - t)
40 if errno != 0 && errno != syscall.EINTR {
41 return 0, os.NewSyscallError("sleep", errno)
42 }
43 t = Nanoseconds()
44 }
45 return t, nil
46 }