Skip to content

Commit 75c87df

Browse files
committed
internal/poll: pass the I/O mode instead of an overlapped object in execIO
execIO callers should be agnostic to the fact that it uses an overlapped object. This will unlock future optimizations and simplifications. Change-Id: I0a58d992101fa74ac75e3538af04cbc44156f0d6 Reviewed-on: https://go-review.googlesource.com/c/go/+/704175 Reviewed-by: Damien Neil <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Junyang Shao <[email protected]>
1 parent fc88e18 commit 75c87df

File tree

2 files changed

+35
-37
lines changed

2 files changed

+35
-37
lines changed

src/internal/poll/fd_windows.go

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,16 @@ func (fd *FD) cancelIO(o *operation) {
250250
// It supports both synchronous and asynchronous IO.
251251
// o.qty and o.flags are set to zero before calling submit
252252
// to avoid reusing the values from a previous call.
253-
func (fd *FD) execIO(o *operation, submit func(o *operation) (uint32, error)) (int, error) {
253+
func (fd *FD) execIO(mode int, submit func(o *operation) (uint32, error)) (int, error) {
254254
// Notify runtime netpoll about starting IO.
255-
err := fd.pd.prepare(int(o.mode), fd.isFile)
255+
err := fd.pd.prepare(mode, fd.isFile)
256256
if err != nil {
257257
return 0, err
258258
}
259+
o := &fd.rop
260+
if mode == 'w' {
261+
o = &fd.wop
262+
}
259263
// Start IO.
260264
if !fd.isBlocking && o.o.HEvent == 0 && !fd.pollable() {
261265
// If the handle is opened for overlapped IO but we can't
@@ -552,7 +556,7 @@ func (fd *FD) Read(buf []byte) (int, error) {
552556
case kindConsole:
553557
n, err = fd.readConsole(buf)
554558
case kindFile, kindPipe:
555-
n, err = fd.execIO(&fd.rop, func(o *operation) (qty uint32, err error) {
559+
n, err = fd.execIO('r', func(o *operation) (qty uint32, err error) {
556560
err = syscall.ReadFile(fd.Sysfd, buf, &qty, fd.overlapped(o))
557561
return qty, err
558562
})
@@ -567,7 +571,7 @@ func (fd *FD) Read(buf []byte) (int, error) {
567571
}
568572
}
569573
case kindNet:
570-
n, err = fd.execIO(&fd.rop, func(o *operation) (qty uint32, err error) {
574+
n, err = fd.execIO('r', func(o *operation) (qty uint32, err error) {
571575
var flags uint32
572576
err = syscall.WSARecv(fd.Sysfd, newWsaBuf(buf), 1, &qty, &flags, &o.o, nil)
573577
return qty, err
@@ -694,7 +698,7 @@ func (fd *FD) Pread(buf []byte, off int64) (int, error) {
694698
defer fd.setOffset(curoffset)
695699
}
696700
fd.setOffset(off)
697-
n, err := fd.execIO(&fd.rop, func(o *operation) (qty uint32, err error) {
701+
n, err := fd.execIO('r', func(o *operation) (qty uint32, err error) {
698702
err = syscall.ReadFile(fd.Sysfd, buf, &qty, &o.o)
699703
return qty, err
700704
})
@@ -727,7 +731,7 @@ func (fd *FD) ReadFrom(buf []byte) (int, syscall.Sockaddr, error) {
727731

728732
rsa := wsaRsaPool.Get().(*syscall.RawSockaddrAny)
729733
defer wsaRsaPool.Put(rsa)
730-
n, err := fd.execIO(&fd.rop, func(o *operation) (qty uint32, err error) {
734+
n, err := fd.execIO('r', func(o *operation) (qty uint32, err error) {
731735
rsan := int32(unsafe.Sizeof(*rsa))
732736
var flags uint32
733737
err = syscall.WSARecvFrom(fd.Sysfd, newWsaBuf(buf), 1, &qty, &flags, rsa, &rsan, &o.o, nil)
@@ -761,7 +765,7 @@ func (fd *FD) ReadFromInet4(buf []byte, sa4 *syscall.SockaddrInet4) (int, error)
761765

762766
rsa := wsaRsaPool.Get().(*syscall.RawSockaddrAny)
763767
defer wsaRsaPool.Put(rsa)
764-
n, err := fd.execIO(&fd.rop, func(o *operation) (qty uint32, err error) {
768+
n, err := fd.execIO('r', func(o *operation) (qty uint32, err error) {
765769
rsan := int32(unsafe.Sizeof(*rsa))
766770
var flags uint32
767771
err = syscall.WSARecvFrom(fd.Sysfd, newWsaBuf(buf), 1, &qty, &flags, rsa, &rsan, &o.o, nil)
@@ -795,7 +799,7 @@ func (fd *FD) ReadFromInet6(buf []byte, sa6 *syscall.SockaddrInet6) (int, error)
795799

796800
rsa := wsaRsaPool.Get().(*syscall.RawSockaddrAny)
797801
defer wsaRsaPool.Put(rsa)
798-
n, err := fd.execIO(&fd.rop, func(o *operation) (qty uint32, err error) {
802+
n, err := fd.execIO('r', func(o *operation) (qty uint32, err error) {
799803
rsan := int32(unsafe.Sizeof(*rsa))
800804
var flags uint32
801805
err = syscall.WSARecvFrom(fd.Sysfd, newWsaBuf(buf), 1, &qty, &flags, rsa, &rsan, &o.o, nil)
@@ -841,7 +845,7 @@ func (fd *FD) Write(buf []byte) (int, error) {
841845
case kindConsole:
842846
n, err = fd.writeConsole(b)
843847
case kindPipe, kindFile:
844-
n, err = fd.execIO(&fd.wop, func(o *operation) (qty uint32, err error) {
848+
n, err = fd.execIO('w', func(o *operation) (qty uint32, err error) {
845849
err = syscall.WriteFile(fd.Sysfd, b, &qty, fd.overlapped(o))
846850
return qty, err
847851
})
@@ -850,7 +854,7 @@ func (fd *FD) Write(buf []byte) (int, error) {
850854
if race.Enabled {
851855
race.ReleaseMerge(unsafe.Pointer(&ioSync))
852856
}
853-
n, err = fd.execIO(&fd.wop, func(o *operation) (qty uint32, err error) {
857+
n, err = fd.execIO('w', func(o *operation) (qty uint32, err error) {
854858
err = syscall.WSASend(fd.Sysfd, newWsaBuf(b), 1, &qty, 0, &o.o, nil)
855859
return qty, err
856860
})
@@ -949,7 +953,7 @@ func (fd *FD) Pwrite(buf []byte, off int64) (int, error) {
949953
max = ntotal + maxRW
950954
}
951955
fd.setOffset(off + int64(ntotal))
952-
n, err := fd.execIO(&fd.wop, func(o *operation) (qty uint32, err error) {
956+
n, err := fd.execIO('w', func(o *operation) (qty uint32, err error) {
953957
err = syscall.WriteFile(fd.Sysfd, buf[ntotal:max], &qty, &o.o)
954958
return qty, err
955959
})
@@ -979,7 +983,7 @@ func (fd *FD) Writev(buf *[][]byte) (int64, error) {
979983
}
980984
bufs := newWSABufs(buf)
981985
defer freeWSABufs(bufs)
982-
n, err := fd.execIO(&fd.wop, func(o *operation) (qty uint32, err error) {
986+
n, err := fd.execIO('w', func(o *operation) (qty uint32, err error) {
983987
err = syscall.WSASend(fd.Sysfd, &(*bufs)[0], uint32(len(*bufs)), &qty, 0, &o.o, nil)
984988
return qty, err
985989
})
@@ -997,7 +1001,7 @@ func (fd *FD) WriteTo(buf []byte, sa syscall.Sockaddr) (int, error) {
9971001

9981002
if len(buf) == 0 {
9991003
// handle zero-byte payload
1000-
n, err := fd.execIO(&fd.wop, func(o *operation) (qty uint32, err error) {
1004+
n, err := fd.execIO('w', func(o *operation) (qty uint32, err error) {
10011005
err = syscall.WSASendto(fd.Sysfd, &syscall.WSABuf{}, 1, &qty, 0, sa, &o.o, nil)
10021006
return qty, err
10031007
})
@@ -1015,7 +1019,7 @@ func (fd *FD) WriteTo(buf []byte, sa syscall.Sockaddr) (int, error) {
10151019
if len(b) > maxRW {
10161020
b = b[:maxRW]
10171021
}
1018-
n, err := fd.execIO(&fd.wop, func(o *operation) (qty uint32, err error) {
1022+
n, err := fd.execIO('w', func(o *operation) (qty uint32, err error) {
10191023
err = syscall.WSASendto(fd.Sysfd, newWsaBuf(b), 1, &qty, 0, sa, &o.o, nil)
10201024
return qty, err
10211025
})
@@ -1037,7 +1041,7 @@ func (fd *FD) WriteToInet4(buf []byte, sa4 *syscall.SockaddrInet4) (int, error)
10371041

10381042
if len(buf) == 0 {
10391043
// handle zero-byte payload
1040-
n, err := fd.execIO(&fd.wop, func(o *operation) (qty uint32, err error) {
1044+
n, err := fd.execIO('w', func(o *operation) (qty uint32, err error) {
10411045
err = windows.WSASendtoInet4(fd.Sysfd, &syscall.WSABuf{}, 1, &qty, 0, sa4, &o.o, nil)
10421046
return qty, err
10431047
})
@@ -1055,7 +1059,7 @@ func (fd *FD) WriteToInet4(buf []byte, sa4 *syscall.SockaddrInet4) (int, error)
10551059
if len(b) > maxRW {
10561060
b = b[:maxRW]
10571061
}
1058-
n, err := fd.execIO(&fd.wop, func(o *operation) (qty uint32, err error) {
1062+
n, err := fd.execIO('w', func(o *operation) (qty uint32, err error) {
10591063
err = windows.WSASendtoInet4(fd.Sysfd, newWsaBuf(b), 1, &qty, 0, sa4, &o.o, nil)
10601064
return qty, err
10611065
})
@@ -1077,7 +1081,7 @@ func (fd *FD) WriteToInet6(buf []byte, sa6 *syscall.SockaddrInet6) (int, error)
10771081

10781082
if len(buf) == 0 {
10791083
// handle zero-byte payload
1080-
n, err := fd.execIO(&fd.wop, func(o *operation) (qty uint32, err error) {
1084+
n, err := fd.execIO('w', func(o *operation) (qty uint32, err error) {
10811085
err = windows.WSASendtoInet6(fd.Sysfd, &syscall.WSABuf{}, 1, &qty, 0, sa6, &o.o, nil)
10821086
return qty, err
10831087
})
@@ -1095,7 +1099,7 @@ func (fd *FD) WriteToInet6(buf []byte, sa6 *syscall.SockaddrInet6) (int, error)
10951099
if len(b) > maxRW {
10961100
b = b[:maxRW]
10971101
}
1098-
n, err := fd.execIO(&fd.wop, func(o *operation) (qty uint32, err error) {
1102+
n, err := fd.execIO('w', func(o *operation) (qty uint32, err error) {
10991103
err = windows.WSASendtoInet6(fd.Sysfd, newWsaBuf(b), 1, &qty, 0, sa6, &o.o, nil)
11001104
return qty, err
11011105
})
@@ -1112,17 +1116,16 @@ func (fd *FD) WriteToInet6(buf []byte, sa6 *syscall.SockaddrInet6) (int, error)
11121116
// called when the descriptor is first created. This is here rather
11131117
// than in the net package so that it can use fd.wop.
11141118
func (fd *FD) ConnectEx(ra syscall.Sockaddr) error {
1115-
o := &fd.wop
1116-
_, err := fd.execIO(o, func(o *operation) (uint32, error) {
1119+
_, err := fd.execIO('w', func(o *operation) (uint32, error) {
11171120
return 0, ConnectExFunc(fd.Sysfd, ra, nil, 0, nil, &o.o)
11181121
})
11191122
return err
11201123
}
11211124

1122-
func (fd *FD) acceptOne(s syscall.Handle, rawsa []syscall.RawSockaddrAny, o *operation) (string, error) {
1125+
func (fd *FD) acceptOne(s syscall.Handle, rawsa []syscall.RawSockaddrAny) (string, error) {
11231126
// Submit accept request.
11241127
rsan := uint32(unsafe.Sizeof(rawsa[0]))
1125-
_, err := fd.execIO(o, func(o *operation) (qty uint32, err error) {
1128+
_, err := fd.execIO('r', func(o *operation) (qty uint32, err error) {
11261129
err = AcceptFunc(fd.Sysfd, s, (*byte)(unsafe.Pointer(&rawsa[0])), 0, rsan, rsan, &qty, &o.o)
11271130
return qty, err
11281131

@@ -1150,15 +1153,14 @@ func (fd *FD) Accept(sysSocket func() (syscall.Handle, error)) (syscall.Handle,
11501153
}
11511154
defer fd.readUnlock()
11521155

1153-
o := &fd.rop
11541156
var rawsa [2]syscall.RawSockaddrAny
11551157
for {
11561158
s, err := sysSocket()
11571159
if err != nil {
11581160
return syscall.InvalidHandle, nil, 0, "", err
11591161
}
11601162

1161-
errcall, err := fd.acceptOne(s, rawsa[:], o)
1163+
errcall, err := fd.acceptOne(s, rawsa[:])
11621164
if err == nil {
11631165
return s, rawsa[:], uint32(unsafe.Sizeof(rawsa[0])), "", nil
11641166
}
@@ -1286,7 +1288,7 @@ func (fd *FD) RawRead(f func(uintptr) bool) error {
12861288

12871289
// Use a zero-byte read as a way to get notified when this
12881290
// socket is readable. h/t https://stackoverflow.com/a/42019668/332798
1289-
_, err := fd.execIO(&fd.rop, func(o *operation) (qty uint32, err error) {
1291+
_, err := fd.execIO('r', func(o *operation) (qty uint32, err error) {
12901292
var flags uint32
12911293
if !fd.IsStream {
12921294
flags |= windows.MSG_PEEK
@@ -1381,7 +1383,7 @@ func (fd *FD) ReadMsg(p []byte, oob []byte, flags int) (int, int, int, syscall.S
13811383

13821384
msg := newWSAMsg(p, oob, flags, true)
13831385
defer freeWSAMsg(msg)
1384-
n, err := fd.execIO(&fd.rop, func(o *operation) (qty uint32, err error) {
1386+
n, err := fd.execIO('r', func(o *operation) (qty uint32, err error) {
13851387
err = windows.WSARecvMsg(fd.Sysfd, msg, &qty, &o.o, nil)
13861388
return qty, err
13871389
})
@@ -1406,7 +1408,7 @@ func (fd *FD) ReadMsgInet4(p []byte, oob []byte, flags int, sa4 *syscall.Sockadd
14061408

14071409
msg := newWSAMsg(p, oob, flags, true)
14081410
defer freeWSAMsg(msg)
1409-
n, err := fd.execIO(&fd.rop, func(o *operation) (qty uint32, err error) {
1411+
n, err := fd.execIO('r', func(o *operation) (qty uint32, err error) {
14101412
err = windows.WSARecvMsg(fd.Sysfd, msg, &qty, &o.o, nil)
14111413
return qty, err
14121414
})
@@ -1430,7 +1432,7 @@ func (fd *FD) ReadMsgInet6(p []byte, oob []byte, flags int, sa6 *syscall.Sockadd
14301432

14311433
msg := newWSAMsg(p, oob, flags, true)
14321434
defer freeWSAMsg(msg)
1433-
n, err := fd.execIO(&fd.rop, func(o *operation) (qty uint32, err error) {
1435+
n, err := fd.execIO('r', func(o *operation) (qty uint32, err error) {
14341436
err = windows.WSARecvMsg(fd.Sysfd, msg, &qty, &o.o, nil)
14351437
return qty, err
14361438
})
@@ -1461,7 +1463,7 @@ func (fd *FD) WriteMsg(p []byte, oob []byte, sa syscall.Sockaddr) (int, int, err
14611463
return 0, 0, err
14621464
}
14631465
}
1464-
n, err := fd.execIO(&fd.wop, func(o *operation) (qty uint32, err error) {
1466+
n, err := fd.execIO('w', func(o *operation) (qty uint32, err error) {
14651467
err = windows.WSASendMsg(fd.Sysfd, msg, 0, nil, &o.o, nil)
14661468
return qty, err
14671469
})
@@ -1484,7 +1486,7 @@ func (fd *FD) WriteMsgInet4(p []byte, oob []byte, sa *syscall.SockaddrInet4) (in
14841486
if sa != nil {
14851487
msg.Namelen = sockaddrInet4ToRaw(msg.Name, sa)
14861488
}
1487-
n, err := fd.execIO(&fd.wop, func(o *operation) (qty uint32, err error) {
1489+
n, err := fd.execIO('w', func(o *operation) (qty uint32, err error) {
14881490
err = windows.WSASendMsg(fd.Sysfd, msg, 0, nil, &o.o, nil)
14891491
return qty, err
14901492
})
@@ -1507,7 +1509,7 @@ func (fd *FD) WriteMsgInet6(p []byte, oob []byte, sa *syscall.SockaddrInet6) (in
15071509
if sa != nil {
15081510
msg.Namelen = sockaddrInet6ToRaw(msg.Name, sa)
15091511
}
1510-
n, err := fd.execIO(&fd.wop, func(o *operation) (qty uint32, err error) {
1512+
n, err := fd.execIO('w', func(o *operation) (qty uint32, err error) {
15111513
err = windows.WSASendMsg(fd.Sysfd, msg, 0, nil, &o.o, nil)
15121514
return qty, err
15131515
})

src/internal/poll/sendfile_windows.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,14 @@ func SendFile(fd *FD, src uintptr, size int64) (written int64, err error, handle
6262
// See https://docs.microsoft.com/en-us/windows/win32/api/mswsock/nf-mswsock-transmitfile
6363
const maxChunkSizePerCall = int64(0x7fffffff - 1)
6464

65-
o := &fd.wop
6665
for size > 0 {
6766
chunkSize := maxChunkSizePerCall
6867
if chunkSize > size {
6968
chunkSize = size
7069
}
7170

72-
off := startpos + written
73-
o.o.Offset = uint32(off)
74-
o.o.OffsetHigh = uint32(off >> 32)
75-
76-
n, err := fd.execIO(o, func(o *operation) (uint32, error) {
71+
fd.setOffset(startpos + written)
72+
n, err := fd.execIO('w', func(o *operation) (uint32, error) {
7773
err := syscall.TransmitFile(fd.Sysfd, hsrc, uint32(chunkSize), 0, &o.o, nil, syscall.TF_WRITE_BEHIND)
7874
if err != nil {
7975
return 0, err

0 commit comments

Comments
 (0)