From 0a09c72c2e9f64a0888f0215db478840d152355f Mon Sep 17 00:00:00 2001 From: Mikio Hara Date: Tue, 14 Feb 2017 05:55:41 +0900 Subject: [PATCH] internal/poll: add RawControl, RawRead and RawWrite methods to FD This change adds RawControl, RawRead and RawWrite methods to type FD to make the runtime-integrated network poller work together with a user-defined function. The methods are used via the net package from external packages and type FD is considered as an implementation of syscall.Conn and syscall.RawConn interfaces. Updates #19435. Change-Id: I4ad04b10ffddb2b54fa8d70587440960d73c0a2d Reviewed-on: https://go-review.googlesource.com/37038 Run-TryBot: Mikio Hara TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- src/internal/poll/fd_plan9.go | 17 ++++++++++++ src/internal/poll/fd_unix.go | 49 +++++++++++++++++++++++++++++++++ src/internal/poll/fd_windows.go | 16 +++++++++++ 3 files changed, 82 insertions(+) diff --git a/src/internal/poll/fd_plan9.go b/src/internal/poll/fd_plan9.go index 49590ab13c..107f454523 100644 --- a/src/internal/poll/fd_plan9.go +++ b/src/internal/poll/fd_plan9.go @@ -5,6 +5,7 @@ package poll import ( + "errors" "io" "sync/atomic" "time" @@ -197,3 +198,19 @@ func isInterrupted(err error) bool { func PollDescriptor() uintptr { return ^uintptr(0) } + +// RawControl invokes the user-defined function f for a non-IO +// operation. +func (fd *FD) RawControl(f func(uintptr)) error { + return errors.New("not implemented") +} + +// RawRead invokes the user-defined function f for a read operation. +func (fd *FD) RawRead(f func(uintptr) bool) error { + return errors.New("not implemented") +} + +// RawWrite invokes the user-defined function f for a write operation. +func (fd *FD) RawWrite(f func(uintptr) bool) error { + return errors.New("not implemented") +} diff --git a/src/internal/poll/fd_unix.go b/src/internal/poll/fd_unix.go index 782ecd5a87..3ca6e157c5 100644 --- a/src/internal/poll/fd_unix.go +++ b/src/internal/poll/fd_unix.go @@ -399,3 +399,52 @@ func (fd *FD) Fstat(s *syscall.Stat_t) error { func (fd *FD) WaitWrite() error { return fd.pd.waitWrite(fd.isFile) } + +// RawControl invokes the user-defined function f for a non-IO +// operation. +func (fd *FD) RawControl(f func(uintptr)) error { + if err := fd.incref(); err != nil { + return err + } + defer fd.decref() + f(uintptr(fd.Sysfd)) + return nil +} + +// RawRead invokes the user-defined function f for a read operation. +func (fd *FD) RawRead(f func(uintptr) bool) error { + if err := fd.readLock(); err != nil { + return err + } + defer fd.readUnlock() + if err := fd.pd.prepareRead(fd.isFile); err != nil { + return err + } + for { + if f(uintptr(fd.Sysfd)) { + return nil + } + if err := fd.pd.waitRead(fd.isFile); err != nil { + return err + } + } +} + +// RawWrite invokes the user-defined function f for a write operation. +func (fd *FD) RawWrite(f func(uintptr) bool) error { + if err := fd.writeLock(); err != nil { + return err + } + defer fd.writeUnlock() + if err := fd.pd.prepareWrite(fd.isFile); err != nil { + return err + } + for { + if f(uintptr(fd.Sysfd)) { + return nil + } + if err := fd.pd.waitWrite(fd.isFile); err != nil { + return err + } + } +} diff --git a/src/internal/poll/fd_windows.go b/src/internal/poll/fd_windows.go index a41209c344..5c55c948c2 100644 --- a/src/internal/poll/fd_windows.go +++ b/src/internal/poll/fd_windows.go @@ -833,3 +833,19 @@ func (fd *FD) GetFileInformationByHandle(data *syscall.ByHandleFileInformation) defer fd.decref() return syscall.GetFileInformationByHandle(fd.Sysfd, data) } + +// RawControl invokes the user-defined function f for a non-IO +// operation. +func (fd *FD) RawControl(f func(uintptr)) error { + return errors.New("not implemented") +} + +// RawRead invokes the user-defined function f for a read operation. +func (fd *FD) RawRead(f func(uintptr) bool) error { + return errors.New("not implemented") +} + +// RawWrite invokes the user-defined function f for a write operation. +func (fd *FD) RawWrite(f func(uintptr) bool) error { + return errors.New("not implemented") +} -- 2.50.0