file.go 7.0 KB

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