github.com/mh-cbon/go@v0.0.0-20160603070303-9e112a3fe4c0/src/net/fd_poll_runtime.go (about) 1 // Copyright 2013 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // +build darwin dragonfly freebsd linux netbsd openbsd windows solaris 6 7 package net 8 9 import ( 10 "sync" 11 "syscall" 12 "time" 13 ) 14 15 // runtimeNano returns the current value of the runtime clock in nanoseconds. 16 func runtimeNano() int64 17 18 func runtime_pollServerInit() 19 func runtime_pollOpen(fd uintptr) (uintptr, int) 20 func runtime_pollClose(ctx uintptr) 21 func runtime_pollWait(ctx uintptr, mode int) int 22 func runtime_pollWaitCanceled(ctx uintptr, mode int) int 23 func runtime_pollReset(ctx uintptr, mode int) int 24 func runtime_pollSetDeadline(ctx uintptr, d int64, mode int) 25 func runtime_pollUnblock(ctx uintptr) 26 27 type pollDesc struct { 28 runtimeCtx uintptr 29 } 30 31 var serverInit sync.Once 32 33 func (pd *pollDesc) init(fd *netFD) error { 34 serverInit.Do(runtime_pollServerInit) 35 ctx, errno := runtime_pollOpen(uintptr(fd.sysfd)) 36 if errno != 0 { 37 return syscall.Errno(errno) 38 } 39 pd.runtimeCtx = ctx 40 return nil 41 } 42 43 func (pd *pollDesc) close() { 44 if pd.runtimeCtx == 0 { 45 return 46 } 47 runtime_pollClose(pd.runtimeCtx) 48 pd.runtimeCtx = 0 49 } 50 51 // Evict evicts fd from the pending list, unblocking any I/O running on fd. 52 func (pd *pollDesc) evict() { 53 if pd.runtimeCtx == 0 { 54 return 55 } 56 runtime_pollUnblock(pd.runtimeCtx) 57 } 58 59 func (pd *pollDesc) prepare(mode int) error { 60 res := runtime_pollReset(pd.runtimeCtx, mode) 61 return convertErr(res) 62 } 63 64 func (pd *pollDesc) prepareRead() error { 65 return pd.prepare('r') 66 } 67 68 func (pd *pollDesc) prepareWrite() error { 69 return pd.prepare('w') 70 } 71 72 func (pd *pollDesc) wait(mode int) error { 73 res := runtime_pollWait(pd.runtimeCtx, mode) 74 return convertErr(res) 75 } 76 77 func (pd *pollDesc) waitRead() error { 78 return pd.wait('r') 79 } 80 81 func (pd *pollDesc) waitWrite() error { 82 return pd.wait('w') 83 } 84 85 func (pd *pollDesc) waitCanceled(mode int) { 86 runtime_pollWaitCanceled(pd.runtimeCtx, mode) 87 } 88 89 func (pd *pollDesc) waitCanceledRead() { 90 pd.waitCanceled('r') 91 } 92 93 func (pd *pollDesc) waitCanceledWrite() { 94 pd.waitCanceled('w') 95 } 96 97 func convertErr(res int) error { 98 switch res { 99 case 0: 100 return nil 101 case 1: 102 return errClosing 103 case 2: 104 return errTimeout 105 } 106 println("unreachable: ", res) 107 panic("unreachable") 108 } 109 110 func (fd *netFD) setDeadline(t time.Time) error { 111 return setDeadlineImpl(fd, t, 'r'+'w') 112 } 113 114 func (fd *netFD) setReadDeadline(t time.Time) error { 115 return setDeadlineImpl(fd, t, 'r') 116 } 117 118 func (fd *netFD) setWriteDeadline(t time.Time) error { 119 return setDeadlineImpl(fd, t, 'w') 120 } 121 122 func setDeadlineImpl(fd *netFD, t time.Time, mode int) error { 123 diff := int64(t.Sub(time.Now())) 124 d := runtimeNano() + diff 125 if d <= 0 && diff > 0 { 126 // If the user has a deadline in the future, but the delay calculation 127 // overflows, then set the deadline to the maximum possible value. 128 d = 1<<63 - 1 129 } 130 if t.IsZero() { 131 d = 0 132 } 133 if err := fd.incref(); err != nil { 134 return err 135 } 136 runtime_pollSetDeadline(fd.pd.runtimeCtx, d, mode) 137 fd.decref() 138 return nil 139 }