file.go 7.2 KB

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