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