2016-10-18 16:38:29 +02:00
|
|
|
// Copyright 2013 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package runtime
|
|
|
|
|
|
|
|
import (
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
const _DWORD_MAX = 0xffffffff
|
|
|
|
|
|
|
|
const _INVALID_HANDLE_VALUE = ^uintptr(0)
|
|
|
|
|
2017-09-14 19:11:35 +02:00
|
|
|
// net_op must be the same as beginning of internal/poll.operation.
|
|
|
|
// Keep these in sync.
|
2016-10-18 16:38:29 +02:00
|
|
|
type net_op struct {
|
|
|
|
// used by windows
|
|
|
|
o overlapped
|
|
|
|
// used by netpoll
|
|
|
|
pd *pollDesc
|
|
|
|
mode int32
|
|
|
|
errno int32
|
|
|
|
qty uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
type overlappedEntry struct {
|
|
|
|
key uintptr
|
|
|
|
op *net_op // In reality it's *overlapped, but we cast it to *net_op anyway.
|
|
|
|
internal uintptr
|
|
|
|
qty uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
var iocphandle uintptr = _INVALID_HANDLE_VALUE // completion port io handle
|
|
|
|
|
|
|
|
func netpollinit() {
|
|
|
|
iocphandle = stdcall4(_CreateIoCompletionPort, _INVALID_HANDLE_VALUE, 0, 0, _DWORD_MAX)
|
|
|
|
if iocphandle == 0 {
|
2017-09-14 19:11:35 +02:00
|
|
|
println("runtime: CreateIoCompletionPort failed (errno=", getlasterror(), ")")
|
|
|
|
throw("runtime: netpollinit failed")
|
2016-10-18 16:38:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-14 19:11:35 +02:00
|
|
|
func netpolldescriptor() uintptr {
|
|
|
|
return iocphandle
|
|
|
|
}
|
|
|
|
|
2016-10-18 16:38:29 +02:00
|
|
|
func netpollopen(fd uintptr, pd *pollDesc) int32 {
|
|
|
|
if stdcall4(_CreateIoCompletionPort, fd, iocphandle, 0, 0) == 0 {
|
2018-01-09 02:23:08 +01:00
|
|
|
return int32(getlasterror())
|
2016-10-18 16:38:29 +02:00
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func netpollclose(fd uintptr) int32 {
|
|
|
|
// nothing to do
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func netpollarm(pd *pollDesc, mode int) {
|
2017-09-14 19:11:35 +02:00
|
|
|
throw("runtime: unused")
|
2016-10-18 16:38:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Polls for completed network IO.
|
|
|
|
// Returns list of goroutines that become runnable.
|
2019-01-18 20:04:36 +01:00
|
|
|
func netpoll(block bool) gList {
|
2016-10-18 16:38:29 +02:00
|
|
|
var entries [64]overlappedEntry
|
|
|
|
var wait, qty, key, flags, n, i uint32
|
|
|
|
var errno int32
|
|
|
|
var op *net_op
|
2019-01-18 20:04:36 +01:00
|
|
|
var toRun gList
|
2016-10-18 16:38:29 +02:00
|
|
|
|
|
|
|
mp := getg().m
|
|
|
|
|
|
|
|
if iocphandle == _INVALID_HANDLE_VALUE {
|
2019-01-18 20:04:36 +01:00
|
|
|
return gList{}
|
2016-10-18 16:38:29 +02:00
|
|
|
}
|
|
|
|
wait = 0
|
|
|
|
if block {
|
|
|
|
wait = _INFINITE
|
|
|
|
}
|
|
|
|
retry:
|
|
|
|
if _GetQueuedCompletionStatusEx != nil {
|
|
|
|
n = uint32(len(entries) / int(gomaxprocs))
|
|
|
|
if n < 8 {
|
|
|
|
n = 8
|
|
|
|
}
|
|
|
|
if block {
|
|
|
|
mp.blocked = true
|
|
|
|
}
|
|
|
|
if stdcall6(_GetQueuedCompletionStatusEx, iocphandle, uintptr(unsafe.Pointer(&entries[0])), uintptr(n), uintptr(unsafe.Pointer(&n)), uintptr(wait), 0) == 0 {
|
|
|
|
mp.blocked = false
|
|
|
|
errno = int32(getlasterror())
|
|
|
|
if !block && errno == _WAIT_TIMEOUT {
|
2019-01-18 20:04:36 +01:00
|
|
|
return gList{}
|
2016-10-18 16:38:29 +02:00
|
|
|
}
|
2017-09-14 19:11:35 +02:00
|
|
|
println("runtime: GetQueuedCompletionStatusEx failed (errno=", errno, ")")
|
|
|
|
throw("runtime: netpoll failed")
|
2016-10-18 16:38:29 +02:00
|
|
|
}
|
|
|
|
mp.blocked = false
|
|
|
|
for i = 0; i < n; i++ {
|
|
|
|
op = entries[i].op
|
|
|
|
errno = 0
|
|
|
|
qty = 0
|
|
|
|
if stdcall5(_WSAGetOverlappedResult, op.pd.fd, uintptr(unsafe.Pointer(op)), uintptr(unsafe.Pointer(&qty)), 0, uintptr(unsafe.Pointer(&flags))) == 0 {
|
|
|
|
errno = int32(getlasterror())
|
|
|
|
}
|
2019-01-18 20:04:36 +01:00
|
|
|
handlecompletion(&toRun, op, errno, qty)
|
2016-10-18 16:38:29 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
op = nil
|
|
|
|
errno = 0
|
|
|
|
qty = 0
|
|
|
|
if block {
|
|
|
|
mp.blocked = true
|
|
|
|
}
|
|
|
|
if stdcall5(_GetQueuedCompletionStatus, iocphandle, uintptr(unsafe.Pointer(&qty)), uintptr(unsafe.Pointer(&key)), uintptr(unsafe.Pointer(&op)), uintptr(wait)) == 0 {
|
|
|
|
mp.blocked = false
|
|
|
|
errno = int32(getlasterror())
|
|
|
|
if !block && errno == _WAIT_TIMEOUT {
|
2019-01-18 20:04:36 +01:00
|
|
|
return gList{}
|
2016-10-18 16:38:29 +02:00
|
|
|
}
|
|
|
|
if op == nil {
|
2017-09-14 19:11:35 +02:00
|
|
|
println("runtime: GetQueuedCompletionStatus failed (errno=", errno, ")")
|
|
|
|
throw("runtime: netpoll failed")
|
2016-10-18 16:38:29 +02:00
|
|
|
}
|
|
|
|
// dequeued failed IO packet, so report that
|
|
|
|
}
|
|
|
|
mp.blocked = false
|
2019-01-18 20:04:36 +01:00
|
|
|
handlecompletion(&toRun, op, errno, qty)
|
2016-10-18 16:38:29 +02:00
|
|
|
}
|
2019-01-18 20:04:36 +01:00
|
|
|
if block && toRun.empty() {
|
2016-10-18 16:38:29 +02:00
|
|
|
goto retry
|
|
|
|
}
|
2019-01-18 20:04:36 +01:00
|
|
|
return toRun
|
2016-10-18 16:38:29 +02:00
|
|
|
}
|
|
|
|
|
2019-01-18 20:04:36 +01:00
|
|
|
func handlecompletion(toRun *gList, op *net_op, errno int32, qty uint32) {
|
2016-10-18 16:38:29 +02:00
|
|
|
if op == nil {
|
2017-09-14 19:11:35 +02:00
|
|
|
println("runtime: GetQueuedCompletionStatus returned op == nil")
|
|
|
|
throw("runtime: netpoll failed")
|
2016-10-18 16:38:29 +02:00
|
|
|
}
|
|
|
|
mode := op.mode
|
|
|
|
if mode != 'r' && mode != 'w' {
|
2017-09-14 19:11:35 +02:00
|
|
|
println("runtime: GetQueuedCompletionStatus returned invalid mode=", mode)
|
|
|
|
throw("runtime: netpoll failed")
|
2016-10-18 16:38:29 +02:00
|
|
|
}
|
|
|
|
op.errno = errno
|
|
|
|
op.qty = qty
|
2019-01-18 20:04:36 +01:00
|
|
|
netpollready(toRun, op.pd, mode)
|
2016-10-18 16:38:29 +02:00
|
|
|
}
|