Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
1e66428d
Commit
1e66428d
authored
Feb 04, 2010
by
Christopher Wedgwood
Committed by
Russ Cox
Feb 04, 2010
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
time: Sleep through interruptions
R=rsc CC=golang-dev
https://golang.org/cl/202043
parent
b655fa8d
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
39 additions
and
2 deletions
+39
-2
sleep.go
src/pkg/time/sleep.go
+13
-2
sleep_test.go
src/pkg/time/sleep_test.go
+26
-0
No files found.
src/pkg/time/sleep.go
View file @
1e66428d
...
@@ -11,5 +11,16 @@ import (
...
@@ -11,5 +11,16 @@ import (
// Sleep pauses the current goroutine for at least ns nanoseconds. Higher resolution
// Sleep pauses the current goroutine for at least ns nanoseconds. Higher resolution
// sleeping may be provided by syscall.Nanosleep on some operating systems.
// sleeping may be provided by syscall.Nanosleep on some operating systems.
// Sleep returns os.EINTR if interrupted.
func
Sleep
(
ns
int64
)
os
.
Error
{
func
Sleep
(
ns
int64
)
os
.
Error
{
return
os
.
NewSyscallError
(
"sleep"
,
syscall
.
Sleep
(
ns
))
}
// TODO(cw): use monotonic-time once it's available
t
:=
Nanoseconds
()
end
:=
t
+
ns
for
t
<
end
{
errno
:=
syscall
.
Sleep
(
end
-
t
)
if
errno
!=
0
&&
errno
!=
syscall
.
EINTR
{
return
os
.
NewSyscallError
(
"sleep"
,
errno
)
}
t
=
Nanoseconds
()
}
return
nil
}
src/pkg/time/sleep_test.go
0 → 100644
View file @
1e66428d
// 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_test
import
(
"os"
"syscall"
"testing"
.
"time"
)
func
TestSleep
(
t
*
testing
.
T
)
{
const
delay
=
int64
(
100e6
)
go
func
()
{
Sleep
(
delay
/
2
)
syscall
.
Kill
(
os
.
Getpid
(),
syscall
.
SIGCHLD
)
}()
start
:=
Nanoseconds
()
Sleep
(
delay
)
duration
:=
Nanoseconds
()
-
start
if
duration
<
delay
{
t
.
Fatalf
(
"Sleep(%d) slept for only %d ns"
,
delay
,
duration
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment