2013-07-16 08:54:42 +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.
|
|
|
|
|
2014-07-19 10:53:52 +02:00
|
|
|
// +build darwin dragonfly freebsd linux netbsd openbsd windows solaris
|
2013-07-16 08:54:42 +02:00
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2014-07-19 10:53:52 +02:00
|
|
|
// runtimeNano returns the current value of the runtime clock in nanoseconds.
|
|
|
|
func runtimeNano() int64
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
func runtime_pollServerInit()
|
2013-11-06 20:49:01 +01:00
|
|
|
func runtime_pollOpen(fd uintptr) (uintptr, int)
|
2013-07-16 08:54:42 +02:00
|
|
|
func runtime_pollClose(ctx uintptr)
|
|
|
|
func runtime_pollWait(ctx uintptr, mode int) int
|
2013-11-06 20:49:01 +01:00
|
|
|
func runtime_pollWaitCanceled(ctx uintptr, mode int) int
|
2013-07-16 08:54:42 +02:00
|
|
|
func runtime_pollReset(ctx uintptr, mode int) int
|
|
|
|
func runtime_pollSetDeadline(ctx uintptr, d int64, mode int)
|
|
|
|
func runtime_pollUnblock(ctx uintptr)
|
|
|
|
|
|
|
|
type pollDesc struct {
|
|
|
|
runtimeCtx uintptr
|
|
|
|
}
|
|
|
|
|
|
|
|
var serverInit sync.Once
|
|
|
|
|
|
|
|
func (pd *pollDesc) Init(fd *netFD) error {
|
|
|
|
serverInit.Do(runtime_pollServerInit)
|
2013-11-06 20:49:01 +01:00
|
|
|
ctx, errno := runtime_pollOpen(uintptr(fd.sysfd))
|
2013-07-16 08:54:42 +02:00
|
|
|
if errno != 0 {
|
|
|
|
return syscall.Errno(errno)
|
|
|
|
}
|
|
|
|
pd.runtimeCtx = ctx
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pd *pollDesc) Close() {
|
2013-11-06 20:49:01 +01:00
|
|
|
if pd.runtimeCtx == 0 {
|
|
|
|
return
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
runtime_pollClose(pd.runtimeCtx)
|
2013-11-06 20:49:01 +01:00
|
|
|
pd.runtimeCtx = 0
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Evict evicts fd from the pending list, unblocking any I/O running on fd.
|
2015-10-31 01:59:47 +01:00
|
|
|
func (pd *pollDesc) Evict() {
|
2013-11-06 20:49:01 +01:00
|
|
|
if pd.runtimeCtx == 0 {
|
2015-10-31 01:59:47 +01:00
|
|
|
return
|
2013-11-06 20:49:01 +01:00
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
runtime_pollUnblock(pd.runtimeCtx)
|
|
|
|
}
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
func (pd *pollDesc) Prepare(mode int) error {
|
|
|
|
res := runtime_pollReset(pd.runtimeCtx, mode)
|
2013-07-16 08:54:42 +02:00
|
|
|
return convertErr(res)
|
|
|
|
}
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
func (pd *pollDesc) PrepareRead() error {
|
|
|
|
return pd.Prepare('r')
|
|
|
|
}
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
func (pd *pollDesc) PrepareWrite() error {
|
2013-11-06 20:49:01 +01:00
|
|
|
return pd.Prepare('w')
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pd *pollDesc) Wait(mode int) error {
|
|
|
|
res := runtime_pollWait(pd.runtimeCtx, mode)
|
2013-07-16 08:54:42 +02:00
|
|
|
return convertErr(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pd *pollDesc) WaitRead() error {
|
2013-11-06 20:49:01 +01:00
|
|
|
return pd.Wait('r')
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pd *pollDesc) WaitWrite() error {
|
2013-11-06 20:49:01 +01:00
|
|
|
return pd.Wait('w')
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pd *pollDesc) WaitCanceled(mode int) {
|
|
|
|
runtime_pollWaitCanceled(pd.runtimeCtx, mode)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pd *pollDesc) WaitCanceledRead() {
|
|
|
|
pd.WaitCanceled('r')
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pd *pollDesc) WaitCanceledWrite() {
|
|
|
|
pd.WaitCanceled('w')
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func convertErr(res int) error {
|
|
|
|
switch res {
|
|
|
|
case 0:
|
|
|
|
return nil
|
|
|
|
case 1:
|
|
|
|
return errClosing
|
|
|
|
case 2:
|
|
|
|
return errTimeout
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
println("unreachable: ", res)
|
2013-07-16 08:54:42 +02:00
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
func (fd *netFD) setDeadline(t time.Time) error {
|
|
|
|
return setDeadlineImpl(fd, t, 'r'+'w')
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
func (fd *netFD) setReadDeadline(t time.Time) error {
|
|
|
|
return setDeadlineImpl(fd, t, 'r')
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
func (fd *netFD) setWriteDeadline(t time.Time) error {
|
|
|
|
return setDeadlineImpl(fd, t, 'w')
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func setDeadlineImpl(fd *netFD, t time.Time, mode int) error {
|
2014-07-19 10:53:52 +02:00
|
|
|
d := runtimeNano() + int64(t.Sub(time.Now()))
|
2013-07-16 08:54:42 +02:00
|
|
|
if t.IsZero() {
|
|
|
|
d = 0
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
if err := fd.incref(); err != nil {
|
2013-07-16 08:54:42 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
runtime_pollSetDeadline(fd.pd.runtimeCtx, d, mode)
|
|
|
|
fd.decref()
|
|
|
|
return nil
|
|
|
|
}
|