-
Notifications
You must be signed in to change notification settings - Fork 10
/
register.go
224 lines (204 loc) · 4.37 KB
/
register.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// +build linux
package iouring
import (
"fmt"
"sync"
"syscall"
"unsafe"
)
// RegisterEventFd is used to register an event file descriptor to a ring.
func RegisterEventFd(ringFd int, fd int) error {
_, _, errno := syscall.Syscall6(
RegisterSyscall,
uintptr(ringFd),
uintptr(RegRegisterEventFd),
uintptr(fd),
uintptr(1),
uintptr(0),
uintptr(0),
)
if errno < 0 {
var err error
err = errno
return err
}
return nil
}
// RegisterEventFdAsync is used to register an event file descriptor for async
// polling on a ring.
func RegisterEventFdAsync(ringFd int, fd int) error {
_, _, errno := syscall.Syscall6(
RegisterSyscall,
uintptr(ringFd),
uintptr(RegRegisterEventFdAsync),
uintptr(fd),
uintptr(1),
uintptr(0),
uintptr(0),
)
if errno < 0 {
var err error
err = errno
return err
}
return nil
}
// UnregisterEventFd is used to unregister a file descriptor to a ring.
func UnregisterEventFd(ringFd int, fd int) error {
_, _, errno := syscall.Syscall6(
RegisterSyscall,
uintptr(ringFd),
uintptr(RegRegisterEventFd),
uintptr(0),
uintptr(0),
uintptr(0),
uintptr(0),
)
if errno < 0 {
var err error
err = errno
return err
}
return nil
}
// RegisterBuffers is used to register buffers to a ring.
func RegisterBuffers(fd int, vecs []*syscall.Iovec) error {
_, _, errno := syscall.Syscall6(
RegisterSyscall,
uintptr(fd),
uintptr(RegRegisterBuffers),
uintptr(unsafe.Pointer(&vecs[0])),
uintptr(len(vecs)),
uintptr(0),
uintptr(0),
)
if errno < 0 {
var err error
err = errno
return err
}
return nil
}
// UnregisterBuffers is used to unregister iovecs from a ring.
func UnregisterBuffers(fd int, vecs []*syscall.Iovec) error {
_, _, errno := syscall.Syscall6(
RegisterSyscall,
uintptr(fd),
uintptr(RegUnregisterBuffers),
uintptr(unsafe.Pointer(&vecs[0])),
uintptr(len(vecs)),
uintptr(0),
uintptr(0),
)
if errno < 0 {
var err error
err = errno
return err
}
return nil
}
// RegisterFiles is used to register files to a ring.
func RegisterFiles(fd int, files []int) error {
_, _, errno := syscall.Syscall6(
RegisterSyscall,
uintptr(fd),
uintptr(RegRegisterFiles),
uintptr(unsafe.Pointer(&files[0])),
uintptr(len(files)),
uintptr(0),
uintptr(0),
)
if errno < 0 {
var err error
err = errno
return err
}
return nil
}
// UnregisterFiles is used to unregister files to a ring.
func UnregisterFiles(fd int, files []int) error {
_, _, errno := syscall.Syscall6(
RegisterSyscall,
uintptr(fd),
uintptr(RegUnregisterFiles),
uintptr(unsafe.Pointer(&files[0])),
uintptr(len(files)),
uintptr(0),
uintptr(0),
)
if errno < 0 {
var err error
err = errno
return err
}
return nil
}
// ReregisterFiles is used to reregister files to a ring.
func ReregisterFiles(fd int, files []int) error {
_, _, errno := syscall.Syscall6(
RegisterSyscall,
uintptr(fd),
uintptr(RegRegisterFilesUpdate),
uintptr(unsafe.Pointer(&files[0])),
uintptr(len(files)),
uintptr(0),
uintptr(0),
)
if errno < 0 {
var err error
err = errno
return err
}
return nil
}
// FileRegistry is an interface for registering files to a Ring.
type FileRegistry interface {
Register(int) error
Unregister(int) error
ID(int) (int, bool)
}
type fileRegistry struct {
mu sync.RWMutex
ringFd int
f []int
fID map[int]int /* map of fd to offset */
}
// NewFileRegistry creates a FileRegistry for use with a ring.
func NewFileRegistry(ringFd int) FileRegistry {
return &fileRegistry{
ringFd: ringFd,
f: []int{},
fID: map[int]int{},
}
}
// Register implements the FileRegistry interface. It is used to register a
// file descriptor with a ring.
func (r *fileRegistry) Register(fd int) error {
r.mu.Lock()
defer r.mu.Unlock()
r.f = append(r.f, fd)
r.fID[fd] = len(r.f) - 1
if r.fID[fd] < 0 {
r.fID[fd] = 0
}
return ReregisterFiles(r.ringFd, r.f)
}
// Unregister implements the FileRegistry interface. It is used to unregister a
// file descriptor form a ring.
func (r *fileRegistry) Unregister(fd int) error {
r.mu.Lock()
defer r.mu.Unlock()
id, ok := r.fID[fd]
if !ok {
return fmt.Errorf("fd %d not registered", fd)
}
r.f = append(r.f[:id], r.f[id+1:]...)
return UnregisterFiles(r.ringFd, r.f)
}
// ID returns the ID of a file descriptor that has been registered.
func (r *fileRegistry) ID(fd int) (int, bool) {
r.mu.RLock()
defer r.mu.RUnlock()
id, ok := r.fID[fd]
return id, ok
}