file.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // Copyright 2021 The Go Authors. All rights reserved.
  2. // Copyright 2015 Microsoft
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. //go:build windows
  6. // +build windows
  7. package namedpipe
  8. import (
  9. "io"
  10. "os"
  11. "runtime"
  12. "sync"
  13. "sync/atomic"
  14. "time"
  15. "unsafe"
  16. "golang.org/x/sys/windows"
  17. )
  18. type timeoutChan chan struct{}
  19. var ioInitOnce sync.Once
  20. var ioCompletionPort windows.Handle
  21. // ioResult contains the result of an asynchronous IO operation
  22. type ioResult struct {
  23. bytes uint32
  24. err error
  25. }
  26. // ioOperation represents an outstanding asynchronous Win32 IO
  27. type ioOperation struct {
  28. o windows.Overlapped
  29. ch chan ioResult
  30. }
  31. func initIo() {
  32. h, err := windows.CreateIoCompletionPort(windows.InvalidHandle, 0, 0, 0)
  33. if err != nil {
  34. panic(err)
  35. }
  36. ioCompletionPort = h
  37. go ioCompletionProcessor(h)
  38. }
  39. // file implements Reader, Writer, and Closer on a Win32 handle without blocking in a syscall.
  40. // It takes ownership of this handle and will close it if it is garbage collected.
  41. type file struct {
  42. handle windows.Handle
  43. wg sync.WaitGroup
  44. wgLock sync.RWMutex
  45. closing uint32 // used as atomic boolean
  46. socket bool
  47. readDeadline deadlineHandler
  48. writeDeadline deadlineHandler
  49. }
  50. type deadlineHandler struct {
  51. setLock sync.Mutex
  52. channel timeoutChan
  53. channelLock sync.RWMutex
  54. timer *time.Timer
  55. timedout uint32 // used as atomic boolean
  56. }
  57. // makeFile makes a new file from an existing file handle
  58. func makeFile(h windows.Handle) (*file, error) {
  59. f := &file{handle: h}
  60. ioInitOnce.Do(initIo)
  61. _, err := windows.CreateIoCompletionPort(h, ioCompletionPort, 0, 0)
  62. if err != nil {
  63. return nil, err
  64. }
  65. err = windows.SetFileCompletionNotificationModes(h, windows.FILE_SKIP_COMPLETION_PORT_ON_SUCCESS|windows.FILE_SKIP_SET_EVENT_ON_HANDLE)
  66. if err != nil {
  67. return nil, err
  68. }
  69. f.readDeadline.channel = make(timeoutChan)
  70. f.writeDeadline.channel = make(timeoutChan)
  71. return f, nil
  72. }
  73. // closeHandle closes the resources associated with a Win32 handle
  74. func (f *file) closeHandle() {
  75. f.wgLock.Lock()
  76. // Atomically set that we are closing, releasing the resources only once.
  77. if atomic.SwapUint32(&f.closing, 1) == 0 {
  78. f.wgLock.Unlock()
  79. // cancel all IO and wait for it to complete
  80. windows.CancelIoEx(f.handle, nil)
  81. f.wg.Wait()
  82. // at this point, no new IO can start
  83. windows.Close(f.handle)
  84. f.handle = 0
  85. } else {
  86. f.wgLock.Unlock()
  87. }
  88. }
  89. // Close closes a file.
  90. func (f *file) Close() error {
  91. f.closeHandle()
  92. return nil
  93. }
  94. // prepareIo prepares for a new IO operation.
  95. // The caller must call f.wg.Done() when the IO is finished, prior to Close() returning.
  96. func (f *file) prepareIo() (*ioOperation, error) {
  97. f.wgLock.RLock()
  98. if atomic.LoadUint32(&f.closing) == 1 {
  99. f.wgLock.RUnlock()
  100. return nil, os.ErrClosed
  101. }
  102. f.wg.Add(1)
  103. f.wgLock.RUnlock()
  104. c := &ioOperation{}
  105. c.ch = make(chan ioResult)
  106. return c, nil
  107. }
  108. // ioCompletionProcessor processes completed async IOs forever
  109. func ioCompletionProcessor(h windows.Handle) {
  110. for {
  111. var bytes uint32
  112. var key uintptr
  113. var op *ioOperation
  114. err := windows.GetQueuedCompletionStatus(h, &bytes, &key, (**windows.Overlapped)(unsafe.Pointer(&op)), windows.INFINITE)
  115. if op == nil {
  116. panic(err)
  117. }
  118. op.ch <- ioResult{bytes, err}
  119. }
  120. }
  121. // asyncIo processes the return value from ReadFile or WriteFile, blocking until
  122. // the operation has actually completed.
  123. func (f *file) asyncIo(c *ioOperation, d *deadlineHandler, bytes uint32, err error) (int, error) {
  124. if err != windows.ERROR_IO_PENDING {
  125. return int(bytes), err
  126. }
  127. if atomic.LoadUint32(&f.closing) == 1 {
  128. windows.CancelIoEx(f.handle, &c.o)
  129. }
  130. var timeout timeoutChan
  131. if d != nil {
  132. d.channelLock.Lock()
  133. timeout = d.channel
  134. d.channelLock.Unlock()
  135. }
  136. var r ioResult
  137. select {
  138. case r = <-c.ch:
  139. err = r.err
  140. if err == windows.ERROR_OPERATION_ABORTED {
  141. if atomic.LoadUint32(&f.closing) == 1 {
  142. err = os.ErrClosed
  143. }
  144. } else if err != nil && f.socket {
  145. // err is from Win32. Query the overlapped structure to get the winsock error.
  146. var bytes, flags uint32
  147. err = windows.WSAGetOverlappedResult(f.handle, &c.o, &bytes, false, &flags)
  148. }
  149. case <-timeout:
  150. windows.CancelIoEx(f.handle, &c.o)
  151. r = <-c.ch
  152. err = r.err
  153. if err == windows.ERROR_OPERATION_ABORTED {
  154. err = os.ErrDeadlineExceeded
  155. }
  156. }
  157. // runtime.KeepAlive is needed, as c is passed via native
  158. // code to ioCompletionProcessor, c must remain alive
  159. // until the channel read is complete.
  160. runtime.KeepAlive(c)
  161. return int(r.bytes), err
  162. }
  163. // Read reads from a file handle.
  164. func (f *file) Read(b []byte) (int, error) {
  165. c, err := f.prepareIo()
  166. if err != nil {
  167. return 0, err
  168. }
  169. defer f.wg.Done()
  170. if atomic.LoadUint32(&f.readDeadline.timedout) == 1 {
  171. return 0, os.ErrDeadlineExceeded
  172. }
  173. var bytes uint32
  174. err = windows.ReadFile(f.handle, b, &bytes, &c.o)
  175. n, err := f.asyncIo(c, &f.readDeadline, bytes, err)
  176. runtime.KeepAlive(b)
  177. // Handle EOF conditions.
  178. if err == nil && n == 0 && len(b) != 0 {
  179. return 0, io.EOF
  180. } else if err == windows.ERROR_BROKEN_PIPE {
  181. return 0, io.EOF
  182. } else {
  183. return n, err
  184. }
  185. }
  186. // Write writes to a file handle.
  187. func (f *file) Write(b []byte) (int, error) {
  188. c, err := f.prepareIo()
  189. if err != nil {
  190. return 0, err
  191. }
  192. defer f.wg.Done()
  193. if atomic.LoadUint32(&f.writeDeadline.timedout) == 1 {
  194. return 0, os.ErrDeadlineExceeded
  195. }
  196. var bytes uint32
  197. err = windows.WriteFile(f.handle, b, &bytes, &c.o)
  198. n, err := f.asyncIo(c, &f.writeDeadline, bytes, err)
  199. runtime.KeepAlive(b)
  200. return n, err
  201. }
  202. func (f *file) SetReadDeadline(deadline time.Time) error {
  203. return f.readDeadline.set(deadline)
  204. }
  205. func (f *file) SetWriteDeadline(deadline time.Time) error {
  206. return f.writeDeadline.set(deadline)
  207. }
  208. func (f *file) Flush() error {
  209. return windows.FlushFileBuffers(f.handle)
  210. }
  211. func (f *file) Fd() uintptr {
  212. return uintptr(f.handle)
  213. }
  214. func (d *deadlineHandler) set(deadline time.Time) error {
  215. d.setLock.Lock()
  216. defer d.setLock.Unlock()
  217. if d.timer != nil {
  218. if !d.timer.Stop() {
  219. <-d.channel
  220. }
  221. d.timer = nil
  222. }
  223. atomic.StoreUint32(&d.timedout, 0)
  224. select {
  225. case <-d.channel:
  226. d.channelLock.Lock()
  227. d.channel = make(chan struct{})
  228. d.channelLock.Unlock()
  229. default:
  230. }
  231. if deadline.IsZero() {
  232. return nil
  233. }
  234. timeoutIO := func() {
  235. atomic.StoreUint32(&d.timedout, 1)
  236. close(d.channel)
  237. }
  238. now := time.Now()
  239. duration := deadline.Sub(now)
  240. if deadline.After(now) {
  241. // Deadline is in the future, set a timer to wait
  242. d.timer = time.AfterFunc(duration, timeoutIO)
  243. } else {
  244. // Deadline is in the past. Cancel all pending IO now.
  245. timeoutIO()
  246. }
  247. return nil
  248. }