file.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package winio
  2. import (
  3. "errors"
  4. "io"
  5. "runtime"
  6. "sync"
  7. "syscall"
  8. "time"
  9. )
  10. //sys cancelIoEx(file syscall.Handle, o *syscall.Overlapped) (err error) = CancelIoEx
  11. //sys createIoCompletionPort(file syscall.Handle, port syscall.Handle, key uintptr, threadCount uint32) (newport syscall.Handle, err error) = CreateIoCompletionPort
  12. //sys getQueuedCompletionStatus(port syscall.Handle, bytes *uint32, key *uintptr, o **ioOperation, timeout uint32) (err error) = GetQueuedCompletionStatus
  13. //sys setFileCompletionNotificationModes(h syscall.Handle, flags uint8) (err error) = SetFileCompletionNotificationModes
  14. //sys timeBeginPeriod(period uint32) (n int32) = winmm.timeBeginPeriod
  15. const (
  16. cFILE_SKIP_COMPLETION_PORT_ON_SUCCESS = 1
  17. cFILE_SKIP_SET_EVENT_ON_HANDLE = 2
  18. )
  19. var (
  20. ErrFileClosed = errors.New("file has already been closed")
  21. ErrTimeout = &timeoutError{}
  22. )
  23. type timeoutError struct{}
  24. func (e *timeoutError) Error() string { return "i/o timeout" }
  25. func (e *timeoutError) Timeout() bool { return true }
  26. func (e *timeoutError) Temporary() bool { return true }
  27. var ioInitOnce sync.Once
  28. var ioCompletionPort syscall.Handle
  29. // ioResult contains the result of an asynchronous IO operation
  30. type ioResult struct {
  31. bytes uint32
  32. err error
  33. }
  34. // ioOperation represents an outstanding asynchronous Win32 IO
  35. type ioOperation struct {
  36. o syscall.Overlapped
  37. ch chan ioResult
  38. }
  39. func initIo() {
  40. h, err := createIoCompletionPort(syscall.InvalidHandle, 0, 0, 0xffffffff)
  41. if err != nil {
  42. panic(err)
  43. }
  44. ioCompletionPort = h
  45. go ioCompletionProcessor(h)
  46. }
  47. // win32File implements Reader, Writer, and Closer on a Win32 handle without blocking in a syscall.
  48. // It takes ownership of this handle and will close it if it is garbage collected.
  49. type win32File struct {
  50. handle syscall.Handle
  51. wg sync.WaitGroup
  52. closing bool
  53. readDeadline time.Time
  54. writeDeadline time.Time
  55. }
  56. // makeWin32File makes a new win32File from an existing file handle
  57. func makeWin32File(h syscall.Handle) (*win32File, error) {
  58. f := &win32File{handle: h}
  59. ioInitOnce.Do(initIo)
  60. _, err := createIoCompletionPort(h, ioCompletionPort, 0, 0xffffffff)
  61. if err != nil {
  62. return nil, err
  63. }
  64. err = setFileCompletionNotificationModes(h, cFILE_SKIP_COMPLETION_PORT_ON_SUCCESS|cFILE_SKIP_SET_EVENT_ON_HANDLE)
  65. if err != nil {
  66. return nil, err
  67. }
  68. runtime.SetFinalizer(f, (*win32File).closeHandle)
  69. return f, nil
  70. }
  71. func MakeOpenFile(h syscall.Handle) (io.ReadWriteCloser, error) {
  72. return makeWin32File(h)
  73. }
  74. // closeHandle closes the resources associated with a Win32 handle
  75. func (f *win32File) closeHandle() {
  76. if !f.closing {
  77. // cancel all IO and wait for it to complete
  78. f.closing = true
  79. cancelIoEx(f.handle, nil)
  80. f.wg.Wait()
  81. // at this point, no new IO can start
  82. syscall.Close(f.handle)
  83. f.handle = 0
  84. }
  85. }
  86. // Close closes a win32File.
  87. func (f *win32File) Close() error {
  88. f.closeHandle()
  89. runtime.SetFinalizer(f, nil)
  90. return nil
  91. }
  92. // prepareIo prepares for a new IO operation
  93. func (f *win32File) prepareIo() (*ioOperation, error) {
  94. f.wg.Add(1)
  95. if f.closing {
  96. return nil, ErrFileClosed
  97. }
  98. c := &ioOperation{}
  99. c.ch = make(chan ioResult)
  100. return c, nil
  101. }
  102. // ioCompletionProcessor processes completed async IOs forever
  103. func ioCompletionProcessor(h syscall.Handle) {
  104. // Set the timer resolution to 1. This fixes a performance regression in golang 1.6.
  105. timeBeginPeriod(1)
  106. for {
  107. var bytes uint32
  108. var key uintptr
  109. var op *ioOperation
  110. err := getQueuedCompletionStatus(h, &bytes, &key, &op, syscall.INFINITE)
  111. if op == nil {
  112. panic(err)
  113. }
  114. op.ch <- ioResult{bytes, err}
  115. }
  116. }
  117. // asyncIo processes the return value from ReadFile or WriteFile, blocking until
  118. // the operation has actually completed.
  119. func (f *win32File) asyncIo(c *ioOperation, deadline time.Time, bytes uint32, err error) (int, error) {
  120. if err != syscall.ERROR_IO_PENDING {
  121. f.wg.Done()
  122. return int(bytes), err
  123. } else {
  124. var r ioResult
  125. wait := true
  126. timedout := false
  127. if f.closing {
  128. cancelIoEx(f.handle, &c.o)
  129. } else if !deadline.IsZero() {
  130. now := time.Now()
  131. if !deadline.After(now) {
  132. timedout = true
  133. } else {
  134. timeout := time.After(deadline.Sub(now))
  135. select {
  136. case r = <-c.ch:
  137. wait = false
  138. case <-timeout:
  139. timedout = true
  140. }
  141. }
  142. }
  143. if timedout {
  144. cancelIoEx(f.handle, &c.o)
  145. }
  146. if wait {
  147. r = <-c.ch
  148. }
  149. err = r.err
  150. if err == syscall.ERROR_OPERATION_ABORTED {
  151. if f.closing {
  152. err = ErrFileClosed
  153. } else if timedout {
  154. err = ErrTimeout
  155. }
  156. }
  157. f.wg.Done()
  158. return int(r.bytes), err
  159. }
  160. }
  161. // Read reads from a file handle.
  162. func (f *win32File) Read(b []byte) (int, error) {
  163. c, err := f.prepareIo()
  164. if err != nil {
  165. return 0, err
  166. }
  167. var bytes uint32
  168. err = syscall.ReadFile(f.handle, b, &bytes, &c.o)
  169. n, err := f.asyncIo(c, f.readDeadline, bytes, err)
  170. // Handle EOF conditions.
  171. if err == nil && n == 0 && len(b) != 0 {
  172. return 0, io.EOF
  173. } else if err == syscall.ERROR_BROKEN_PIPE {
  174. return 0, io.EOF
  175. } else {
  176. return n, err
  177. }
  178. }
  179. // Write writes to a file handle.
  180. func (f *win32File) Write(b []byte) (int, error) {
  181. c, err := f.prepareIo()
  182. if err != nil {
  183. return 0, err
  184. }
  185. var bytes uint32
  186. err = syscall.WriteFile(f.handle, b, &bytes, &c.o)
  187. return f.asyncIo(c, f.writeDeadline, bytes, err)
  188. }
  189. func (f *win32File) SetReadDeadline(t time.Time) error {
  190. f.readDeadline = t
  191. return nil
  192. }
  193. func (f *win32File) SetWriteDeadline(t time.Time) error {
  194. f.writeDeadline = t
  195. return nil
  196. }