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.
10package poll
11
12import (
13	"errors"
14)
15
16// ErrNetClosing is returned when a network descriptor is used after
17// it has been closed. Keep this string consistent because of issue
18// #4373: since historically programs have not been able to detect
19// this error, they look for the string.
20var ErrNetClosing = errors.New("use of closed network connection")
21
22// ErrFileClosing is returned when a file descriptor is used after it
23// has been closed.
24var ErrFileClosing = errors.New("use of closed file")
25
26// ErrNoDeadline is returned when a request is made to set a deadline
27// on a file type that does not use the poller.
28var ErrNoDeadline = errors.New("file type does not support deadline")
29
30// Return the appropriate closing error based on isFile.
31func errClosing(isFile bool) error {
32	if isFile {
33		return ErrFileClosing
34	}
35	return ErrNetClosing
36}
37
38// ErrTimeout is returned for an expired deadline.
39var ErrTimeout error = &TimeoutError{}
40
41// TimeoutError is returned for an expired deadline.
42type TimeoutError struct{}
43
44// Implement the net.Error interface.
45func (e *TimeoutError) Error() string   { return "i/o timeout" }
46func (e *TimeoutError) Timeout() bool   { return true }
47func (e *TimeoutError) Temporary() bool { return true }
48
49// ErrNotPollable is returned when the file or socket is not suitable
50// for event notification.
51var ErrNotPollable = errors.New("not pollable")
52
53// consume removes data from a slice of byte slices, for writev.
54func consume(v *[][]byte, n int64) {
55	for len(*v) > 0 {
56		ln0 := int64(len((*v)[0]))
57		if ln0 > n {
58			(*v)[0] = (*v)[0][n:]
59			return
60		}
61		n -= ln0
62		*v = (*v)[1:]
63	}
64}
65
66// TestHookDidWritev is a hook for testing writev.
67var TestHookDidWritev = func(wrote int) {}
68