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 the type of the variable ErrNetClosing.
17// This is used to implement the net.Error interface.
18type errNetClosing struct{}
19
20// Error returns the error message for ErrNetClosing.
21// Keep this string consistent because of issue #4373:
22// since historically programs have not been able to detect
23// this error, they look for the string.
24func (e errNetClosing) Error() string { return "use of closed network connection" }
25
26func (e errNetClosing) Timeout() bool   { return false }
27func (e errNetClosing) Temporary() bool { return false }
28
29// ErrNetClosing is returned when a network descriptor is used after
30// it has been closed.
31var ErrNetClosing = errNetClosing{}
32
33// ErrFileClosing is returned when a file descriptor is used after it
34// has been closed.
35var ErrFileClosing = errors.New("use of closed file")
36
37// ErrNoDeadline is returned when a request is made to set a deadline
38// on a file type that does not use the poller.
39var ErrNoDeadline = errors.New("file type does not support deadline")
40
41// Return the appropriate closing error based on isFile.
42func errClosing(isFile bool) error {
43	if isFile {
44		return ErrFileClosing
45	}
46	return ErrNetClosing
47}
48
49// ErrDeadlineExceeded is returned for an expired deadline.
50// This is exported by the os package as os.ErrDeadlineExceeded.
51var ErrDeadlineExceeded error = &DeadlineExceededError{}
52
53// DeadlineExceededError is returned for an expired deadline.
54type DeadlineExceededError struct{}
55
56// Implement the net.Error interface.
57// The string is "i/o timeout" because that is what was returned
58// by earlier Go versions. Changing it may break programs that
59// match on error strings.
60func (e *DeadlineExceededError) Error() string   { return "i/o timeout" }
61func (e *DeadlineExceededError) Timeout() bool   { return true }
62func (e *DeadlineExceededError) Temporary() bool { return true }
63
64// ErrNotPollable is returned when the file or socket is not suitable
65// for event notification.
66var ErrNotPollable = errors.New("not pollable")
67
68// consume removes data from a slice of byte slices, for writev.
69func consume(v *[][]byte, n int64) {
70	for len(*v) > 0 {
71		ln0 := int64(len((*v)[0]))
72		if ln0 > n {
73			(*v)[0] = (*v)[0][n:]
74			return
75		}
76		n -= ln0
77		*v = (*v)[1:]
78	}
79}
80
81// TestHookDidWritev is a hook for testing writev.
82var TestHookDidWritev = func(wrote int) {}
83