Much better sys.SetNonblock...

This commit is contained in:
Cheer Xiao 2014-03-17 22:39:57 +08:00
parent 79f80b94e9
commit 4a1415dcda
2 changed files with 12 additions and 3 deletions

View File

@ -66,7 +66,7 @@ type Reader struct {
}
func NewReader(f *os.File) *Reader {
if err := sys.SetNonblock(int(f.Fd())); err != nil {
if err := sys.SetNonblock(int(f.Fd()), true); err != nil {
panic(err)
}
rd := &Reader{

View File

@ -19,7 +19,16 @@ func GetNonblock(fd int) (bool, error) {
return r&syscall.O_NONBLOCK != 0, err
}
func SetNonblock(fd int) error {
_, err := Fcntl(fd, syscall.F_SETFL, syscall.O_NONBLOCK)
func SetNonblock(fd int, nonblock bool) error {
r, err := Fcntl(fd, syscall.F_GETFL, 0)
if err != nil {
return err
}
if nonblock {
r |= syscall.O_NONBLOCK
} else {
r &^= syscall.O_NONBLOCK
}
_, err = Fcntl(fd, syscall.F_SETFL, r)
return err
}