From 4a1415dcda4c02fb965719815b6da023148908df Mon Sep 17 00:00:00 2001 From: Cheer Xiao Date: Mon, 17 Mar 2014 22:39:57 +0800 Subject: [PATCH] Much better sys.SetNonblock... --- edit/reader.go | 2 +- sys/fcntl.go | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/edit/reader.go b/edit/reader.go index 6d453b9b..a95ef9a9 100644 --- a/edit/reader.go +++ b/edit/reader.go @@ -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{ diff --git a/sys/fcntl.go b/sys/fcntl.go index 1bbd1db8..76a29dcb 100644 --- a/sys/fcntl.go +++ b/sys/fcntl.go @@ -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 }