Home | History | Annotate | Download | only in poll
      1 // Copyright 2017 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 poll supports non-blocking I/O on file descriptors with polling.
      6 // This supports I/O operations that block only a goroutine, not a thread.
      7 // This is used by the net and os packages.
      8 // It uses a poller built into the runtime, with support from the
      9 // runtime scheduler.
     10 package poll
     11 
     12 import "errors"
     13 
     14 // ErrNetClosing is returned when a network descriptor is used after
     15 // it has been closed. Keep this string consistent because of issue
     16 // #4373: since historically programs have not been able to detect
     17 // this error, they look for the string.
     18 var ErrNetClosing = errors.New("use of closed network connection")
     19 
     20 // ErrFileClosing is returned when a file descriptor is used after it
     21 // has been closed.
     22 var ErrFileClosing = errors.New("use of closed file")
     23 
     24 // ErrNoDeadline is returned when a request is made to set a deadline
     25 // on a file type that does not use the poller.
     26 var ErrNoDeadline = errors.New("file type does not support deadline")
     27 
     28 // Return the appropriate closing error based on isFile.
     29 func errClosing(isFile bool) error {
     30 	if isFile {
     31 		return ErrFileClosing
     32 	}
     33 	return ErrNetClosing
     34 }
     35 
     36 // ErrTimeout is returned for an expired deadline.
     37 var ErrTimeout error = &TimeoutError{}
     38 
     39 // TimeoutError is returned for an expired deadline.
     40 type TimeoutError struct{}
     41 
     42 // Implement the net.Error interface.
     43 func (e *TimeoutError) Error() string   { return "i/o timeout" }
     44 func (e *TimeoutError) Timeout() bool   { return true }
     45 func (e *TimeoutError) Temporary() bool { return true }
     46 
     47 // consume removes data from a slice of byte slices, for writev.
     48 func consume(v *[][]byte, n int64) {
     49 	for len(*v) > 0 {
     50 		ln0 := int64(len((*v)[0]))
     51 		if ln0 > n {
     52 			(*v)[0] = (*v)[0][n:]
     53 			return
     54 		}
     55 		n -= ln0
     56 		*v = (*v)[1:]
     57 	}
     58 }
     59 
     60 // TestHookDidWritev is a hook for testing writev.
     61 var TestHookDidWritev = func(wrote int) {}
     62