forked from coyove/gotfo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfd_poll_runtime.go
117 lines (100 loc) · 2.6 KB
/
fd_poll_runtime.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package gotfo
import (
"errors"
"time"
)
func (pd *pollDesc) close() {
if pd.runtimeCtx == 0 {
return
}
runtime_pollClose(pd.runtimeCtx)
pd.runtimeCtx = 0
}
// Evict evicts fd from the pending list, unblocking any I/O running on fd.
func (pd *pollDesc) evict() {
if pd.runtimeCtx == 0 {
return
}
runtime_pollUnblock(pd.runtimeCtx)
}
func (pd *pollDesc) prepare(mode int) error {
if pd.runtimeCtx == 0 {
return nil
}
res := runtime_pollReset(pd.runtimeCtx, mode)
return convertErr(res)
}
func (pd *pollDesc) prepareRead(isFile bool) error {
return pd.prepare('r')
}
func (pd *pollDesc) prepareWrite(isFile bool) error {
return pd.prepare('w')
}
func (pd *pollDesc) wait(mode int) error {
if pd.runtimeCtx == 0 {
return errors.New("waiting for unsupported file type")
}
res := runtime_pollWait(pd.runtimeCtx, mode)
return convertErr(res)
}
func (pd *pollDesc) waitRead() error {
return pd.wait('r')
}
func (pd *pollDesc) waitWrite() error {
return pd.wait('w')
}
func (pd *pollDesc) waitCanceled(mode int) {
if pd.runtimeCtx == 0 {
return
}
runtime_pollWaitCanceled(pd.runtimeCtx, mode)
}
func (pd *pollDesc) pollable() bool {
return pd.runtimeCtx != 0
}
func convertErr(res int) error {
switch res {
case 0:
return nil
case 1:
return errClosing
case 2:
return errTimeout
}
println("unreachable: ", res)
panic("unreachable")
}
// SetDeadline sets the read and write deadlines associated with fd.
func (fd *netFD) SetDeadline(t time.Time) error {
return setDeadlineImpl(fd, t, 'r'+'w')
}
// SetReadDeadline sets the read deadline associated with fd.
func (fd *netFD) SetReadDeadline(t time.Time) error {
return setDeadlineImpl(fd, t, 'r')
}
// SetWriteDeadline sets the write deadline associated with fd.
func (fd *netFD) SetWriteDeadline(t time.Time) error {
return setDeadlineImpl(fd, t, 'w')
}
func setDeadlineImpl(fd *netFD, t time.Time, mode int) error {
diff := int64(time.Until(t))
d := runtimeNano() + diff
if d <= 0 && diff > 0 {
// If the user has a deadline in the future, but the delay calculation
// overflows, then set the deadline to the maximum possible value.
d = 1<<63 - 1
}
if t.IsZero() {
d = 0
}
if err := fd.incref(); err != nil {
return err
}
// https://github.com/golang/go/commit/3813f941f6ff80f64d14db35f8b6446e10e45411#diff-2fac41fb57b6d5728ad14f6115f7219b
defer fd.decref()
if fd.pd.runtimeCtx == 0 {
return errors.New("file type does not support deadlines")
}
runtime_pollSetDeadline(fd.pd.runtimeCtx, d, mode)
return nil
}