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 package net 6 7 import ( 8 "runtime" 9 "syscall" 10 "time" 11 ) 12 13 type pollDesc struct { 14 fd *netFD 15 closing bool 16 } 17 18 func (pd *pollDesc) init(fd *netFD) error { pd.fd = fd; return nil } 19 20 func (pd *pollDesc) close() {} 21 22 func (pd *pollDesc) evict() { 23 pd.closing = true 24 if pd.fd != nil { 25 syscall.StopIO(pd.fd.sysfd) 26 runtime.KeepAlive(pd.fd) 27 } 28 } 29 30 func (pd *pollDesc) prepare(mode int) error { 31 if pd.closing { 32 return errClosing 33 } 34 return nil 35 } 36 37 func (pd *pollDesc) prepareRead() error { return pd.prepare('r') } 38 39 func (pd *pollDesc) prepareWrite() error { return pd.prepare('w') } 40 41 func (pd *pollDesc) wait(mode int) error { 42 if pd.closing { 43 return errClosing 44 } 45 return errTimeout 46 } 47 48 func (pd *pollDesc) waitRead() error { return pd.wait('r') } 49 50 func (pd *pollDesc) waitWrite() error { return pd.wait('w') } 51 52 func (pd *pollDesc) waitCanceled(mode int) {} 53 54 func (pd *pollDesc) waitCanceledRead() {} 55 56 func (pd *pollDesc) waitCanceledWrite() {} 57 58 func (fd *netFD) setDeadline(t time.Time) error { 59 return setDeadlineImpl(fd, t, 'r'+'w') 60 } 61 62 func (fd *netFD) setReadDeadline(t time.Time) error { 63 return setDeadlineImpl(fd, t, 'r') 64 } 65 66 func (fd *netFD) setWriteDeadline(t time.Time) error { 67 return setDeadlineImpl(fd, t, 'w') 68 } 69 70 func setDeadlineImpl(fd *netFD, t time.Time, mode int) error { 71 d := t.UnixNano() 72 if t.IsZero() { 73 d = 0 74 } 75 if err := fd.incref(); err != nil { 76 return err 77 } 78 switch mode { 79 case 'r': 80 syscall.SetReadDeadline(fd.sysfd, d) 81 case 'w': 82 syscall.SetWriteDeadline(fd.sysfd, d) 83 case 'r' + 'w': 84 syscall.SetReadDeadline(fd.sysfd, d) 85 syscall.SetWriteDeadline(fd.sysfd, d) 86 } 87 fd.decref() 88 return nil 89 } 90