Revendor hcsshim and go-winio

Signed-off-by: Darren Stahl <darst@microsoft.com>
This commit is contained in:
Darren Stahl
2017-07-25 12:03:44 -07:00
parent c52523c4d3
commit cec6331e4b
3 changed files with 15 additions and 955 deletions

View File

@@ -23,6 +23,13 @@ type atomicBool int32
func (b *atomicBool) isSet() bool { return atomic.LoadInt32((*int32)(b)) != 0 }
func (b *atomicBool) setFalse() { atomic.StoreInt32((*int32)(b), 0) }
func (b *atomicBool) setTrue() { atomic.StoreInt32((*int32)(b), 1) }
func (b *atomicBool) swap(new bool) bool {
var newInt int32
if new {
newInt = 1
}
return atomic.SwapInt32((*int32)(b), newInt) == 1
}
const (
cFILE_SKIP_COMPLETION_PORT_ON_SUCCESS = 1
@@ -69,10 +76,9 @@ func initIo() {
// win32File implements Reader, Writer, and Closer on a Win32 handle without blocking in a syscall.
// It takes ownership of this handle and will close it if it is garbage collected.
type win32File struct {
sync.Mutex
handle syscall.Handle
wg sync.WaitGroup
closing bool
closing atomicBool
readDeadline deadlineHandler
writeDeadline deadlineHandler
}
@@ -106,28 +112,17 @@ func MakeOpenFile(h syscall.Handle) (io.ReadWriteCloser, error) {
return makeWin32File(h)
}
func (f *win32File) isClosing() bool {
f.Lock()
closing := f.closing
f.Unlock()
return closing
}
// closeHandle closes the resources associated with a Win32 handle
func (f *win32File) closeHandle() {
f.Lock()
if !f.closing {
// Atomically set that we are closing, releasing the resources only once.
if !f.closing.swap(true) {
// cancel all IO and wait for it to complete
f.closing = true
f.Unlock()
cancelIoEx(f.handle, nil)
f.wg.Wait()
// at this point, no new IO can start
syscall.Close(f.handle)
f.handle = 0
return
}
f.Unlock()
}
// Close closes a win32File.
@@ -139,7 +134,7 @@ func (f *win32File) Close() error {
// prepareIo prepares for a new IO operation.
// The caller must call f.wg.Done() when the IO is finished, prior to Close() returning.
func (f *win32File) prepareIo() (*ioOperation, error) {
if f.isClosing() {
if f.closing.isSet() {
return nil, ErrFileClosed
}
f.wg.Add(1)
@@ -171,7 +166,7 @@ func (f *win32File) asyncIo(c *ioOperation, d *deadlineHandler, bytes uint32, er
return int(bytes), err
}
if f.isClosing() {
if f.closing.isSet() {
cancelIoEx(f.handle, &c.o)
}
@@ -187,7 +182,7 @@ func (f *win32File) asyncIo(c *ioOperation, d *deadlineHandler, bytes uint32, er
case r = <-c.ch:
err = r.err
if err == syscall.ERROR_OPERATION_ABORTED {
if f.isClosing() {
if f.closing.isSet() {
err = ErrFileClosed
}
}